Thursday, August 8, 2019

How to call an action in Dynamic CRM in Javascript

Calling of Global Action

function CallGlobalCustomAction(){

//get the current organization name
var serverURL = Xrm.Page.context.getClientUrl();

//query to send the request to the global Action
var actionName = "new_MyCustomAction"// Global Action Unique Name - Also Case Sensitive

//set the current loggedin userid in to _inputParameter of the
var InputParameterValue = Xrm.Page.context.getUserId();

//Pass the input parameters of action
var data = {
    "MyInputParameter": InputParameterValue
};

//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/" + actionName, 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) {
            alert("Action Executed Successfully...");
            //You can get the output parameter of the action with name as given below
            result = JSON.parse(this.response);
            alert(result.MyOutputParameter);
        }
        else {
            var error = JSON.parse(this.response).error;
            alert("Error in Action: " + error.message);
        }
    }
};
//Execute request passing the input parameter of the action 
req.send(window.JSON.stringify(data));
}

Calling of Entity Specific Action

function ExecuteEntitySpecificAction(entityName, entityId, actionName) {
debugger;
var serverURL = Xrm.Page.context.getClientUrl();
var query = entityName + "(" + entityId + ")/Microsoft.Dynamics.CRM." + actionName;
var req = new XMLHttpRequest();
req.open("POST", serverURL + "/api/data/v8.2/" + query, false);
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);
            if (data != null)
                if (data.Result == "True") {
                    alert("Duplicate Quote created...");
                    var windowOptions = {
                        openInNewWindow: true
                    };
                    Xrm.Utility.openEntityForm("quote", data.DuplicateQuoteId, null, windowOptions);
                }
                else
                    alert("Something went wrong while copying Quote. Error: " + data.Result);
        } else {
            var error = JSON.parse(this.response).error;
            alert(error.message);
        }
    }
};
req.send();
}

The calling mechanism can be further simplified for calling custom actions by using Process.js and can be found here. This library simplifies calling custom actions from JavaScript.  Calling custom actions involves creating rather lengthy XML Http Requests and this library simplifies that down to a function call with a couple of parameters.  It also provides callbacks for success and errors.

No comments: