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.

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
0 0 vote
Article Rating
Subscribe
Notify of
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
How to create and register a custom action in Dynamics CRM? - CRM Crate
6 months ago

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

error: CRM Crate Security Engine - Right Click Is Disabled!
1
0
Would love your thoughts, please comment.x
()
x

Congratulations!

Well Done,
Welcome to CRM Crate

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

Join CRM Crate Now!

Subscribe our newsletter for free and get all latest updates in Microsoft CRM & related technologies.

Learn & Grow Together