{"id":1718,"date":"2015-09-04T16:07:50","date_gmt":"2015-09-04T10:37:50","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=1718"},"modified":"2015-09-04T16:07:50","modified_gmt":"2015-09-04T10:37:50","slug":"lockunlock-composite-fields-using-java-script-in-dynamics-crm","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2015\/09\/lockunlock-composite-fields-using-java-script-in-dynamics-crm\/","title":{"rendered":"Lock\/Unlock Composite fields using Java Script in Dynamics CRM"},"content":{"rendered":"<p>Traditionally we use the following way to lock or unlock the fields programmatically,<\/p>\n<p>Xrm.Page.getControl(fieldname).setDisabled(true\/false);<\/p>\n<p>But there could be some cases where we would need to lock\/unlock the composite fields programmatically.<\/p>\n<p>For example, consider the following scenario,<\/p>\n<p>Suppose that we are populating the Bill To address on Quote as the address of the customer selected on the quote and we want this address should not be changed and should not be editable. In this case we would need to lock the Bill To address fields programmatically.<\/p>\n<p>As we cannot access the composite fields directly, we would need to use some different way to lock the composite fields. The following code snippet shows how to lock the composite fields using JScript.<\/p>\n<p>\/\/\/This function will lock\/unlock the fields defined in the fields array<\/p>\n<p>\/\/\/It takes the input parameter as \u201cstate\u201d, whose value can be \u201cTrue\u201d or \u201cFalse\u201d. Depending on the value of this parameter the field will get locked or unlocked.<\/p>\n<p>function setFieldReadOnly(state){<\/p>\n<p>var fields = new Array();<\/p>\n<p>try {<\/p>\n<p>fields = [&#8220;billto_line1&#8221;, &#8220;billto_line2&#8221;, &#8220;billto_line3&#8221;, &#8220;billto_postalcode&#8221;, &#8220;billto_stateorprovince&#8221;, &#8220;billto_city&#8221;, &#8220;billto_country&#8221;];<\/p>\n<p>\/\/loop through each field of the composite field Bill To<\/p>\n<p>$.each(fields, function (index, item) {<\/p>\n<p>if (Xrm.Page.getControl(&#8220;billto_composite_compositionLinkControl_&#8221;+item)) {<\/p>\n<p>\/\/lock or unlock the fields<\/p>\n<p>Xrm.Page.getControl(&#8220;billto_composite_compositionLinkControl_&#8221;+item).setDisabled(state);<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>} catch (e) {<\/p>\n<p>Xrm.Utility.alertDialog(e);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>As you can see in the above code snippet, we have to access the composite field as composite field name with the field that belongs to the composite field.<\/p>\n<p>For example, to access the composite field \u201cbillto_line1\u201d, we have used<\/p>\n<p>Xrm.Page.getControl(&#8220;billto_composite_compositionLinkControl_billto_line1\u201d).setDisabled(true\/false);<\/p>\n<p>You can call this function on any event (OnLoad,OnSave,OnChange).<a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2015\/09\/lock.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1719\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2015\/09\/lock.png\" alt=\"lock\" width=\"630\" height=\"310\" \/><\/a><\/p>\n<p>Similarly to lock or unlock the field \u201caddress1_line1\u201d of the composite field \u201cAddress1\u201d on Account we have to use following code<\/p>\n<p>Xrm.Page.getControl(&#8220;address1_composite_compositionLinkControl_address1_line1\u201d).setDisabled(true\/false);<\/p>\n<p><strong>Conclusion<\/strong>:<\/p>\n<p>To lock\/unlock the fields of the Composite field, we have to access the field as, Xrm.Page.getControl(compositeFieldName_compositionLinkControl_fieldBelongToCompositeField).setDisabled(state);<\/p>\n<p>Get more from your Dynamics CRM..Look Beyond &#8220;CRM&#8221; with \u00a0QuickBooks Integration. Read more about Inolink <a href=\"http:\/\/bit.ly\/1KPSAnf\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.\u00a0For more details get in touch on crm@inogic.com.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Traditionally we use the following way to lock or unlock the fields programmatically, Xrm.Page.getControl(fieldname).setDisabled(true\/false); But there could be some cases where we would need to lock\/unlock the composite fields programmatically. For example, consider the following scenario, Suppose that we are populating the Bill To address on Quote as the address of the customer selected on\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2015\/09\/lockunlock-composite-fields-using-java-script-in-dynamics-crm\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[19,33],"tags":[312,592,1006],"class_list":["post-1718","post","type-post","status-publish","format-standard","hentry","category-dynamics-crm","category-javascript","tag-composite-fields","tag-dynamics-crm","tag-lockunlock-composite-fields"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/1718","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=1718"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/1718\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=1718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=1718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=1718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}