Pages

Wednesday 19 February 2014

Filtering Records in Sub-Grid Based upon the Lookup

Hi Folks,

The Below scenario explains how to filter the Subgrid using a record selected in Lookup.

In this scenario I have a lookup for selecting the courses in Post Graduation. If we select any of the record in lookup, then the corresponding subjects must be displayed in the grid view.

The below screen shot shoes the subgrid name



For creating this we require the FetchXml code, which we can get through Advanced Find in CRM.
The below snippet code can be used to filter the records,

---------------->>

function updateSubGrid() {  
    var Subjects = document.getElementById("Subjects");
    var lookupfield = new Array;
var dept;
    lookupfield = Xrm.Page.getAttribute("lookup schema name").getValue();
    if (lookupfield != null) {
        var lookupid = lookupfield[0].id;
dept=lookupfield[0].name;                              //I am storing the name of record in "dept" variable 
}
    else 
{
        return;
}
    if (Subjects ==null || Subjects.readyState != "complete")
{
        setTimeout('updateSubGrid()', 2000);
        return;
}    
    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
    fetchXml += "<entity name='new_subjects'>";
    fetchXml += "<attribute name='new_subjectsid' />";
    fetchXml += "<attribute name='new_name' />";
    fetchXml += "<attribute name='createdon' />";
    fetchXml += "<order attribute='new_name' descending='false' />";
    fetchXml += "<filter type='and'>";
    fetchXml += "<condition attribute='new_dept' operator='eq' uiname='"+ dept +"' uitype='new_department' value='" + lookupid + "' />";                                                               //dept specifies the name of record and lookupid represents Guid
    fetchXml += "</filter>";
    fetchXml += "</entity>";
    fetchXml += "</fetch>";
    Subjects.control.SetParameter("fetchXml", fetchXml);
    Subjects.control.refresh(); 

}   

1 comment:

  1. sir i have a task in which i want to filter the records that appear in the lookup field of the subgrid between two entity which have relationship of N:N type.
    This lookup field is been generated dynamically on the click of plus image in the subgrid so i cannot get the id of the filed to filter it.
    please help me out with dis issue.
    Thanks
    Chetan Khandelwal

    ReplyDelete