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 – This is used for the simple logic and fields should be on the entity form
Custom Rule – This is used for the 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 field part of the form, we need to write webapi calls to get fields value so let’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:
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 above code if both the condition is true we want to show our button otherwise we want to hide our button.
Hope it will help someone !!
Keep learning and Keep Sharing !!