Just sharing a sample code to call an Action through Web API.
Suppose below is our Action with one input parameter.
JavaScript code :-
function CallAction() { // get the id of the record and remove the curly bracket part // id will be used in Web API url var Id = Xrm.Page.data.entity.getId().replace('{', '').replace('}', ''); var serverURL = Xrm.Page.context.getClientUrl(); // pass the id as inpurt parameter var data = { "recordid": 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", serverURL + "/api/data/v8.2/sab_costmanagements(" + Id + ")/Microsoft.Dynamics.CRM.sab_Recalculate", 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)); }
Basically we need to pass name of the Entity Set with id of the record followed by name of the action appended with Microsoft.Dynamics.CRM.
In case of global action, we just need the Microsoft.Dynamics.CRM.<<ActionName>>.
More details here
https://www.inogic.com/blog/2016/10/execute-the-global-action-using-web-api-in-dynamics-crm/
Hope it helps..
Hi Nishant,
I want to call CRM API from outside portal. I want to call it from Javascript from ADX portal.
LikeLike
The success response status doesn’t necessarily have to be 200. It can be 204 (Success with no content returned in response body) or 304 (Success with Not Modified) as well.
LikeLike
My custom action targets the contact entity (not a global action) and does not have input parameters. I had to do the following:
var data = {
//”recordid”: id
“entity”: “contact”
};
LikeLike