Using New Scripting Methods in CRM 2016: Part 2

In our earlier article, we discussed getValue and KeyPress methods introduced in CRM 2016. In this post we are going to discuss AutoComplete methods. The following are the two methods added to implement auto complete feature to text fields:
  • ShowAutoComplete
  • HideAutoComplete

ShowAutoComplete

This method allows us to show list of possible values as dropdown to add auto complete feature to text fields just like auto complete feature in traditional web pages. As soon when we select any item under the list, it will be populated in target text field and OnChange event of that field will fire.

We can use this method like the following,

  1. Xrm.Page.getControl(field name).showAutoComplete(object);  

This method takes object as a parameter which include results and commands. We can define object like the following,

  1. var resultset = 
  2. {   
  3.    results: [
  4.    {   
  5.          id: <value1>,   
  6.          icon: <url>,   
  7.          fields: [<fieldValue1>]}],   
  8.          commands:
  9.    {   
  10.          id: <value>,   
  11.          icon: <url>,   
  12.          label: <value>,   
  13.          action: <function reference>   
  14.    }   
  15. }  

Here result represents array of possible values that we want to show under drop down on keypress. Under the results, we can define id, for individual array item, icon to show particular icon for array items and field where we pass values.

Commands is used to define additional action on the drop down, for example opening any additional page by providing a link.

HideAutoComplete

This method is used to hide the auto complete drop down list that is implemented using showAutoComplete method. We can use it like the following,

  1. Xrm.Page.getControl(field name).hideAutoComplete()  

Note:

Both of above methods only works with Web and Outlook client. 
Let’s take an example we want to implement autocomplete feature for salutation field in contact entity and want to provide the following possible options for it,

{ ‘Mr.’,’Miss’,’Ms’, ‘Dr.’,’Prof.’,’Mrs.’}

We can use the following code to implement our requirement,

  1. function SalutationAutoComplete() {  
  2.   //defind possible salutation  
  3.   salutations = [  
  4.   { name: 'Mr.'},  
  5.   { name: 'Miss'},  
  6.   { name: 'Ms'},  
  7.   { name: 'Dr.'},  
  8.   { name: 'Prof.'},  
  9.   { name: 'Mrs.'}  
  10.    ];  
  11. var OnSalutationkeyPress = function(fld) 
  12. {  
  13.         var salutationtxt = Xrm.Page.getControl("salutation").getValue();  
  14.         resultSet = 
  15.             {  
  16.             results: new Array(),  
  17.             commands: 
  18.                 {  
  19.                 id: "salutationcmd",  
  20.                 label: "Search in Google",  
  21.                 action: function() {  
  22.                     window.open("http://google.com"); //dummy url demo  
  23.                 }  
  24.             }  
  25.         };  
  26.         var salutationtxtLowerCase = salutationtxt.toLowerCase();  
  27.         for (i = 0; i < salutations.length; i++) 
  28.          {  
  29.             if (salutationtxtLowerCase === salutations[i].name.substring(0, salutationtxtLowerCase.length).toLowerCase()) 
  30.                {  
  31.                 resultSet.results.push(
  32.                 {  
  33.                     id: i,  
  34.                     fields: [salutations[i].name]  
  35.                 });  
  36.             }  
  37.             if (resultSet.results.length >= 10) break;  
  38.         }  
  39.    
  40.         if (resultSet.results.length > 0) {  
  41.             fld.getEventSource().showAutoComplete(resultSet);  
  42.         } else {  
  43.             fld.getEventSource().hideAutoComplete();  
  44.         }  
  45.     };  
  46.    
  47.     Xrm.Page.getControl("salutation").addOnKeyPress(OnSalutationkeyPress);  
  48. }  

To use above code we can create a JavaScript web resource and paste the above code under text editor,



After that we need to add our web resource to contact entity form and need to call SalutationAutoComplete on contact form OnLoad like the following,


 

Now after publishing our changes we can test our code by creating or modifying any existing record. As soon as we will type first character, we will see auto complete dropdown like the following,

 

Stay tuned for more Dynamics CRM Updates.

Up Next
    Ebook Download
    View all
    Learn
    View all