{Dynamics Version 9.0} Execute custom action with all parameter types in Dynamics CRM Version 9.0

Dynamics Version 9.0 introduced the Xrm.WebApi namespace which provides all the methods to interact with dynamics CRM server from client side script.

Detailed Documentation – https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference/xrm-webapi

However recently I was working on a project to upgrade the SOAP calls from client and replace them with the new Xrm.WebApi methods. Create/ delete/ update were kind of easy and was easily ported to the new API’s.

However then came the Actions. The client had actions which had many input parameters and which were getting invoked from the client using SOAP calls. We had to port them to the Xrm.WebApi. But in Xrm.WebApi we did not had any function like executeAction. But the Xrm.WebApi.execute came to our rescue.

Let’s first examine Xrm.WebApi.execute method. Microsoft documentation shows it like this –

Xrm.WebApi.execute(request).then(successCallback, errorCallback);

For the sake of brevity of this blog post, I am skipping the documentation of the parameters. For detailed information on the parameters, please refer the below Microsoft documentation link – https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference/xrm-webapi/execute

 

So how to call an action with input parameters? For this post, I created an action named “new_TestAction”. The action is global. It has the following input parameters.

  • Name – Type (string)
  • Age – Type(string)
  • InputEntityReference – Type (EntityReference)
  • InputEntity – Type (Entity)
  • InputEntityCollection – Type(EntityCollection)

For entityreference, entitycollection and entity, I have kept the Entity as Account Entity. All set and done. Now the next step is to call the action. below is the sample code to create the request object. All the important parameters has been highlighted and appropriate comments put.

TestAction1: function (input1, input2, inputEntityReference, inputAccount, inputAccountCollection) {
        this.InputEntityReference = inputEntityReference;
        this.Input1 = input1;
        this.Input2 = input2;
        this.InputEntity = inputAccount;

        this.InputEntityCollection = inputAccountCollection;

        this.getMetadata = function () {
            return {
                boundParamter: null, // for entity action, put the entitysetname/ entitylogicalname here
                parameterTypes: {
                    "Input1": {
                        "typeName": "Edm.String",
                        "structuralProperty": 1 // Primitive Type
                    },
                    "Input2": {
                        "typeName": "Edm.String",
                        "structuralProperty": 1 // Primitive Type
                    },
                    "InputEntityReference": { // Entity Reference
                        "typeName": "mscrm.account",
                        "structuralProperty": 5
                    },
                    "InputEntity": { // entity
                        "typeName": "mscrm.account",
                        "structuralProperty": 5
                    },
                    "InputEntityCollection": { // entity collection
                        "typeName": "mscrm.account",
                        "structuralProperty":4 // collection type
                    }
                },
                operationType: 0, // 0 for action, 1 for function and 2 for CRUD
                operationName: "new_TestAction1"
            };
        };
    }

Next is the code to create this request object and pass the object to the action.

ExecuteAction: function () {
        debugger;
        var accountReference = {
            "@odata.type": "Microsoft.Dynamics.CRM.account",
            "@account.id": "9602CEB2-55F7-E711-A954-000D3A34A0AA"
        };
        var accountEntity = {
            "@odata.type": "Microsoft.Dynamics.CRM.account",
            "@account.id": "9602CEB2-55F7-E711-A954-000D3A34A0AA"
        };
        var accountEntityCollection =[
            {
            "@odata.type": "Microsoft.Dynamics.CRM.account",
            "@account.id": "9602CEB2-55F7-E711-A954-000D3A34A0AA",
            "name": "Test Account 1"
            },
            {
            "@odata.type": "Microsoft.Dynamics.CRM.account",
            "@account.id": "9602CEB2-55F7-E711-A954-000D3A34A0AA",
            "name": "Test Account 1"
            }];
        var requestObject = new TestAction1("Name", "Age", accountReference, accountEntity, accountEntityCollection);
        Xrm.WebApi.execute(requestObject).then(function (result) {
            debugger;
        },
        function (error) {
            debugger;
            console.log(error.message);
        });
    }

Simple isn’t it. I haven’t covered the other primitive types like – Decimal/ Int. They are pretty simple like for int, it is Edm.Int32.

Hope this helps!

 

-Debajit Dutta

(Dynamics MVP)

For consultation/ training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

 

 

Advertisements

Author: Debajit

I am a Dynamics CRM Most Valuable Professional (MVP) with 10 years of experience in Microsoft .NET Technologies and 7 years of dedicated experience in Microsoft Dynamics CRM. I have worked with companies like Microsoft, SanDisk, PwC, TMF Group and have extensive experience of implementing complex CRM solutions from both offshore and client side. Currently the face of XrmForYou.com with significant experience in delivering corporate training on Dynamics CRM and have already delivered multiple projects to client through XrmForYou.com Author of multiple tools on codeplex including the 'Role Based Views' and 'CRM-Sharepoint Metadata manager & Attachment Extractor' which are available for commercial use under XrmForYou.com For consulting/ training, drop me a note at info@xrmforyou.com or visit our website www.xrmforyou.com

4 thoughts on “{Dynamics Version 9.0} Execute custom action with all parameter types in Dynamics CRM Version 9.0”

  1. Hi Debajit,

    Thank you so much for this blogpost. It helped me a lot.

    One question: Should this “@account.id”: be “accountid”: ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s