CRM Crate

We will learn how to call a global or entity based action using Xrm.WebApi.online.execute in Microsoft Dynamics 365. Actions in Dynamics provides a capability to create business operational logic that can be called in different ways such as through code or a workflow. 

Understanding core concepts of an Action in Dynamics CRM

Calling an action from JavaScript using Xrm.WebApi.online.execute

We will call a global action using Microsoft’s client API reference named “Xrm.WebApi.online.execute“. Below is an action process which has one input parameter and one output parameter with type as global.

Action Process –

JavaScript Code –

   //This Function Is To Be Called In Event Handler (Event On Which You Have To Fire Action)
    var ActionCallingFunction = function () {
        //Stage 1 - Input Parameter For Action
        var parameters = {
            RecordGUID: Xrm.Page.data.entity.getId()
        };
        try {
		
		//Stage 2 - Data Intialization Parameter For Action
            var actionData = new actionProcessingMethod (parameters.RecordGUID);

            //Triggering The Action Process With Web API.
            Xrm.WebApi.online.execute(actionData).then(
                function (data) {
                    if (data.ok) {
                        data.json().then(function (response) {
						
						//Stage 3 - Checking Output Parameter From Custom Code
                            if (response.SuccessCallBack == true) {
						    console.log("Sucessfully Executed Web Service new_CRMCrateGlobalAction");
                            }
                            else {
                                console.log("Error Occured While Calling Action : " + response.SuccessCallBack);
                            }
                        });
                    }
                },
                function (error) {
                    console.log("Error occcurred in executing Global Action");
                    console.log(JSON.stringify(error));
                });
        }
        catch (e) {
            console.log("Error Occured in while calling process : " + JSON.stringify(e));
        }
    };

    //Method Used To Process  Action.
    var actionProcessingMethod = function (RecordGUID) {
        this.RecordGUID = RecordGUID;

        this.getMetadata = function () {
            return {
                boundParameter: null,
                parameterTypes: {
                    "RecordGUID": {
                        "typeName": "Edm.String",
                        "structuralProperty": 1 
                    }
                },
                operationType: 0, 
                operationName: "new_CRMCrateGlobalAction"
            };
        };
    };

Code Explanation –

We have called the action “new_CRMCrateGlobalAction” with help of Xrm.WebApi.online.execute API in the JavaScript.

  • Stage 1 : Assigned data to the input parameter of our action. In our case, we have passed record GUID as an action input parameter.
  • Stage 2 : Collected the parameterize data to be passed in the Web API. This parameterized data includes action parameters and action name.
  • Stage 3 : Once the Web API calls the action, we can perform the further operation by reading the output parameter provided by our action. For example, after successful completion of logic, if the action is passing “True” value in the output parameter , then we can read the output parameter and execute the further code once the output is “True”.

 

CRM Crate

All In One Platform For Learning Microsoft CRM.

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
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.

Practise EXams

Free!!

Sharpen your skills with our free practise exams.