D365 Set status and statusreason from custom ribbon button using JavaScript on UCI

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.

  1. Javascript webresouce with a function as written below

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.

  • Add Ribbon button on account form if you don’t have one
  • Add Command definition with JavaScript Action
  • Select the webresource and function name ‘ClickCustomButton’ from step 1
  • Set Crm parameter -> PrimaryControl

 

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.

 

One thought on “D365 Set status and statusreason from custom ribbon button using JavaScript on UCI

  1. Pingback: D365 Set status and statusreason from custom ribbon button using JavaScript on UCI - 365 Community

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: