Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

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

(1) ShareShare
ReportReport
Posted on by 355

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"
            };
        };

  • Joel D Profile Picture
    Joel D 355 on at
    RE: Create request object to call an action with Xrm.WebApi from Javascript

    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.




  • Joel D Profile Picture
    Joel D 355 on at
    RE: Create request object to call an action with Xrm.WebApi from Javascript

    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?
                            }
                        }
    
                    };
                };

  • cloflyMao Profile Picture
    cloflyMao 25,202 on at
    RE: Create request object to call an action with Xrm.WebApi from Javascript

    Hi Joel,

    Is there any answer would help you?

  • Verified answer
    cloflyMao Profile Picture
    cloflyMao 25,202 on at
    RE: Create request object to call an action with Xrm.WebApi from Javascript

    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

  • Suggested answer
    Wahaj Rashid Profile Picture
    Wahaj Rashid 11,321 on at
    RE: Create request object to call an action with Xrm.WebApi from Javascript

    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/

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,735 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,466 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans