{"id":1970,"date":"2015-12-20T06:29:21","date_gmt":"2015-12-20T06:29:21","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=1970"},"modified":"2016-03-17T06:47:14","modified_gmt":"2016-03-17T06:47:14","slug":"load-autocomplete-options-from-config-entity","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=1970","title":{"rendered":"Load AutoComplete options from Config Entity"},"content":{"rendered":"<p>In our <a href=\"https:\/\/himbap.com\/blog\/?p=1952\">earlier post <\/a>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.<br \/>\nWe have setup a custom entity named <strong>Configuration<\/strong>, where we have not created any custom fields so we are just using default primary attribute field to store possible options for salutations.<\/p>\n<p><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/salutationentity11.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-1973 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/salutationentity11-274x300.png\" alt=\"salutationentity1\" width=\"274\" height=\"300\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/salutationentity11-274x300.png 274w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/salutationentity11.png 383w\" sizes=\"(max-width: 274px) 100vw, 274px\" \/><\/a><\/p>\n<p>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 following:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar odataQuery = &quot;?$select=him_name&amp;$top=10&amp;&quot; +\r\n    &quot;$filter=startswith(him_name,'&quot; + salutationtxt + &quot;')&quot;;\r\n<\/pre>\n<p><strong>Note<\/strong>: Here we are using schema name of the attributes, because in OData query we need to use schema name for the entity and attributes. You can get schema name of the entity and attributes from solution.<\/p>\n<p>In above query we are just retrieving name attribute and applied top clause to fetch only 10 records, in case there are many record for the character entered by the user. To filter results we are using <strong>startswith<\/strong> operator which will compare first character of this attribute value.<br \/>\nTo write OData query we will use <strong>SDK.Rest.js<\/strong> library that comes with SDK, so we need add this library as a web resource in CRM, please refer our <a href=\"https:\/\/himbap.com\/blog\/?p=1837\">earlier post <\/a>for how to do the same.<\/p>\n<p>We need to update our earlier <strong>AutoCompleteSalutations.js<\/strong> web resource with following code (if you have not check our earlier post, please read it <a href=\"https:\/\/himbap.com\/blog\/?p=1952\">here<\/a> first):<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction ContactOnLoad() {\r\n    Xrm.Page.getControl(&quot;salutation&quot;).addOnKeyPress(LoadAutoComplete);\r\n}\r\n\/\/query configuration entity using OData\r\nfunction LoadAutoComplete() {\r\n    var salutationtxt = Xrm.Page.getControl(&quot;salutation&quot;).getValue();\r\n    var entitySchemaName = &quot;him_configuration&quot;;\r\n    var odataQuery = &quot;?$select=him_name&amp;$top=10&amp;&quot; +\r\n        &quot;$filter=startswith(him_name,'&quot; + salutationtxt + &quot;')&quot;;\r\n    if (typeof(SDK) != &quot;undefined&quot;) {\r\n        SDK.REST.retrieveMultipleRecords(entitySchemaName, odataQuery, Callback, function(error) {\r\n            alert(error.message);\r\n        }, function() {});\r\n    } else {\r\n        alert(&quot;Not able to load REST.SDK library&quot;);\r\n    }\r\n}\r\n\/\/call back to process results\r\nfunction Callback(entityresultSet) {\r\n    if (entityresultSet.length &gt; 0) {\r\n\r\n        var salutationtxt = Xrm.Page.getControl(&quot;salutation&quot;).getValue();\r\n        resultSet = {\r\n            results: new Array(),\r\n            commands: {\r\n                id: &quot;salutationcmd&quot;,\r\n                label: &quot;Search in Google&quot;,\r\n                action: function() {\r\n                    window.open(&quot;http:\/\/google.com&quot;);\r\n                }\r\n            }\r\n        };\r\n        var salutationtxtLowerCase = salutationtxt.toLowerCase();\r\n        for (i = 0; i &lt; entityresultSet.length; i++) {\r\n            if (salutationtxtLowerCase === entityresultSet[i].him_name.substring(0, salutationtxtLowerCase.length).toLowerCase()) {\r\n                resultSet.results.push({\r\n                    id: i,\r\n                    fields: [entityresultSet[i].him_name]\r\n                });\r\n            }\r\n        }\r\n\r\n        if (resultSet.results.length &gt; 0) {\r\n            Xrm.Page.getControl(&quot;salutation&quot;).showAutoComplete(resultSet);\r\n        } else {\r\n            Xrm.Page.getControl(&quot;salutation&quot;).hideAutoComplete();\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Now we need to add our<strong> SDK.REST.js<\/strong> script web resource to contact entity form and need to call <strong>ContactOnLoad<\/strong> method on contact form <strong>OnLoad<\/strong> like following:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/salutationentity3.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-1974 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/salutationentity3-300x222.png\" alt=\"salutationentity3\" width=\"300\" height=\"222\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/salutationentity3-300x222.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/salutationentity3.png 585w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Make sure to keep <strong>SDK.Rest.js<\/strong> first in order.<br \/>\nSave 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 below:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/salutationentity2.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-1972 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/salutationentity2-300x183.png\" alt=\"salutationentity2\" width=\"300\" height=\"183\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/salutationentity2-300x183.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/salutationentity2.png 452w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our earlier post 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&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=1970\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":1972,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[275],"tags":[287,282,286],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1970"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1970"}],"version-history":[{"count":9,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1970\/revisions"}],"predecessor-version":[{"id":1983,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1970\/revisions\/1983"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/media\/1972"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}