Selecting Lookup View Dynamically Using JavaScript In Dynamics CRM/ 365

Lookup Views are shown when the user clicks on "Look Up More Records" in any Lookup field. In Dynamics CRM/365, Lookup Views are available in almost all entities. In newly created custom entities also, it will be available by default. Lookup View of Account Entity is shown. 

Introduction

If you haven’t worked with Lookup Views before, follow the below screenshots, or you can skip to the next section.

Dynamics CRM

Dynamics CRM

Parent Account field is a Lookup in Account, so you can see Lookup View here.

Dynamics CRM
Dynamics CRM

Setting Custom View with JavaScript

Now, if we want to show some other View here by default, the setDeafautView() method does our job. See the syntax below.

  1. Xrm.Page.getControl("<ControlName>").setDefaultView("<ViewGUID>");  

Now, the question is how to get View Id. Well, Advanced Find is here to the rescue.

Select Look for as "View" and condition as "Equals".

Dynamics CRM

Select your desired View from Lookup. It must belong to your Entity otherwise it won’t show any error but won’t work also, I am taking Active Accounts view Here.

Dynamics CRM

Download and Open Fetch XML

Dynamics CRM

You can find View ID here, which is {00000000-0000-0000-00AA-000010001002} for me.

  1. <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">  
  2.   <entity name="savedquery">  
  3.     <attribute name="name" />  
  4.     <attribute name="description" />  
  5.     <attribute name="savedqueryid" />  
  6.     <order attribute="name" descending="false" />  
  7.     <filter type="and">  
  8.       <condition attribute="savedqueryid" operator="eq" uiname="Active Accounts" uitype="savedquery" value="{00000000-0000-0000-00AA-000010001002}" />  
  9.     </filter>  
  10.   </entity>  
  11. </fetch>  

Let’s use this in setDefaultView() and use in Form. I am setting this to Parent Account of Account Main Form onLoad event. Add below code to JS web resource and Add to Form.

  1. function formOnLoad(){  
  2. Xrm.Page.getControl("parentaccountid").setDefaultView("{00000000-0000-0000-00AA-000010001002}");  
  3. }  

Attach the function to Form OnLoad event, and Save & Publish it.

Dynamics CRM

Now, go back to the Account Form, refresh it to Load JavaScript, and in Parent Account, click Look Up More Records.

Congrats! Your default Lookup View is "Active Accounts" now.

Dynamics CRM

Let’s make it more generic and useful by retrieving View Id from the name with WebAPI AJAX call.

I’m creating new generic function with name setLookupViewByName(). Replace your web resource content with the below-given code. Then, save and publish it.

  1. function formOnLoad() {  
  2.     setLookupViewByName("parentaccountid""Customers"true);  
  3. }  
  4.   
  5. function setLookupViewByName(fieldName, viewName, asynchronous) {  
  6.     var req = new XMLHttpRequest();  
  7.     req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/savedqueries?$select=savedqueryid&$filter=name eq '" + viewName + "'", asynchronous);  
  8.     req.setRequestHeader("OData-MaxVersion""4.0");  
  9.     req.setRequestHeader("OData-Version""4.0");  
  10.     req.setRequestHeader("Accept""application/json");  
  11.     req.setRequestHeader("Content-Type""application/json; charset=utf-8");  
  12.     req.setRequestHeader("Prefer""odata.include-annotations=\"*\"");  
  13.     req.onreadystatechange = function () {  
  14.         if (this.readyState === 4) {  
  15.             req.onreadystatechange = null;  
  16.             if (this.status === 200) {  
  17.                 var results = JSON.parse(this.response);  
  18.                 if (results.value.length > 0) {  
  19.                     var savedqueryid = results.value[0]["savedqueryid"];  
  20.                     Xrm.Page.getControl(fieldName).setDefaultView(savedqueryid);  
  21.                 } else {  
  22.                     Xrm.Utility.alertDialog(viewName + " view is not available.");  
  23.                 }  
  24.             } else {  
  25.                 Xrm.Utility.alertDialog(this.statusText);  
  26.             }  
  27.         }  
  28.     };  
  29.     req.send();  
  30. }  

Dynamics CRM

I have given Customers as View name, so it is coming as the default View.

Hope it is helpful. Cheers!

Up Next
    Ebook Download
    View all
    Learn
    View all