We have seen including myself this request quite a lot from early days on CRM 2011 ‘Set Status, Statusreason by clicking the ribbon button’. I started to love the new way of getting this done so quickly in latest UCI by just calling the Xrm.WebApi.updateRecord function to update the record. All we need before start is JavaScript Webresource, Ribbon button on the entity form. See below scenario to click a custom button on Account form to change the status and status reason and refresh the form.
function ClickCustomButton(primaryControl) {
var formContext = primaryControl;
setStatusStatusReason(formContext, “account”, formContext.data.entity.getId(), 1, 614020000); // these need to be changed as per your context
}function setStatusStatusReason(executionContext, entityLogicalName, recordId, statecode, statuscode) {
var id = recordId.replace(“{“, “”).replace(“}”, “”);
var data = { “statecode”: statecode, “statuscode”: statuscode };Xrm.WebApi.updateRecord(entityLogicalName, id, data).then(
function success(result) { executionContext.data.refresh(true); },
function (error) { executionContext.data.refresh(true); }
);
}
2. Access Scott’s tool Ribbonworkbench plugin from Xrmtoolbox and access Account entity from the solution you intend to apply these changes.
Publish! that should do the magic, no more long code as we done in olden days!
To test go to the account form and click the custom button from step 2, upon click it should change the status and status reason of account.
Note: If you don’t know how to add ribbon button on ribbon workbench click this link before jumping to this blog. If you face any status reason transitions error, then go to the status reason field customizations and edit the ‘Status Reason Transitions’ or disable it.
Original Post https://mscrmsama.wordpress.com/2020/03/25/d365-set-status-and-statusreason-from-custom-ribbon-button-using-javascript-on-uci/