Dynamics 365 CRM Global Action Call

In the previous blog post, we discussed about calling an Entity Specific Custom Action using JavaScript with the help of Xrm.WebApi introduced in Dynamics 365 CRM v9.0.

In this blog, we are going to see how to call Global Custom Action using JavaScript in Dynamics 365 CRM.

Below is the code for calling a custom Action having 2 string Parameters, sFirstParam and sSecondParam. Call the myFunction from your trigger point via JavaScript to call your required Process Action.

function myFunction(PrimaryControl)
    var objFormContext = PrimaryControl;
    var sFirstParamValue = null;
    var sSecondParamValue = null;
    try {
        sFirstParamValue = "My Value 1";
        sSecondParamValue = "MyValue 2";
        CallGlobalAction(sFirstParamValue, sSecondParamValue);
    }
    catch (ex) {
        Xrm.Navigation.openErrorDialog({ message: ex.message, details: ex.message });
    }
}

function CallGlobalAction(sFirstParamValue, sSecondParamValue) {
    var objActionCallRequest = null;
    var objMetadata = null;
    var sActionName = "rk_GlobalAction"; // Global Action Name
    try {
        objActionCallRequest = {
            sFirstParam: sFirstParamValue,
            sSecondParam: sSecondParamValue,
            getMetadata: function () {
                objMetadata = {
                    boundParameter: null, // Null for Global Action
                    parameterTypes: {
                        "sFirstParam": {
                            "typeName": "Edm.String",
                            "structuralProperty": 1 // For PrimitiveType
                        },
                        "sSecondParam": {
                            "typeName": "Edm.String",
                            "structuralProperty": 1 // For PrimitiveType
                        }
                    },
                    operationName: sActionName,
                    operationType: 0    // 0 is for Calling Action using Xrm.WebApi.execute
                };
                return objMetadata;
            }
        };
        Xrm.WebApi.online.execute(objActionCallRequest).then(
            function success(objResult) {
                console.log("Action call success :: " + sActionName);
                alert("Success : Action is called");
            },
            function (ex) {
                Xrm.Navigation.openErrorDialog({ message: "Error :: " + ex.message, details: ex.message });
            }
        );
    }
    catch (ex) {
        Xrm.Navigation.openErrorDialog({ message: ex.message, details: ex.message });
    }
}

Happy CRM-ing! Ciao.