{"id":41773,"date":"2025-07-16T15:29:34","date_gmt":"2025-07-16T09:59:34","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=41773"},"modified":"2025-07-16T15:29:34","modified_gmt":"2025-07-16T09:59:34","slug":"how-to-use-setisvalid-in-dynamics-365-for-effective-field-validation","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2025\/07\/how-to-use-setisvalid-in-dynamics-365-for-effective-field-validation\/","title":{"rendered":"How to Use setIsValid in Dynamics 365 for Effective Field Validation"},"content":{"rendered":"<p><img decoding=\"async\" class=\"alignnone size-full wp-image-41781\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/How-to-Use-setIsValid-in-Dynamics-365-for-Effective-Field-Validation.png\" alt=\"How to Use setIsValid in Dynamics 365 for Effective Field Validation\" width=\"1925\" height=\"1100\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/How-to-Use-setIsValid-in-Dynamics-365-for-Effective-Field-Validation.png 1925w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/How-to-Use-setIsValid-in-Dynamics-365-for-Effective-Field-Validation-300x171.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/How-to-Use-setIsValid-in-Dynamics-365-for-Effective-Field-Validation-1024x585.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/How-to-Use-setIsValid-in-Dynamics-365-for-Effective-Field-Validation-768x439.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/How-to-Use-setIsValid-in-Dynamics-365-for-Effective-Field-Validation-1536x878.png 1536w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/How-to-Use-setIsValid-in-Dynamics-365-for-Effective-Field-Validation-660x377.png 660w\" sizes=\"(max-width: 1925px) 100vw, 1925px\" \/><\/p>\n<p>In Dynamics 365, JavaScript plays a crucial role in customizing forms and enhancing the user experience. One useful function in D365 form scripting is \u201c<strong>setIsValid()<\/strong>\u201d, which allows developers to control field validation dynamically.<\/p>\n<p>In this blog post, we\u2019ll explore a real-world business use case for \u201csetIsValid()\u201d and demonstrate how it can improve data integrity and user workflows.<\/p>\n<h3><strong>Understanding setIsValid()<br \/>\n<\/strong><\/h3>\n<p>The \u201c<strong>setIsValid()<\/strong>\u201d function is used to manually set the validation state of a field in a D365 form. By default, fields are validated based on required rules, but sometimes, business logic requires conditional validation.<\/p>\n<p><strong>Syntax:\u00a0 <\/strong>formContext.getAttribute(&#8220;fieldname&#8221;).setIsValid(bool, message);<\/p>\n<p>bool (Boolean): Set this to true if the field passes validation, or false if it fails.<\/p>\n<p>message (String, optional): An error message displayed if validation fails.<\/p>\n<h3>Business Use Case: Conditional Field Validation in Opportunity Form<\/h3>\n<h4><strong>Scenario:\u00a0 <\/strong><\/h4>\n<p>A sales organization wants to ensure that the &#8220;Estimated Revenue&#8221; field in an Opportunity record is mandatory only if the &#8220;Probability&#8221; field is greater than 50%. Otherwise, the field should remain optional.<\/p>\n<h3><strong>Step by Step Implementation:\u00a0 <\/strong><\/h3>\n<p><strong>1. Add JavaScript to the Opportunity Form <\/strong><\/p>\n<p>Create a JavaScript web resource and add it to the Opportunity form\u2019s `OnChange` event for the Probability field.<\/p>\n<p><strong>2. JavaScript Code: <\/strong><\/p>\n<pre class=\"lang:css gutter:true start:1\">function validateEstimatedRevenue(executionContext) {\r\nvar formContext = executionContext.getFormContext();\r\n\r\nvar probability = formContext.getAttribute(\"closeprobability\").getValue();\r\n\r\nvar estimatedRevenue = formContext.getAttribute(\"estimatedvalue\");\r\n\r\nif (probability &gt; 50) {\r\n\r\n\/\/ If Probability &gt; 50%, ensure Estimated Revenue is filled\r\n\r\nif (!estimatedRevenue.getValue()) {\r\n\r\nestimatedRevenue.setIsValid(false, \"Estimated Revenue is required when Probability &gt; 50%.\");\r\n\r\n} else {\r\n\r\nestimatedRevenue.setIsValid(true);\r\n\r\n}\r\n\r\n} else {\r\n\r\n\/\/ If Probability &lt;= 50%, field is optional\r\n\r\nestimatedRevenue.setIsValid(true);\r\n\r\n}\r\n\r\n}<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-41774\" style=\"border: 1px solid #000000; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/1Dynamics-365-for-Effective-Field-Validation.png\" alt=\"Dynamics 365 for Effective Field Validation\" width=\"2048\" height=\"933\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/1Dynamics-365-for-Effective-Field-Validation.png 2048w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/1Dynamics-365-for-Effective-Field-Validation-300x137.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/1Dynamics-365-for-Effective-Field-Validation-1024x467.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/1Dynamics-365-for-Effective-Field-Validation-768x350.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/1Dynamics-365-for-Effective-Field-Validation-1536x700.png 1536w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/1Dynamics-365-for-Effective-Field-Validation-660x301.png 660w\" sizes=\"(max-width: 2048px) 100vw, 2048px\" \/><\/p>\n<p><strong>3. Add the Function to the OnChange Event<\/strong><\/p>\n<p>Open the <strong>Opportunity <\/strong>form in the D365 Customizer.<\/p>\n<p>Select the <strong>Probability <\/strong>field.<\/p>\n<p>In the Event Handlers, add the `<strong>validateEstimatedRevenue<\/strong>` function to the `<strong>OnChange<\/strong>` event.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-41775\" style=\"border: 1px solid #000000; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/2Dynamics-365-for-Effective-Field-Validation.png\" alt=\"Dynamics 365 for Effective Field Validation\" width=\"2048\" height=\"927\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/2Dynamics-365-for-Effective-Field-Validation.png 2048w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/2Dynamics-365-for-Effective-Field-Validation-300x136.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/2Dynamics-365-for-Effective-Field-Validation-1024x464.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/2Dynamics-365-for-Effective-Field-Validation-768x348.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/2Dynamics-365-for-Effective-Field-Validation-1536x695.png 1536w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/2Dynamics-365-for-Effective-Field-Validation-660x299.png 660w\" sizes=\"(max-width: 2048px) 100vw, 2048px\" \/><\/p>\n<p>Now that we\u2019ve implemented our custom validation logic, let\u2019s verify how Dynamics 365 renders the error message when setIsValid(false, message) is triggered.<\/p>\n<p>Tapping on the error message will automatically navigate to the corresponding field and switch it to write mode.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-41776\" style=\"border: 1px solid #000000; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/3Dynamics-365-for-Effective-Field-Validation.png\" alt=\"Dynamics 365 for Effective Field Validation\" width=\"2048\" height=\"932\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/3Dynamics-365-for-Effective-Field-Validation.png 2048w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/3Dynamics-365-for-Effective-Field-Validation-300x137.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/3Dynamics-365-for-Effective-Field-Validation-1024x466.png 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/3Dynamics-365-for-Effective-Field-Validation-768x350.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/3Dynamics-365-for-Effective-Field-Validation-1536x699.png 1536w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2025\/07\/3Dynamics-365-for-Effective-Field-Validation-660x300.png 660w\" sizes=\"(max-width: 2048px) 100vw, 2048px\" \/><\/p>\n<h3><strong>Benefits of Using setIsValid()\u00a0 <\/strong><\/h3>\n<p>Dynamic Validation: Ensures fields are validated based on business logic rather than static rules.<\/p>\n<p>Better User Experience: Prevents unnecessary errors when fields are conditionally required.<\/p>\n<p>Improved Data Quality: Enforces mandatory fields only when necessary, reducing incomplete submissions.<\/p>\n<p>The setIsValid() function in D365 provides a powerful way to implement conditional field validation, ensuring data consistency while maintaining a smooth user experience. By applying this in scenarios like Opportunity management, businesses can enforce rules dynamically without frustrating users with rigid validation requirements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Dynamics 365, JavaScript plays a crucial role in customizing forms and enhancing the user experience. One useful function in D365 form scripting is \u201csetIsValid()\u201d, which allows developers to control field validation dynamically. In this blog post, we\u2019ll explore a real-world business use case for \u201csetIsValid()\u201d and demonstrate how it can improve data integrity and\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2025\/07\/how-to-use-setisvalid-in-dynamics-365-for-effective-field-validation\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":15,"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":[16,2361],"tags":[3186],"class_list":["post-41773","post","type-post","status-publish","format-standard","hentry","category-dynamics-365","category-technical","tag-dynamics-365-for-effective-field-validation"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/41773","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\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=41773"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/41773\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=41773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=41773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=41773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}