Dynamics 365 CRM Entity Specific Action Call

Photo by KOBU Agency on Unsplash

Dynamics 365 CRM Entity Specific Action Call

Custom Actions can only be “triggered” by code.

You can call actions:

  • From code that executes within a plug-in or custom workflow.

  • From a command that is placed in the application and executes the operation using JavaScript code.

  • From an integration with another system that uses the Microsoft Dynamics 365 web services.

  • From a custom client application that uses the Microsoft Dynamics 365 web services.

In this blog post, we are going to see how to call an Entity Specific Custom Action using JavaScript with the help of Xrm.WebApi introduced in Dynamics 365 CRM v9.0. Calling Global Action is discussed in another blog.

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 formContext = PrimaryControl;
    var sEntityLogicalName = null;
    var sRecordID = null;
    var sFirstParamValue = null;
    var sSecondParamValue = null;
    try {
        sEntityLogicalName = formContext.data.entity.getEntityName();
        sRecordID = formContext.data.entity.getId().slice(1, -1);
        sFirstParamValue = "My Value 1";
        sSecondParamValue = "MyValue 2";
        CallAction(sEntityLogicalName, sRecordID, sFirstParamValue, sSecondParamValue);
    }
    catch (ex) {
        Xrm.Navigation.openErrorDialog({ message: ex.message, details: ex.message });
    }
}

function CallAction(sEntityLogicalName, sRecordGuid, sFirstParamValue, sSecondParamValue) {
    var objActionCallRequest = null;
    var objMetadata = null;
    var sActionName = "rk_LeadAction";
    try {
        objActionCallRequest = {
            entity: {
                entityType: sEntityLogicalName,
                id: sRecordGuid
            },
            sFirstParam: sFirstParamValue,
            sSecondParam: sSecondParamValue,
            getMetadata: function () {
                objMetadata = {
                    boundParameter: "entity",
                    parameterTypes: {
                        "entity": {
                            "typeName": sEntityLogicalName,
                            "structuralProperty": 5
                        },
                        "sFirstParam": {
                            "typeName": "Edm.String",
                            "structuralProperty": 1
                        },
                        "sSecondParam": {
                            "typeName": "Edm.String",
                            "structuralProperty": 1
                        }
                    },
                    operationName: sActionName,
                    operationType: 0    // 0 is for Calling Action using Xrm.WebApi.execute
                };
                return objMetadata;
            }
        };
        Xrm.WebApi.online.execute(objActionCallRequest).then(
            function success(result) {
                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.