Load AutoComplete Options From Config Entity

In earlier article we discussed about new scripting methods introduced in CRM 2016 to implement AutoComplete feature. In last example we used hard coded option to show over autocomplete list, but in this post we are going to demonstrate how we can bring these options from a custom configuration entity to populate under autocomplete list.

We have setup a custom entity named Configuration, where we have not created any custom fields so we are just using default primary attribute field to store possible options for salutations.



Using this custom entity CRM admin can create/delete required salutations easily, so it is easy to configure list options without any coding changes. We will write OData query to get list of our configuration entity records and will utilize here retrievemultiple method of CRM organization service to get data based on the character enter by the user, so our query filter will be like the following,

  1. var odataQuery = "?$select=him_name&$top=10&" +  
  2.     "$filter=startswith(him_name,'" + salutationtxt + "')";  

Note: Here we are using schema name of the attributes, because in OData query we need to use schema name of the entity and attributes. You can get schema name of the entity and attributes from solution.

In above query we are just retrieving name attribute and applied top clause to fetch only 10 records, in case there are many record based on the character entered by the user. To filter results we are using startswith operator which will compare first character of this attribute value.

To write OData query we will use SDK.Rest.js library that comes with SDK, so we need add this library as a web resource in CRM, please refer our earlier article for how to do the same.

We need to update our earlier AutoCompleteSalutations.js web resource with the following code (if you have not check our earlier article, please read it here first): 

  1. function ContactOnLoad() {  
  2.     Xrm.Page.getControl("salutation").addOnKeyPress(LoadAutoComplete);  
  3. }  
  4. //query configuration entity using OData  
  5. function LoadAutoComplete() {  
  6.     var salutationtxt = Xrm.Page.getControl("salutation").getValue();  
  7.     var entitySchemaName = "him_configuration";  
  8.     var odataQuery = "?$select=him_name&$top=10&" +  
  9.         "$filter=startswith(him_name,'" + salutationtxt + "')";  
  10.     if (typeof(SDK) != "undefined") {  
  11.         SDK.REST.retrieveMultipleRecords(entitySchemaName, odataQuery, Callback, function(error) {  
  12.             alert(error.message);  
  13.         }, function() {});  
  14.     } else {  
  15.         alert("Not able to load REST.SDK library");  
  16.     }  
  17. }  
  18. //call back to process results  
  19. function Callback(entityresultSet) {  
  20.     if (entityresultSet.length > 0) {  
  21.    
  22.         var salutationtxt = Xrm.Page.getControl("salutation").getValue();  
  23.         resultSet = {  
  24.             results: new Array(),  
  25.             commands: {  
  26.                 id: "salutationcmd",  
  27.                 label: "Search in Google",  
  28.                 action: function() {  
  29.                     window.open("http://google.com");  
  30.                 }  
  31.             }  
  32.         };  
  33.         var salutationtxtLowerCase = salutationtxt.toLowerCase();  
  34.         for (i = 0; i < entityresultSet.length; i++) {  
  35.             if (salutationtxtLowerCase === entityresultSet[i].him_name.substring(0, salutationtxtLowerCase.length).toLowerCase()) {  
  36.                 resultSet.results.push({  
  37.                     id: i,  
  38.                     fields: [entityresultSet[i].him_name]  
  39.                 });  
  40.             }  
  41.         }  
  42.    
  43.         if (resultSet.results.length > 0) {  
  44.             Xrm.Page.getControl("salutation").showAutoComplete(resultSet);  
  45.         } else {  
  46.             Xrm.Page.getControl("salutation").hideAutoComplete();  
  47.         }  
  48.     }  
  49. }  

Now we need to add our SDK script web resource to contact entity form and need to call ContactOnLoad method on contact form OnLoad like the following,



Make sure to keep SDK.Rest.js first in order.

Save and publish your changes and now try to modify salutation field for existing or new contact record, it will load list from configuration entity like the following,