Wednesday, 18 January 2012

Custom Filtered Lookup in MS CRM 2011 - Runtime Dynamically



Hey All,

Greetings..

While working on a project of CRM 2011 i came across a scenario where i needed to build a custom lookup on run time dynamically. After go-ogling for many hours could find a simple method  in CRM 2011 as compared in CRM 4.0.

Here is a function below 

Create a new library resource file and add the following function and save it.

function SetCustomLookup() {
    //get the current account
    var requiredlookupfilter = Xrm.Page.getAttribute("Lookupid for which you need filtering").getValue();
    var requiredlookupfilterid = requiredlookupfilter[0].id;
    var requiredlookupfiltername = requiredlookupfilter[0].name;

   //any guid you can generate one if required online Please click the below link

    
    var viewId = "{131da142-5b91-4c6f-986b-0b70a3a3a35b}";
    var entityName = "Look up Entity name";
    var viewDisplayName = "Active Users for " + requiredlookupfiltername;
    //build fetchxml – better to generate it from Advanced Find
    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                   "<entity name='Look up Entity name'>" +
                   "<attribute name='Look up Entity required column field 1' />" +
                   "<attribute name=' Look up Entity required column field 2' />" +
                   "<attribute name=' Look up Entity required column field 3' />" +
                   "<order attribute=' Look up Entity required column field 1' descending='false' />" +
                   "<filter type='and'>" +
                   "<condition attribute=' Look up Entity required column condition 1' operator='eq' value='" + requiredlookupfilterid + "' />" +
                   "<condition attribute='statuscode' operator='eq' value='1' />" +
                   "</filter>" +
                   "</entity>" +
                   "</fetch>";

    //build grid layout
    var layoutXml = "<grid name='resultset' " +
                             "object='1' " +
                             "jump=' Look up Entity id' " +
                             "select='1' " +
                             "icon='1' " +
                             "preview='1'>" +
                         "<row name='result' " +
                              "id=' Look up Entity column field 1'>" +
                           "<cell name=' Look up Entity column field 2' " +
                                 "width='300' />" +
                           "<cell name=' Look up Entity required column field 3 " +
                                 "width='100' />" +
                                 "disableSorting='1' />" +
                         "</row>" +
                       "</grid>";

    //add new view view
    Xrm.Page.getControl("Lookup Control id ").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
 }


Once the library is ready..open the entity form and add the function SetCustomLookup on onload event and if any other as per your requirement.

Regards
Sandeep Patil

No comments:

Post a Comment