Using Custom Value Rule with Promise

Requirement

Sometimes, we have a requirement to hide/show command buttons in D365 CE UI based on the 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 the following options:

  1. Value Rule: This is used for simple logic, and fields should be on the entity form
  2. Custom Rule: This is used for complex logic, and we don’t need fields on the entity form.

While using complex logic for the custom rule where we don’t have the field part of the form, we need to write webapi calls to get field value, so let’s take an example: we want to hide a custom button based on the state of the entity and value of two options set field. As we need to write a webapi call to get data, we need to use Promise like below:

this.HideCreateButton = function(primaryControl) {
     var formContext = primaryControl;
     
     var Id = formContext.data.entity.getId().replace('{', '').replace('}', '');

     return new Promise(function(resolve, reject) {
         Xrm.WebApi.online.retrieveRecord("entityname", Id, "?$select=customfieldname,statecode").then(
             function success(result) {
                
                 var customfieldvalue = result["customfieldname"];
                 var statecode = result["statecode"]; 
				 
                 if (customfieldvalue && statecode == 1) 
						resolve(true);
                 else
						reject(false);
             },
             function(error) {
                 console.log(error.message);
             }
         );
     });

 }

In the above code. If both the conditions are true, we want to show our button; otherwise, we want to hide our button.

Hope it will help someone !!

Keep learning and Keep Sharing !!

HIMBAP
We are expert in Microsoft Power Platform.