{"id":37843,"date":"2025-02-15T14:18:44","date_gmt":"2025-02-15T08:48:44","guid":{"rendered":"https:\/\/techmasala.addastudents.com\/dev\/?p=37843"},"modified":"2025-02-15T14:18:46","modified_gmt":"2025-02-15T08:48:46","slug":"how-to-disable-multiple-occurrences-of-the-same-field-on-a-dynamics-365-crm-form-using-javascript","status":"publish","type":"post","link":"https:\/\/techmasala.addastudents.com\/dev\/how-to-disable-multiple-occurrences-of-the-same-field-on-a-dynamics-365-crm-form-using-javascript\/","title":{"rendered":"How to Disable Multiple Occurrences of the Same Field on a Dynamics 365 CRM Form Using JavaScript"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In <strong>Microsoft Dynamics 365 CRM<\/strong>, a field can appear multiple times on a form, in different <strong>tabs, sections, or even within the same tab<\/strong>. However, when trying to manipulate such fields via JavaScript, you might notice that only <strong>one instance<\/strong> is affected while the others remain unchanged.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This happens because <strong>each occurrence of a field on a form is treated as a separate control<\/strong>, even though they all point to the same <strong>schema name<\/strong> in the database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we will explore:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Why this happens<\/strong><\/li>\n\n\n\n<li><strong>How CRM assigns control names<\/strong><\/li>\n\n\n\n<li><strong>How to handle this scenario using JavaScript<\/strong><\/li>\n\n\n\n<li><strong>An example to disable all instances of a field<\/strong><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><br><strong>Understanding the Issue<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s assume we have a custom field called <strong>&#8220;Customer Type&#8221;<\/strong> with the schema name <strong><code>new_customfield<\/code><\/strong>. This field appears in two different sections of the same form:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>General Information (Section 1)<\/strong>\n<ul class=\"wp-block-list\">\n<li>Control Name: <code>new_customfield<\/code><br><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Additional Information (Section 2)<\/strong>\n<ul class=\"wp-block-list\">\n<li>Control Name: <code>new_customfield_1<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Even though both controls map to the same schema field (<code>new_customfield<\/code>), CRM treats them as separate <strong>UI elements<\/strong> with different <strong>control names<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Common Issue in JavaScript<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If we try to disable the field using:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>formContext.getControl(\"new_customfield\").setDisabled(true);\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will only disable the <strong>first occurrence<\/strong> of the field, leaving the second instance (<code>new_customfield_1<\/code>) still editable.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Disabling All Instances of a Field<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To ensure that <strong>all occurrences<\/strong> of the field are disabled, we need to loop through <strong>all controls<\/strong> linked to that field.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>JavaScript Solution<\/strong>:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>function disableAllInstancesOfField(executionContext) {\n    var formContext = executionContext.getFormContext();\n    var fieldName = \"new_customfield\";  \/\/ Schema name of the field\n\n    \/\/ Retrieve all controls associated with this field\n    var controls = formContext.getAttribute(fieldName).controls;\n\n    \/\/ Loop through each control and disable it\n    controls.forEach(function (control) {\n        control.setDisabled(true);\n    });\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How This Works<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>formContext.getAttribute(fieldName).controls<\/code><\/strong>\n<ul class=\"wp-block-list\">\n<li>This retrieves <strong>all control instances<\/strong> of the field.<br><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Looping through controls<\/strong>\n<ul class=\"wp-block-list\">\n<li>Each occurrence of the field is disabled individually.<br><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Ensures consistency<\/strong>\n<ul class=\"wp-block-list\">\n<li>Regardless of how many times the field appears on the form, <strong>all instances will be disabled<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><br><strong>How to Implement This in CRM<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create a JavaScript Web Resource<\/strong>\n<ul class=\"wp-block-list\">\n<li>Navigate to <strong>Power Apps \u2192 Solutions<\/strong>.<\/li>\n\n\n\n<li>Open your solution and go to <strong>Web Resources<\/strong>.<\/li>\n\n\n\n<li>Click <strong>New<\/strong> and choose <strong>Script (JScript)<\/strong>.<\/li>\n\n\n\n<li>Upload the JavaScript file or add the function in a new script.<br><br><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Add JavaScript to Form Events<\/strong>\n<ul class=\"wp-block-list\">\n<li>Open the <strong>Form Editor<\/strong> for the entity.<\/li>\n\n\n\n<li>Click <strong>Form Properties<\/strong>.<\/li>\n\n\n\n<li>Add the <strong>JavaScript Web Resource<\/strong>.<\/li>\n\n\n\n<li>Attach the <code>disableAllInstancesOfField<\/code> function to the <strong>OnLoad<\/strong> event.<\/li>\n\n\n\n<li>Click <strong>Save and Publish<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><br><strong>Targeting a Specific Instance<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you only want to disable <strong>one<\/strong> instance of the field (e.g., only in Section 2), use the <strong>control name<\/strong> directly:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function disableSpecificFieldInstance(executionContext) {\n    var formContext = executionContext.getFormContext();\n\n    \/\/ Disable only the control in \"Additional Information\" section\n    formContext.getControl(\"new_customfield_1\").setDisabled(true);\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To find the <strong>control name<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open the <strong>Form Editor<\/strong> in CRM.<\/li>\n\n\n\n<li>Double-click the field where it&#8217;s placed.<\/li>\n\n\n\n<li>Check the <strong>Control Name<\/strong> in the <strong>Details<\/strong> tab.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><br><strong>Key Takeaways<\/strong>:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u2714 <strong>Each instance of a field on the form is a separate control.<\/strong><br>\u2714 <strong>CRM assigns unique control names dynamically (e.g., <code>new_customfield_1<\/code>, <code>new_customfield_2<\/code>).<\/strong><br>\u2714 <strong>Using <code>getControl()<\/code> affects only one instance, not all.<\/strong><br>\u2714 <strong>To modify all occurrences, use <code>getAttribute(fieldName).controls<\/code> and loop through them.<\/strong><br>\u2714 <strong>You can target a specific instance using <code>getControl(controlName)<\/code>.<\/strong><br><br><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By following this approach, you can <strong>ensure consistent field behavior<\/strong> across the form, preventing issues where only some instances are modified while others remain unchanged.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I hope this article helps! Let me know if you need further clarifications or enhancements. \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Microsoft Dynamics 365 CRM, a field can appear multiple times on a form, in different tabs, sections, or even within the same tab. However, when trying to manipulate such fields via JavaScript, you might notice that only one instance is affected while the others remain unchanged. This happens because each occurrence of a field [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":37846,"comment_status":"open","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"footnotes":""},"categories":[71,70,17,72],"tags":[891,894,47,45,98],"class_list":["post-37843","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-learn-javascript-online","category-learn-technology-free","category-learn-microsoft-dynamics-crm","category-tech","tag-dynamics-365-crm","tag-form","tag-javascript","tag-js","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/posts\/37843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/comments?post=37843"}],"version-history":[{"count":2,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/posts\/37843\/revisions"}],"predecessor-version":[{"id":37845,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/posts\/37843\/revisions\/37845"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/media\/37846"}],"wp:attachment":[{"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/media?parent=37843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/categories?post=37843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/tags?post=37843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}