Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Custom action with a complex input parameter (entityreference)

Posted on by Microsoft Employee

I create a global (unbound) action in Dynamics 365 (new_newAction) that accepts an input parameter ( of name Target )  with type (EntityReference) and activate it.

I checked that my action is available throw WebAPI

 I want to call it in a JavaScript method, but for testing I wanted to call it from a REST Client ( example Postman, Insomnia  SoapUI).

 To do that  I did the following :

-          I use method POST with a json payload

-          I put my credential ( other calls to the WebAPI endpoint work)

-          I put the Url :   server/organization/api/data/v8.2/tgz_newAction

-          I put necessary headers :

Accept: application/json

OData-MaxVersion: 4.0

OData-Version: 4.0

Content-Type: application/json; charset=utf-8

 -          I put the payload but it’s not correct

Example :

{

     "inputTarget" :

           {

              "@odata.type": "Microsoft.Dynamics.CRM.crmbaseentity",

              "@odata.id": "78713858-5e81-df11-afdb-00155dba380a",

              "@odata.LogicalName": "new_entity",

              "@odata.Name": "new_entity"

         }

}

 But that it’s not working.  Do you have any idea ?

*This post is locked for comments

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Custom action with a complex input parameter (entityreference)

    Please refer to below article : https://community.dynamics.com/365/b/learncrminfingertips/posts/call-action-from-js-how-to-send-parameters-of-different-data-types

    Alternative method : 

    Process.callAction("abc_IssueGuestPass",
    [
    { key: "Opportunity", type: Process.Type.EntityReference, value: { id: obj.EntityRecordId, entityType: "opportunity" } },
    { key: "IssuedBy", type: Process.Type.EntityReference, value: { id: Xrm.Utility.getGlobalContext().userSettings.userId, entityType: "systemuser" } },
    { key: "PassBeginDate", type: Process.Type.DateTime, value: passBeginDate },
    { key: "PassExpireDate", type: Process.Type.DateTime, value: passExpireDate },
    { key: "PassIssuedDays", type: Process.Type.Int, value: parseInt(document.getElementById("abc_passdays").value) },
    { key: "PassRemainingDays", type: Process.Type.Int, value: parseInt(document.getElementById("abc_passdays").value) }
    ],
    function (params) {
    abc.GuestPass.IssueGuestPassSuccess(params);
    },
    function (e, t) {
    console.log(e + "\n" + t);

    });

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Custom action with a complex input parameter (entityreference)

    Sample Code with Different Types of input parameters -

    // Function to initiate Guest Visit Action Calls .................................................................................
    function InitiateGuestPassRequest(PassBeginDate, PassDays) {
    Shared.WriteToConsole("Action Call ......................................");
    var parameters = GetGlobalContext().getQueryStringParameters();
    Shared.WriteToConsole("Entity ID =: " + parameters.id);
    Shared.WriteToConsole("Entity Name =: " + parameters.entityTypeName);

    var InitiatingUserID = Xrm.Utility.getGlobalContext().userSettings.userId;
    Shared.WriteToConsole("InitiatingUserID =: " + InitiatingUserID);

    Shared.WriteToConsole("PassBeginDate =: " + PassBeginDate);
    PassBeginDate = new Date(PassBeginDate.setUTCHours(23, 59, 59));

    Shared.WriteToConsole("PassBeginDate =: " + PassBeginDate);
    Shared.WriteToConsole("PassDays =: " + PassDays);

    var PassExpireDate = new Date();
    PassExpireDate.setDate(PassBeginDate.getDate() + PassDays);
    PassExpireDate = new Date(PassExpireDate.setUTCHours(23, 59, 59));

    Shared.WriteToConsole("PassExpireDate =: " + PassExpireDate);

    window.parent.Xrm.Page.ui.setFormNotification("Guest Pass allocation in Progress.", "INFORMATION", "GuestPassProcessNotification");

    //get the current organization name
    var serverURL = window.parent.Xrm.Page.context.getClientUrl();

    //query to send the request to the global Action
    var actionName = "abc_IssueGuestPass"; // Global Action Unique Name

    //set the current loggedin userid in to _inputParameter of the
    var InputParameterValue = window.parent.Xrm.Page.context.getUserId();

    //Pass the input parameters of action

    var InputParameters = {
    "Opportunity": { "@odata.type": "Microsoft.Dynamics.CRM.opportunity", "opportunityid": parameters.id },
    "IssuedBy": { "@odata.type": "Microsoft.Dynamics.CRM.systemuser", "systemuserid": InitiatingUserID },
    "PassBeginDate": new Date(PassBeginDate),
    "PassExpireDate": new Date(PassExpireDate),
    "PassIssuedDays": parseInt(PassDays),
    "PassRemainingDays": parseInt(PassDays)
    };

    var req = new XMLHttpRequest();
    //Post the WEB API Request
    req.open("POST", serverURL + "/api/data/v9.1/" + actionName, true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");

    req.onreadystatechange = function () {
    if (this.readyState == 4 /* complete */) {
    req.onreadystatechange = null;

    if (this.status == 200 || this.status == 204) {

    //You can get the output parameter of the action with name as given below
    result = JSON.parse(this.response);

    if (result.Error == false) {
    window.parent.Xrm.Page.ui.clearFormNotification("GuestPassProcessNotification");
    window.parent.Xrm.Page.ui.setFormNotification("Guest Pass allocation Process Successfully Executed.", "INFORMATION", "GuestPasSuccessNotification");
    window.parent.Xrm.Page.data.refresh();
    setTimeout(function () {
    window.parent.Xrm.Page.ui.clearFormNotification("GuestPasSuccessNotification");
    }, 3000);
    }
    else {
    window.parent.Xrm.Page.ui.clearFormNotification("GuestPassProcessNotification");
    Shared.UCIIFrameNotifications(result.ErrorMsg, "WARNING", "GuestPassValidationWarning");
    }
    }
    else {
    var error = JSON.parse(this.response).error;
    window.parent.Xrm.Page.ui.clearFormNotification("GuestPassProcessNotification");
    Shared.UCIIFrameNotifications("Please contact IT Service Desk.Error in Action: " + error.message, "ERROR", "GuestPassErrorNotification");
    }
    }
    };
    //Execute request passing the input parameter of the action
    req.send(window.JSON.stringify(InputParameters));


    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Custom action with a complex input parameter (entityreference)

    Hello everyone,

    Doing pretty much the same - calling Action with JS:

    let parameters = {};

       parameters.myParameter =  {

        "inputTarget" :

            {

                "@odata.type": "Microsoft.Dynamics.CRM.new_myEntity",

                "new_myEntityid": parent.Xrm.Page.data.entity.getId().replace(/[{}]/, ''),

            }

       }

       parameters.Target = {

           entityType: "new_myEntity",

           id: entityId,

       };

    But getting an error: Input field type 'JObject' does not match expected type 'EntityReference' for field 'myParameter'

    Any suggestions on what am I doing wrong?

    Thanks in advance.

  • Suggested answer
    Arun Vinoth Profile Picture
    Arun Vinoth 11,613 on at
    RE: Custom action with a complex input parameter (entityreference)

    Yes Daryl is right. This below line

    "new_newentityid": "78713858-5e81-df11-afdb-00155dba380a",


    should has either new_newentityid or "new_newentityid"

    Double quote (") is ok. No need of identifier (@) or dot (.)

     

    "new_newentityid": "78713858-5e81-df11-afdb-00155dba380a",

    or

    new_newentityid: "78713858-5e81-df11-afdb-00155dba380a",


  • Suggested answer
    Daryl LaBar Profile Picture
    Daryl LaBar 500 Most Valuable Professional on at
    RE: Custom action with a complex input parameter (entityreference)

    Your solution has a typo, "new_NewEntity.id" should not have the period before "id" ("new_newentityid") and I believe it should be lowercase as well.

    {
    
        "inputTarget" :
    
              {
    
                 "@odata.type": "Microsoft.Dynamics.CRM.new_NewEntity",
    
                 "new_newentityid": "78713858-5e81-df11-afdb-00155dba380a",
    
                 // no need to put name and logical name.
    
            }
    
    }
  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Custom action with a complex input parameter (entityreference)

    I make it worked when i use the following payload :

    {

        "inputTarget" :

              {

                 "@odata.type": "Microsoft.Dynamics.CRM.new_NewEntity",

                 "new_NewEntity.id": "78713858-5e81-df11-afdb-00155dba380a",

                 // no need to put name and logical name.

            }

    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Custom action with a complex input parameter (entityreference)

    thank you for your speed answers.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Custom action with a complex input parameter (entityreference)

    I believe it would work with SOAP endpoint (when Actions were introduced). Looks like it's platform limitation of WebApi endpoint.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Custom action with a complex input parameter (entityreference)

    why we have the possibility to define a entityreference and not to use it.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Custom action with a complex input parameter (entityreference)

    Then the easiest solution for you is to use string field and pass serialized version of entity reference inside.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans