Invoke bound actions (entity actions) using script in Dynamics 365 – Xrm.WebApi.execute

Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com

This is a follow up to my previous blog where I showed you how can you call a global action with all parameter types using the newly introduced Xrm.WebApi.execute method.

https://debajmecrm.com/dynamics-version-9-0-execute-custom-action-with-all-parameter-types-in-dynamics-crm-version-9-0/

Now coming to bound actions i.e actions which are bound to entities, I was getting multiple queries on how to do it after my first blog post. Readers were telling that they are unable to call the action after repeated trials. I was perplexed. So I thought, why not give a try.
So I created a very simple action with the below details:
Name: new_TestProcess
Bound to entity: Account
Input Parameter – EntityReference of type custom entity named new_TestEntity
image
So harmless isn’t it. Even I was thinking the same till it took me more than couple of hours to figure out on how to call this action using Xrm.WebApi.execute.
So let’s dive into the code. The first thing we need to understand is how to pass the parameter for the bound entity. In other words this action will always be called on account record. And how do you pass the account record reference?
Well, the first thing that came to my mind is the input parameter must be named as “Target”. After all that is the convention created by Microsoft right? Well in-fact it’s bit different. It’s not “Target”. Then how do I find out the parameter name.

  • Settings –> Customizations –> Developer resources
  • Dowload Web Api Metadata
image
  • Once downloaded, open up the metadata in Visual studio or any other XML editor of your choice. Search for your action. In this case – “new_TestProcess”.
  • Below is my metadata for my action. See the highlighted line. From that it is clear that the bound entity parameter type is “entity” and not “Target”
image

Ok, past the first hurdle. Now comes my nightmare.
Highlighting Microsoft Documentation for Xrm.WebApi.execute below
image
Well, a very detailed documentation I would say. Everything is pretty much explained. But let’s shift our focus to to the highlighted line which says that for entity actions, we need to set the boundParameter to entity logical name or entity set name. Well, that’s when my ordeal started.
Started with all possible combinations like “mscrm.account”/ “Microsoft.Dynamics.CRM.Account”/”account”/”accounts” and a host of illogical others which are all ridiculous. Smile


Finally just before giving up , I started debugging and went around whichever files the browser takes me in while debugging. And finally eureka moment when I realized that you need to set the value of boundParameter to the word “entity”. Could anyone imagine that from the statement in the documentation.
And finally piece of code below for you
var target = { entityType: “account”, id: “78FA0233-7D86-E811-A94D-000D3A3AB6B4” };
    var reqObject = {};
reqObject.entity = target;
reqObject.ArgEntRef = { “@odata.type”: “Microsoft.Dynamics.CRM.new_testentity”, “@new_testentity.id”: “C182D39E-6B8B-E811-A94D-000D3A3AB6B4” }

    reqObject.getMetadata = function () {
return {
boundParameter: “entity”,
operationType: 0,
operationName: “new_TestProcess”,
parameterTypes: {
“entity”: {
typeName: “mscrm.account”,
structuralProperty: 5
}, “ArgEntRef”:{typeName: “mscrm.new_testentity”,
structuralProperty: 5}
}
}
};

    Xrm.WebApi.execute(reqObject).then(
function (data) {
var e = data;
debugger;
},
function (error) {
debugger;
var errMsg = error.message;
}
);

Hope this helps!
-Debajit Dutta
(Dynamics MVP)
For consultation/ training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

6 thoughts on “Invoke bound actions (entity actions) using script in Dynamics 365 – Xrm.WebApi.execute”

  1. Debajit,
    good stuff, the docs are a little ambiguous, but in their defence the definition does say “The *name* of the bound *parameter* for the action or function to execute. ” (highlights mine)
    To help them improve, why don’t you provide the feedback on the documentation page? If relevant, it’ll be fixed within couple days (speaking from experience).
    Cheers
    George

    1. Hi George, taken your suggestion and provided the feedback. The confusing statement is – “Specify entity logical name or entity set name in case the action or function to execute is bound to one.” This is the one which makes it confusing at the end.

  2. Hi Debajit,
    its a nice experiment.
    Is it possible to show the Executemultiple example and one function example?
    waiting for your reply

  3. Firoz Rahman Shaik

    Hi Debajith,
    I have a similar requirement. Created a global action and tried to call that from script. Everything is working perfect as expected on web client but not on UCI. Unable to figure out the exact issue behind this. Is it a known bug on UCI? Awaiting your response. Thank you.

      1. Firoz Rahman Shaik

        Hi Debajit,
        In UCI, the successcallback, doesn’t contain responseText. Below code would work for both the clients. Thank You.
        Xrm.WebApi.online.execute(reqObj).then(
        function(response){
        return response.json();
        },function(error){
        return error;
        }).then(
        function(response){
        //Use the Action Output parameters here or anyother custom logic
        },function(error){
        //Handle exceptions here
        });

Comments are closed.