How to Create Custom Lookup View in CRM

Today I am going to tell you how to do filter lookup field using JavaScript.

Here you need to do two things:

  1. Get the filtered data in the form of fetchXML as per the your requirement.

  2. You will need to create a custom look view. (You are going to click 'View more records,' if you dont have it will throw an error so you need to create custom lookup view)

So here...

  1. My requirement is On Entity Custom3 I have two fields,

    lookup field Custome1 (lookup to Custome1 entity) and lookup field Custome2 (lookup to Custome2 entity).

    Lookup field Custome1 should get filtered by lookup field Custome2.

    (Entity Custome1 is having N:1 relationship with Entity Custome2)

    I have code ready we can use it:
    1. getFetchXMLData: function(entityLogicalName, attibute, attributeValue)   
    2. {  
    3.     /// <summary>  
    4.     /// Generates the XML for entity with conditions  
    5.     /// </summary>  
    6.     /// <param name="entityLogicalName" type="string">  
    7.     /// Entity logical name  
    8.     /// </param>  
    9.     /// <param name="attibute" type="string">  
    10.     /// Attribute name for filter  
    11.     /// </param>  
    12.     /// <param name="attributeValue" type="string">  
    13.     /// Attribute value for filter  
    14.     /// </param>  
    15.     /// <returns type="Xml"> Xml Records</returns>   
    16.     var condition = null;  
    17.     if (attibute != null || attributeValue != null) condition = " <filter type='and'> " +  
    18.         " <condition attribute='" + attibute + "' operator='eq' value='" + attributeValue + "' /> " +  
    19.         " </filter> "  
    20.     var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true' >" +  
    21.         "<entity name='" + entityLogicalName + "' >" + condition +  
    22.         "</entity>" +  
    23.         "</fetch>"  
    24.     return fetchXml;  
    25. }  
  2. Now the second step: We have filtered data and we need to create a custom lookup view.

    I have code ready that can be used here:
    1. createCustomLookupView: function(columnsForCustomLookup, entityLogicalName, fieldName, viewDisplayName, fetchXml)  
    2. {  
    3.     /// <summary>  
    4.     /// A function to Create a custom lookup view  
    5.     /// </summary>  
    6.     /// <param name="columnsForCustomLookup" type="Array">  
    7.     /// An array list of columns for view  
    8.     /// </param>  
    9.     /// <param name="entityLogicalName" type="String">  
    10.     /// Entity logical name  
    11.     /// </param>  
    12.     /// <param name="fieldName" type="String">  
    13.     /// Field Name view be shown  
    14.     /// </param>  
    15.     /// <param name="viewDisplayName" type="String">  
    16.     /// Display name of view  
    17.     /// </param>  
    18.     /// <param name="fetchXML" type="Xml">  
    19.     /// XMl result  
    20.     /// </param>  
    21.     var columns = "";  
    22.     for (var i = 0; i < columnsForCustomLookup.length; i++)   
    23.     {  
    24.         columns += "<cell name='" + columnsForCustomLookup[i] + "' width='150' />";  
    25.     }  
    26.     var layoutXml = "<grid name='resultset' object='1' select='1' icon='1' preview='1'>" +  
    27.         "<row name='result' id='" + entityLogicalName + "id'>" + columns +  
    28.         "</row>" +  
    29.         "</grid>";  
    30.     // add the randomly generated GUID for the view id  
    31.     var viewId = "{00000000-0000-0000-0000-000000000001}";  
    32.     Xrm.Page.getControl(fieldName).addCustomView(viewId, entityLogicalName, viewDisplayName, fetchXml, layoutXml, true);  
    33. }  
    Now on change event of lookup Custome2 field I can call the methods:
    1. onCustome2Change: function(context)  
    2. {  
    3.     var guidOfCustome2;  
    4.     var lookup = Xrm.Page.data.entity.attributes.get(Custome2);  
    5.     if (lookup != null)   
    6.     {  
    7.         var lookUpObjectValue = lookup.getValue();  
    8.         if ((lookUpObjectValue != null))   
    9.         {  
    10.             guidOfA = lookUpObjectValue[0].id;  
    11.         }  
    12.     }  
    13.     if (guidOfA == nullreturn;  
    14.     var fetchXML = getFetchXMLData(logicalNameOfCustom1, Custome2, guidOfCustome2)  
    15.         //Creates the custom lookup view  
    16.     createCustomLookupView([name, phone], logicalNameOfCustom1, Custome1, " Custom Lookup View Name", fetchXml);  
    17. }  

Hope this will help you to get your work done.

If you have any issues with the code, please comment below and suggestions are welcomed.

Ebook Download
View all
Learn
View all