Text Scroll

You must welcome, Thank you for visiting my blog.

Search This Blog

Monday, July 6, 2020

Set default search records to sub grid in ms crm / custom search or filter in sub grid

//For account using primary contact

function filterSubGrid() {
var accountsGrid = document.getElementById("accounts"); //grid to filter
if (accountsGrid == null) { //make sure the grid has loaded
    setTimeout(function () {
        filterSubGrid();
    }, 2000); //if the grid hasn’t loaded run this again when it has
    return;
}

var contactValue = Xrm.Page.getAttribute("primarycontactid").getValue(); //field to filter by

var contactId = "00000000-0000-0000-0000-000000000000"; //if filter field is null display nothing
if (contactValue != null) {
    var contactId = contactValue[0].id;
}

//fetch xml code which will retrieve all the accounts related to the contact
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
    "  <entity name='account'>" +
    "    <attribute name='name' />" +
    "    <attribute name='address1_city' />" +
    "    <attribute name='primarycontactid' />" +
    "    <attribute name='telephone1' />" +
    "    <attribute name='accountid' />" +
    "    <order attribute='name' descending='false' />" +
    "    <filter type='and'>" +
    "      <condition attribute='primarycontactid' operator='eq' uitype='contact' value='" + contactId + "' />" +
    "    </filter>" +
    "    <link-entity name='contact' from='contactid' to='primarycontactid' visible='false' link-type='outer' alias='accountprimarycontactidcontactcontactid'>" +
    "      <attribute name='emailaddress1' />" +
    "    </link-entity>" +
    "  </entity>" +
    "</fetch>";

accountsGrid.control.SetParameter("fetchXml", fetchXml); //set the fetch xml to the sub grid
accountsGrid.control.refresh(); //refresh the sub grid using the new fetch xml

No comments:

Post a Comment