Thursday, 26 May 2016

CRM 2016/2015/2013: Applying custom FetchXml to a subgrid using JavaScript

I have faced a scenario to show Account related contacts emails in sub-grid on account form.
In my scenario there is a many to many relationship between account and contact.
and i want to show Account related contacts emails in sub-grid on Account.

To achieve this goal i add a sub-grid of emails on account form.



Then i developed fetch XML in advanced find to show account related contacts emails.

var FetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
                                "<entity name='email'>" +
                                    "<attribute name='subject' />" +
                                    "<order attribute='subject' descending='false' />" +
                                    "<attribute name='to' />" +
                                    "<attribute name='from' />" +
                                    "<attribute name='createdon' />" +
                                    "<attribute name='activityid' />" +
                                    "<link-entity name='activityparty' from='activityid' to='activityid' alias='ad'>" +
                                       "<link-entity name='contact' from='contactid' to='partyid' alias='ae'>" +
                                       "<link-entity name='new_account_contact' from='contactid' to='contactid' visible='false' intersect='true'>" +
                                           "<filter type='and'>" +
                                            "<condition attribute='accountid' uitype='account' value='" + accountid + "' operator='eq'/>" +
                                         "</filter>" +
                                       "</link-entity>" +
                                     "</link-entity>" +
                                    "</link-entity>" +
                                    "</entity>" +
                                  "</fetch>";


To apply this custom fetch XML  to the sub-grid i used the below JavaScript function.

function dynamicallyFilterSubGrid() {
    var objSubGrid = window.parent.document.getElementById("RelatedContactEmails");
    //CRM loads subgrid after form is loaded.. so when we are adding script on form load.. need to wait unitil subgrid is loaded.
    // thats why adding delay..
    if (objSubGrid == null) {
        setTimeout(dynamicallyFilterSubGrid, 2000);
        return;
    }
    else {
        if (objSubGrid.control != null) {
            var accountid = Xrm.Page.data.entity.getId();
            var FetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
                                "<entity name='email'>" +
                                    "<attribute name='subject' />" +
                                    "<order attribute='subject' descending='false' />" +
                                    "<attribute name='to' />" +
                                    "<attribute name='from' />" +
                                    "<attribute name='createdon' />" +
                                    "<attribute name='activityid' />" +
                                    "<link-entity name='activityparty' from='activityid' to='activityid' alias='ad'>" +
                                       "<link-entity name='contact' from='contactid' to='partyid' alias='ae'>" +
                                       "<link-entity name='new_account_contact' from='contactid' to='contactid' visible='false' intersect='true'>" +
                                           "<filter type='and'>" +
                                            "<condition attribute='accountid' uitype='account' value='" + accountid + "' operator='eq'/>" +
                                         "</filter>" +
                                       "</link-entity>" +
                                     "</link-entity>" +
                                    "</link-entity>" +
                                    "</entity>" +
                                  "</fetch>";
            objSubGrid.control.SetParameter("fetchXml", FetchXml);
            objSubGrid.control.Refresh();
        }
        else {
            setTimeout(dynamicallyFilterSubGrid, 2000); // this setTimeout function is necessay because first objSubGrid.control is null
        }
    }
}

No comments:

Post a Comment

Read only field , get latest value from read only field in in power app portal

Below line is used to make field readonly in power app portal through js $( "#bdo_relationshiprole_name" ).parent().css( "po...