{"id":1940,"date":"2015-12-17T05:34:40","date_gmt":"2015-12-17T05:34:40","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=1940"},"modified":"2015-12-17T09:52:48","modified_gmt":"2015-12-17T09:52:48","slug":"using-new-scripting-method-in-crm-2016-part-1","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=1940","title":{"rendered":"Using New Scripting Methods in CRM 2016 Part 1"},"content":{"rendered":"<p>With the release of Dynamics CRM 2016, new client side methods introduced that we can use to make user experience more interactive while enter data in CRM forms. Following are the methods:<br \/>\n\u2022 <strong>getValue<\/strong><br \/>\n\u2022 <strong>Keypress<\/strong><br \/>\n\u2022 <strong>Autocomplete<\/strong><\/p>\n<p><strong>getValue Method <\/strong>\u2013 This method is used to get the latest value of the field. Earlier this method was only available in attribute collection, which provides value of the field only after <strong>Onchange <\/strong>event fired. Whereas when it is used with control collection, we can capture value as soon as users starts entering value in field, so it means if we have any requirement to validate data entry then we can use this method to make it more interactive. We can use this method like below:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nXrm.Page.getControl(field name).getValue();\r\n<\/pre>\n<p>Let\u2019s take one example that we want to restrict user to enter only number between 0-9 under phone field in account entity, so we can simply use below code to do that:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction OnTelephone1KeyPress() {\r\n    var phoneValue = Xrm.Page.getControl(&quot;telephone1&quot;).getValue().toString().replace(\/[^0-9]\/g, &quot;&quot;);\r\n    Xrm.Page.getAttribute(&quot;telephone1&quot;).setValue(phoneValue);\r\n}\r\n<\/pre>\n<p>Above code will get character from telephone1 field as soon as user enters it and will replace character with null if it is not under 0 to 9 range. But we need to use above code with keypress events, so let\u2019s understand use of keypress event first to complete our code.<\/p>\n<p><strong>Keypress Method <\/strong>\u2013 Below three keypress methods are added to work with controls:<br \/>\n\u2022 <strong>addOnKeyPress<\/strong> \u2013 This method is used to associate method to KeyPress event of text or number, so it will fire when user will press any key. We can use this method like below:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nXrm.Page.getControl(field name).addOnKeyPress(name of function);\r\n<\/pre>\n<p>For example if we want to call above method on keypress event of telephone1 field then we can use it like below:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nXrm.Page.getControl(&quot;telephone1&quot;).addOnKeyPress(OnTelephone1KeyPress);\r\n<\/pre>\n<p>Now let&#8217;s deploy this code to CRM, we can create a java script web resource and use below code under text editor:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/validate character\r\nfunction OnTelephone1KeyPress() {\r\n    var phoneValue = Xrm.Page.getControl(&quot;telephone1&quot;).getValue().toString().replace(\/[^0-9]\/g, &quot;&quot;);\r\n    Xrm.Page.getAttribute(&quot;telephone1&quot;).setValue(phoneValue);\r\n}\r\n\/\/add keypress event handler\r\nfunction AccountOnLoad() {\r\n    Xrm.Page.getControl(&quot;telephone1&quot;).addOnKeyPress(OnTelephone1KeyPress);\r\n}\r\n<\/pre>\n<p><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/clientevent1.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-1941 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/clientevent1-300x124.png\" alt=\"clientevent1\" width=\"300\" height=\"124\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/clientevent1-300x124.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/clientevent1.png 611w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>After that we need to attach this web resource to account entity form and need to call AccountOnLoad method OnLoad of account form like below and need save and publish our changes:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/clientevent2.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-1942 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/clientevent2-300x205.png\" alt=\"clientevent2\" width=\"300\" height=\"205\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/clientevent2-300x205.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/12\/clientevent2.png 598w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nNow when we will create a new account record or modify existing account record, we will see our code won\u2019t allow us to enter non numeric character under phone field.<\/p>\n<p>\u2022 <strong>removeOnKeyPress<\/strong> \u2013 We can use this method to remove KeyPress event handler from the text or number field, that is added using addOnKeyPress method. We can use it like below:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nXrm.Page.getControl(arg).removeOnKeyPress(function name)\r\n<\/pre>\n<p>\u2022 <strong>fireOnKeyPress <\/strong>\u2013 We can use this method to call KeyPress event handler for text or number field, we can use it like below:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nXrm.Page.getControl(field name).fireOnKeyPress()\r\n<\/pre>\n<p><strong>Note<\/strong>: All the above methods are only supported for Web and Outlook client.<\/p>\n<p>We will discuss AutoComplete methods in next post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With the release of Dynamics CRM 2016, new client side methods introduced that we can use to make user experience more interactive while enter data in CRM forms. Following are the methods: \u2022 getValue \u2022 Keypress \u2022 Autocomplete getValue Method \u2013 This method is used to get the latest value of the field. Earlier this method was only available in&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=1940\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[275],"tags":[277,276,279,280,278],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1940"}],"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=1940"}],"version-history":[{"count":9,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1940\/revisions"}],"predecessor-version":[{"id":1953,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1940\/revisions\/1953"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}