{"id":4679,"date":"2025-01-31T12:54:42","date_gmt":"2025-01-31T12:54:42","guid":{"rendered":"https:\/\/himbap.com\/blog\/?p=4679"},"modified":"2025-01-31T12:54:42","modified_gmt":"2025-01-31T12:54:42","slug":"using-custom-value-rule-with-promise","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=4679","title":{"rendered":"Using Custom Value Rule with Promise"},"content":{"rendered":"\n<p><strong>Requirement<\/strong><\/p>\n\n\n\n<p>Sometimes we have requirement to hide\/show command buttons in D365 CE UI based on a values of the fields and those fields are not part of the Entity form.<\/p>\n\n\n\n<p><strong>Details<\/strong><\/p>\n\n\n\n<p>When we want to hide\/show fields based on the entity field, we have to following options:<\/p>\n\n\n\n<p><strong>Value Rule<\/strong>  &#8211; This is used for the simple logic and fields should be on the entity form<\/p>\n\n\n\n<p><strong>Custom Rule<\/strong> &#8211; This is used for the complex logic and we don&#8217;t need fields on the entity form.<\/p>\n\n\n\n<p>While using complex logic for the custom rule where we don&#8217;t have field part of the form, we need to write webapi calls to get fields value so let&#8217;s take and example we want to hide a custom button based on the state of the entity and value of a two option set field. As we need to write webapi call to get data we need to use Promise like below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> this.HideCreateButton = function(primaryControl) {\r\n     var formContext = primaryControl;\r\n     \r\n     var Id = formContext.data.entity.getId().replace('{', '').replace('}', '');\r\n\r\n     return new Promise(function(resolve, reject) {\r\n         Xrm.WebApi.online.retrieveRecord(\"entityname\", Id, \"?$select=customfieldname,statecode\").then(\r\n             function success(result) {\r\n                \r\n                 var customfieldvalue = result&#91;\"customfieldname\"];\r\n                 var statecode = result&#91;\"statecode\"]; \r\n\t\t\t\t \r\n                 if (customfieldvalue &amp;&amp; statecode == 1) \r\n\t\t\t\t\t\tresolve(true);\r\n                 else\r\n\t\t\t\t\t\treject(false);\r\n             },\r\n             function(error) {\r\n                 console.log(error.message);\r\n             }\r\n         );\r\n     });\r\n\r\n }<\/code><\/pre>\n\n\n\n<p>In above code if both the condition is true we want to show our button otherwise we want to hide our button.<\/p>\n\n\n\n<p>Hope it will help someone !!<br><strong>Keep learning and Keep Sharing !!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Requirement Sometimes we have requirement to hide\/show command buttons in D365 CE UI based on a values of the fields and those fields are not part of the Entity form. Details When we want to hide\/show fields based on the entity field, we have to following options: Value Rule &#8211; This is used for the simple logic and fields should&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=4679\">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":[21],"tags":[1116,1115,1114],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4679"}],"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=4679"}],"version-history":[{"count":1,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4679\/revisions"}],"predecessor-version":[{"id":4680,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4679\/revisions\/4680"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}