Sharing simple scenario to call custom action from JavaScript (from ribbon button).
First of all, create an Action for Sales Order entity and a step to change status to Active(New) as shown below.
Call below function form ribbon button to change the status of Sales Order to New.
function ChangeAgreementStatus() {
var Id = Xrm.Page.data.entity.getId().replace(‘{‘, ”).replace(‘}’, ”);
var clientURL = Xrm.Page.context.getClientUrl();
// pass the id as inpurt parameter
var data = {
“agreementid”: Id
};
var req = new XMLHttpRequest();
// specify name of the entity, record id and name of the action in the Wen API Url
req.open(“POST”, clientURL + “/api/data/v8.2/salesorders(” + Id + “)/Microsoft.Dynamics.CRM.new_ActivateAgreement”, true);
req.setRequestHeader(“Accept”, “application/json”);
req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
req.setRequestHeader(“OData-MaxVersion”, “4.0”);
req.setRequestHeader(“OData-Version”, “4.0”);
req.onreadystatechange = function () {
if (this.readyState == 4 /* complete */) {
req.onreadystatechange = null;
if (this.status == 200) {
var data = JSON.parse(this.response);
alert(data);
} else {
var error = JSON.parse(this.response).error;
alert(error.message);
}
}
};
// send the request with the data for the input parameter
req.send(window.JSON.stringify(data));
//Refresh form
Xrm.Page.data.refresh();
}
For details, please refer below URL.
hope this help you
You must be logged in to post a comment.