web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Create request object to call an action with Xrm.WebApi from Javascript

(1) ShareShare
ReportReport
Posted on by 363

I associated a Javascript function to a ribbon button.
I have a process of type "action", on the click event of the ribbon button I want to call the action by using Xrm.WebApi.
The process is associated to a custom workflow activity and doesn't take any input/output params, let's say that its name is "My Process Name" and its Unique name is "myProcess_uniqueName" and it's associated to the entity "myEntity".

I'm passing the "actionRequest" object to the Execute method here -->

Xrm.WebApi.online(actionRequest).then(
            function (response) {
                if (response.ok) {
                    Xrm.Navigation.OpenAlertDialog("successful");
                }
            },
            function(error) {
                Xrm.Navigation.OpenAlertDialog("not successful");
            }        
        );


Here is how I created the request object "actionRequest", but I'm not sure if it's correct:

var actionRequest = function() {};

// defined metadata for the object request:
actionRequest.prototype.getMetadata = function () {
            return {
                // my process is an action, so that operationType is 0:
                operationType: 0,
                operationName: "myProcess_UniqueName"
            };
        };

I have the same question (0)
  • Suggested answer
    Wahaj Rashid Profile Picture
    11,321 on at

    Hi,

    Thank you for your post.

    I am not sure if you are facing any issue. If you can share more detail, it would be helpful.

    Furthermore, for your reference, here is a very nice blog on executing an action using Web API in JavaScript:

    https://carldesouza.com/calling-a-dynamics-365-action-from-javascript-using-xrm-webapi-online-execute/

  • Verified answer
    cloflyMao Profile Picture
    25,210 on at

    Hi Joel,

    You said that the action is associated to the custom entity "myEntity".

    So you need to add " boundParameter: 'entity' " to the return body.(and parameterTypes might be also required if you are passing parameters to the action.)

    return {
      boundParameter: "entity",
      operationType: 0,
      operationName: "myProcess_UniqueName"
    }

    Please take a look to this section to know which parameters are required when running action using Xrm.WebApi.online.execute method.

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-webapi/online/execute#parameters

    Try CRMRestBuilder to automatically generate Xrm.WebApi.online.execute style code to check whether anything is missing.

    e.g: QualifyLead is a typical entity-bound action.

    pastedimage1611568075868v1.png

    Regards,

    Clofly

  • cloflyMao Profile Picture
    25,210 on at

    Hi Joel,

    Is there any answer would help you?

  • Joel D Profile Picture
    363 on at

    Hi, thank you all for replying!
    Yes, I'm beginning to understand how it works. 

    As my process is an action, it's bound to entity and takes an EntityId as input parameter, I modified my request object by adding the missing keys and values, but which value should I assign to "structuralProperty" of String? As it's not a primitive type, I used 5, but I'm not sure if it's correct.
    When I call the action with Xrm.WebApi.online.execute(ActionRequest) something goes wrong, I wrote it as in the following:


    ActionRequest.prototype.getMetadata = function () {
                    return {
                        operationType: 0,                        // it's an action
                        operationName: "new_nameOfTheProcess",   // prefix and name of process
                        boundParameter: "entity",                // it's bound to entity
                        parameterTypes: {
                            "entity": {
                                "typeName": "mscrm.schemaNameOfEntity",
                                "structuralProperty": 5 
                            },
                            "EntityId": {
                                "typeName": "Edm.String",
                                "structuralProperty": 5           // Shall I use 5 for string?
                            }
                        }
    
                    };
                };

  • Joel D Profile Picture
    363 on at

    I'm calling the action as in the following but the req.metaData causes issues (the function is not shown by the browser):

    //  Create obj for the target
    var target = {};
    target.entityType = "collection";
    target.Id = collectionid;  
    
    //  build the request...
    var req = {};
    req.entity = target;
    req.CollectionId = collectionid;  // CollectionId is the input parameter of the process
    req.getMetadata = function () {
        return {
            boundParameter: "entity",
            parameterTypes: {
                "entity": {
                    "typeName": "mscrm.collection",
                    "structuralProperty": 5
                },
                "CollectionId": {    // CollectionId is the input parameter of the process
                    "typeName": "Edm.String",
                    "structuralProperty": 1
                }
            },
            operationType = 0,   // process is an action
            operationName = "new_nameOfTheProcess"
        }
    };
    

    collectionid was previously declared as in the following:

    var collectionid = formContext.data.entity.getId();
    



    The block of instructions to create the request works if I comment the block of code to get metadata, the values shown are the following (see comments):

    var req = {}; 
      
    req.entity = target;  // Id: "{D4800847-7E4E-EB11-BF68-000D3A9C7067}" entityType: "collection"
      
    req.EncyclopediaId = collectionid;   // "{D4800847-7E4E-EB11-BF68-000D3A9C7067}"



    In the https://muìyOrganizationName.crm.dynamics.com/api/data/v9.1/$metadata  I get the following info about the process/action:

    <Action Name="myProcessName" IsBound="true">
    <Parameter Name="entity" Type="mscrm.collection" Nullable="false"/>
    <Parameter Name="CollectionId" Type="Edm.String" Unicode="false"/>
    </Action>

    ...Which are the parameters (entity and Id) which I put inside the function to get metadata for the request object (see the first block of code above).


    The process was created from Crm (from Processes -> new process) as in the following:

    - Activate as Process

    - Category: Action

    - Entity: Collection

    - Process Arguments:
         Name : CollectionId
         Type : String
         Required : Optional
         Direction : Input

    The process is associated to a Custom Workflow Activity and the process argument CollectionId is bounded with the "collectionid" input parameter of the Custom Workflow Activity.




Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 49 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 38 Super User 2025 Season 2

#3
#ManoVerse Profile Picture

#ManoVerse 31

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans