Introduction:
We have already discussed how to perform entity specific custom action using Web API in one of our earlier blog. But when we want an action to be performed on multiple entities, we need to create a global action.
Create global action:-
- Go to Settings → Processes → Click New then select category as “Action” and entity as “None(global)” as shown in below screenshot.
- Create the input argument (i.e InputParameter) and output argument (i.e. OutputParameter) as per below screenshot. Save and Activate the action.
Execute Global Action using Web API:-
Execute the created global action using Web API. //get the current organization name var serverURL = Xrm.Page.context.getClientUrl(); //query to send the request to the global Action var query=”new_GlobalAction”; //set the current loggedin userid in to _inputParameter of the _InputParameter=Xrm.Page.context.getUserId(); //Pass the input parameters of action var data = { "InputParameter": _ InputParameter }; //Create the HttpRequestObject to send WEB API Request var req = new XMLHttpRequest(); //Post the WEB API Request req.open("POST", serverURL + "/api/data/v8.0/”+query+", 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) { //You can get the output parameter of the action with name as given below result = JSON.parse(this.response); Xrm.Page.getAttribute(“Fieldname”).setValue(Result.OutputParameter); } else { var error = JSON.parse(this.response).error; alert(error.message); } } }; //Execute request passing the input parameter of the action req.send(window.JSON.stringify(data)); }
Note:
To execute the global action you only need to pass Action Name “new_GlobalAction” shown in the above code
req.open(“POST”, serverURL + “/api/data/v8.0/”+query+”, true);
But to perform entity specific custom action you need to pass the EntityName and Primary id of record as
“account(” + Xrm.Page.data.entity.getId() + “)/Microsoft.Dynamics.CRM.new_EntityAction ”.
Conclusion:
Using the above code, global action can be performed. You can perform this action on multiple entities using Web API.
Are you at CRMUG Summit? Don’t Forget to stop by our booth # 742!