CRM Crate

How to call / trigger Action from JavaScript?

Many times there are scenarios where you need to create Global Actions where you don’t specify an entity in particularly. When you create such global action and if you need to call that action from your JavaScript code or on Button Click. Here we will learn how to call / trigger the Action from user form using JavaScript.

Create an Action.

Lets assume you have an Action created in your CRM. If you want to learn how to create Action then click here. Once you create Action validate that it is in active state.

Build a JavaScript function which will call your Action.

Now we have our Action in place, so lets create a JavaScript to call this Action. For calling Action via JavaScript we need the CRM url, record GUID and Action GUID.

Use the below JavaScript snippet on the record form and the JavaScript triggers can the button click, form on load, form on save, field change etc.

//Function to call an action.   
var callAction = function () {
            /*Define Record GUID.*/
            var sRecordGUID = "1d856432-d6c1-ea11-a812-000d3a382c70";
            /*Define Action GUID.*/
            var accActionName = "1d856432-d6c1-ea11-a812-000d3a382c70";
            var accActionParameters = {
                "ForecastGuid": sRecordGUID 
            };
            /*This will return instance url such as 
            "https://crmcratev1.crm.dynamics.com/". */
            var serverURL = parent.Xrm.Page.context.getClientUrl();
            var ActionCallBack;
            //Calling Action Web API with HTTP Request.
            var req = new XMLHttpRequest();
            req.open("POST", serverURL + "/api/data/v8.0/" + accActionName, 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 || this.status === 204) {
                        var result = JSON.parse(this.response);
                    /*Action Is been successfully called.*/
                    /*The below object contains the returning parameter from 
                    the Action if your Action has output parameters.*/
                        ActionCallBack = result.SuccessCallBack;                   
                    }
                    else {
                        var error = JSON.parse(this.response).error;
                    }
                }
            };
            req.send(window.JSON.stringify(accActionParameters));
   };

CRM Crate

All In One Platform For Learning Microsoft CRM.

Facebook
Twitter
LinkedIn
0 0 votes
Article Rating
Subscribe
Notify of
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
How to create and register a custom action in Dynamics CRM? - CRM Crate
3 years ago

[…] How to call an action from JavaScript using HTTP request? […]

What is a custom action in Microsoft Dynamics CRM? - CRM Crate
21 days ago

[…] How to call an action from JavaScript using HTTP request? […]

error: CRM Crate Security Engine - Disabled Right Click & Selection!

Congratulations!

Well Done,
Welcome to CRM Crate

Stay tuned with us and get all latest updates and learning in Microsoft CRM and related techonologes.