Saturday 27 April 2013

CRM 2011 Embed Report in IFrame and Pass Parameters from the Form

In CRM you can insert SubGrid and Chart. However this functionality is limited in a way that it can only grab the related entities that have direct relationship to that entity. If we want to get the other related entities from the related entity, it's not possible using the OOTB Subgrid and Chart.

In my case I want to display a chart in an Engagement Plan entity. This Engagement Plan is associated with an Account (N:1). The Account has 1:N relationships with Production History and Contract History entities that have information about commodity and tonnes produced. I want that chart to be shown on the Engagement Plan form. The parameters for this chart will be the Account Id and the Season Id located on the form.



The Steps:
  1. Create a custom SSRS Report to display this chart, taking @CRM_AccountId and @CRM_SeasonId as parameters.
  2. Insert an IFRAME on the form and give dummy Url. Also uncheck 'Restrict Cross Site Scripting' to allow cross scripting between servers. 
  3. Create a javascript that is triggered in Form_OnLoad, Account_OnChange, and Season_OnChange:

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    // JScript source code
    function Form_onload() {
        loadChart();
    }
    function loadChart() {
        var growerLookupItem = new Array();
        growerLookupItem = Xrm.Page.getAttribute("new_growerentityid").getValue();
        var seasonLookupItem = new Array();
        seasonLookupItem = Xrm.Page.getAttribute("new_seasonid").getValue();
        var serverUrl = Xrm.Page.context.getServerUrl();
        var url = serverUrl + "/crmreports/viewer/viewer.aspx?action=filter&helpID=CommodityTonnes.rdl&id=%7bB135D3F7-65C1-E011-A653-005056A10003%7d";
        SetReportUrl(url, "IFRAME_CommodityTonnes", growerLookupItem, seasonLookupItem);
    }
    function SetReportUrl(reportUrl, iFrame, growerLookupItem, seasonLookupItem) {
        if (growerLookupItem == null || seasonLookupItem == null) {
            Xrm.Page.getControl(iFrame).setVisible(false);
        }
        else {
            Xrm.Page.getControl(iFrame).setVisible(true);
            var growerId = growerLookupItem[0].id;
            var seasonId = seasonLookupItem[0].id;
            Xrm.Page.getControl(iFrame).setSrc(reportUrl + "&p:CRM_AccountId=" + growerId + "&p:CRM_SeasonId=" + seasonId);
        }
    }

The method for the form OnLoad is 'Form_onload'
The method for the field Onchange is 'loadChart'