Customer Engagement & Dynamics CRM Forum

Expand all | Collapse all

ribbon javascript calling action

  • 1.  ribbon javascript calling action

    GOLD CONTRIBUTOR
    Posted 20 days ago
    ​Hi Anyone,

    I am trying to call the custom action from the ribbon button javascript. it throws error not found.

    Below is my code:

    var parameters = {};
    parameters.registrationID = "544916CD-6602-EA11-A811-000D3AF3AC9D";

    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/XXXXXX(FC16D1DE-16EE-4ABC-B719-1B734C328A2D)/Microsoft.Dynamics.CRM.XXXXXX", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 204) {
                //Success - No Return Data - Do Something
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send(JSON.stringify(parameters));


    What I am doing wrong here?

    ------------------------------
    Jeebo Mathews
    Agricorp
    ------------------------------
    Conference-CRMUG_200x200


  • 2.  RE: ribbon javascript calling action

    TOP CONTRIBUTOR
    Posted 20 days ago
    Hello Jeebo,
    Can you please post a screenshot of your action and confirm that it is published?

    ------------------------------
    Andrew Butenko
    ------------------------------

    Conference-CRMUG_200x200


  • 3.  RE: ribbon javascript calling action

    GOLD CONTRIBUTOR
    Posted 20 days ago
      |   view attached

    Hi Andrew,

     

    I am sending you the screenshot of the custom action created.

    It's activated.

     

    Regards

     

    Jeebo

     

     




    Conference-CRMUG_200x200


  • 4.  RE: ribbon javascript calling action

    TOP CONTRIBUTOR
    Posted 20 days ago
    It's a bit complicated to compare values you removed from code with values in action you removed from the picture. Do you have any problems publishing it without whipping it out?

    ------------------------------
    Andrew Butenko
    ------------------------------

    Conference-CRMUG_200x200


  • 5.  RE: ribbon javascript calling action

    GOLD CONTRIBUTOR
    Posted 20 days ago
      |   view attached

    I have attached the action screenshot with no edit from my side.

     




    Conference-CRMUG_200x200


  • 6.  RE: ribbon javascript calling action

    TOP CONTRIBUTOR
    Posted 20 days ago
    Thanks! Can I see the code?

    ------------------------------
    Andrew Butenko
    ------------------------------

    Conference-CRMUG_200x200


  • 7.  RE: ribbon javascript calling action

    GOLD CONTRIBUTOR
    Posted 20 days ago

    The javascript:

     

    function testButton(selectedRegistrationItems)

        {

           debugger;

                    //if nothing selected or more than 1 registration selected do nothing

                    if(selectedRegistrationItems.length < 0 || selectedRegistrationItems.length > 1)

                    {

                       return;

                    }

                   

                     var selectedRegistrationGUIDs = '';

                    for (var i = 0; i < selectedRegistrationItems.length; i++) {

                                    selectedRegistrationGUIDs += selectedRegistrationItems[i].Id.replace(/[{}]/g, "") + ";"

                    }

            alert(selectedRegistrationGUIDs);

                   

                    //XMLHttpRequest    

           var parameters = {};

           //parameters.registrationID = "544916CD-6602-EA11-A811-000D3AF3AC9D";

            parameters.registrationID = selectedRegistrationGUIDs;

                    var req = new XMLHttpRequest();

           var actionId = "FC16D1DE-16EE-4ABC-B719-1B734C328A2D"

           req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/fbr_registrations("+actionId +")/Microsoft.Dynamics.CRM.fbr_MakePayment", true);

        req.setRequestHeader("OData-MaxVersion", "4.0");

        req.setRequestHeader("OData-Version", "4.0");

        req.setRequestHeader("Accept", "application/json");

        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

        req.onreadystatechange = function() {

                                    if (this.readyState === 4) {

                                                    req.onreadystatechange = null;

                                                    if (this.status === 204) {

                                                                    //Success - No Return Data - Do Something

                                                    } else {

                                                                    Xrm.Utility.alertDialog(this.statusText);

                                                    }

                                    }

       };

       req.send(JSON.stringify(parameters));

      }

     




    Conference-CRMUG_200x200


  • 8.  RE: ribbon javascript calling action

    TOP CONTRIBUTOR
    Posted 20 days ago
    Thanks! Now it's more or less clear. Where you put action id as a hardcoded guid you should put id of existing fbr_registrationrecord. I see your design - I would recommend to drop this action and recreate it as a global. You can see example here - https://www.inogic.com/blog/2016/10/execute-the-global-action-using-web-api-in-dynamics-crm/

    ------------------------------
    Andrew Butenko
    ------------------------------

    Conference-CRMUG_200x200


  • 9.  RE: ribbon javascript calling action

    TOP CONTRIBUTOR
    Posted 20 days ago
    Jeebo

    Try putting this at the first line in your code.

    console.log("Hello")

    If you don't see Hello in your console, then the ribbon cannot find your code.

    Also, have you tried using the new 9.1 WebApi? It is a LOT easier to use than the outdated xhr.
    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-webapi/createrecord

    const customAction = async() => {
        const par = {registrationID: "544916CD-6602-EA11-A811-000D3AF3AC9D"},
            logicalName = "XXXXXX"
    
        await Xrm.WebApi.createRecord(logicalName, par);
    }




    ------------------------------
    Rex Kenley Tan, MCSA, MCSD
    Tallmadge OH
    https://www.youracclaim.com/users/rex-kenley-tan

    *Always be CURRENT with JavaScript & C#, NEVER be obsolete.

    DISCLAIMER: All views expressed on this site are my own and DO NOT represent the opinions of ANY entity whatsoever with which I have been, am now, or will be affiliated.
    ------------------------------

    Conference-CRMUG_200x200


  • 10.  RE: ribbon javascript calling action

    GOLD CONTRIBUTOR
    Posted 20 days ago
    ​Hi Rex,

    The JavaScript is showing the alert about the current ID selection from the ribbon. So I can confirm the JavaScript is been called.

    ------------------------------
    Jeebo Mathews
    Agricorp
    ------------------------------

    Conference-CRMUG_200x200


  • 11.  RE: ribbon javascript calling action

    TOP CONTRIBUTOR
    Posted 19 days ago
    Edited by Rex Kenley Tan 19 days ago
    Jeebo

    Please try this. This is how you should be calling actions with js in v9.1.
    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-webapi/online/execute

    async function testButton(selectedRegistrationItems) {
      try {
        if (selectedRegistrationItems.length !== 1) return;
    
        const registrationID = selectedRegistrationItems[0].Id.replace(/[{}]/g, ""),
          result = await Xrm.WebApi.online.execute({
            registrationID,
            getMetadata: () => {
              return {
                boundParameter: "fbr_registration",
                parameterTypes: {
                  registrationID: { typeName: "Edm.String", structuralProperty: 1 }
                },
                operationType: 0,
                operationName: "fbr_MakePayment"
              };
            }
          });
    
        if (result.ok) console.log(`Status: ${result.status} ${result.statusText}`);
      } catch (e) {
        console.error(e.message || e);
      }
    }
    


    ------------------------------
    Rex Kenley Tan, MCSA, MCSD
    Tallmadge OH
    https://www.youracclaim.com/users/rex-kenley-tan

    *Always be CURRENT with JavaScript & C#, NEVER be obsolete.

    DISCLAIMER: All views expressed on this site are my own and DO NOT represent the opinions of ANY entity whatsoever with which I have been, am now, or will be affiliated.
    ------------------------------

    Conference-CRMUG_200x200


  • 12.  RE: ribbon javascript calling action

    GOLD CONTRIBUTOR
    Posted 19 days ago

    Hi Rex,

     

    Thanks for your reply. If I use WebApi call in JavaScript It is giving me error at the catch. "resource not found for the segment".

    Any clue for this particular error.

     

    Thanks

     

    Regards

     

    Jeebo

     




    Conference-CRMUG_200x200


  • 13.  RE: ribbon javascript calling action

    TOP CONTRIBUTOR
    Posted 19 days ago
    Edited by Rex Kenley Tan 19 days ago
    Jeebo

    Please change boundParameter to (I read the documentation wrong)

    boundParameter: "entity"

    boundParameter: (Optional) String. The name of the bound parameter for the action or function to execute.
    • Specify undefined if you are executing a CRUD request.
    • Specify null if the action or function to execute is not bound to any entity.
    • Specify entity in case the action or function to execute is bound to an entity.

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

    ------------------------------
    Rex Kenley Tan, MCSA, MCSD
    Tallmadge OH
    https://www.youracclaim.com/users/rex-kenley-tan

    *Always be CURRENT with JavaScript & C#, NEVER be obsolete.

    DISCLAIMER: All views expressed on this site are my own and DO NOT represent the opinions of ANY entity whatsoever with which I have been, am now, or will be affiliated.
    ------------------------------

    Conference-CRMUG_200x200


  • 14.  RE: ribbon javascript calling action

    GOLD CONTRIBUTOR
    Posted 19 days ago

    Hi Rex,

     

    I am getting same error message even after using  "fbr_registrations" for bound parameter

     

    Regards

     

    Jeebo

     




    Conference-CRMUG_200x200


  • 15.  RE: ribbon javascript calling action

    TOP CONTRIBUTOR
    Posted 19 days ago
    Edited by Rex Kenley Tan 19 days ago
    Jeebo

    Finally found a decent example for bound actions. You may have to look at your action metadata to get the proper name of your bound entity.

    async function testButton(selectedRegistrationItems) {
      try {
        if (selectedRegistrationItems.length !== 1) return;
    
        const registrationID = selectedRegistrationItems[0].Id.replace(/[{}]/g, ""),
          result = await Xrm.WebApi.online.execute({
            registrationID,
            getMetadata: () => {
              return {
                boundParameter: "entity",
                parameterTypes: {
                  entity: {
                    typeName: "fbr_registration",
                    structuralProperty: 5
                  },
                  registrationID: { typeName: "Edm.String", structuralProperty: 1 }
                },
                operationType: 0,
                operationName: "fbr_MakePayment"
              };
            }
          });
    
        if (result.ok) console.log(`Status: ${result.status} ${result.statusText}`);
      } catch (e) {
        console.error(e.message || e);
      }
    }


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

    ------------------------------
    Rex Kenley Tan, MCSA, MCSD
    Tallmadge OH
    https://www.youracclaim.com/users/rex-kenley-tan

    *Always be CURRENT with JavaScript & C#, NEVER be obsolete.

    DISCLAIMER: All views expressed on this site are my own and DO NOT represent the opinions of ANY entity whatsoever with which I have been, am now, or will be affiliated.
    ------------------------------

    Conference-CRMUG_200x200


  • 16.  RE: ribbon javascript calling action

    GOLD CONTRIBUTOR
    Posted 19 days ago

    Hi Rex,

     

    Thanks Rex. After the code change It giving different error "Cannot read property 'id' of undefined"  

    This should be const registrationID issue.

     

    Not sure why?

     

    Regards

     

    Jeebo

     

     

     




    Conference-CRMUG_200x200


  • 17.  RE: ribbon javascript calling action

    TOP CONTRIBUTOR
    Posted 19 days ago
    Jeebo

    I think it is coming from somewhere else. It is complaining about "id" (case sensitive) and the Id in the code has a capital I.

    But just to be sure put a break at console.error. It will stop there if there is something wrong with the code.

    ------------------------------
    Rex Kenley Tan, MCSA, MCSD
    Tallmadge OH
    https://www.youracclaim.com/users/rex-kenley-tan

    *Always be CURRENT with JavaScript & C#, NEVER be obsolete.

    DISCLAIMER: All views expressed on this site are my own and DO NOT represent the opinions of ANY entity whatsoever with which I have been, am now, or will be affiliated.
    ------------------------------

    Conference-CRMUG_200x200


  • 18.  RE: ribbon javascript calling action

    GOLD CONTRIBUTOR
    Posted 18 days ago
    ​Hi Rex,

    I used the CRMRestApi to build the WebApi call to the action and it worked.

    function testButton(selectedRegistrationItems)
    {
       debugger;
       if(selectedRegistrationItems.length < 0 || selectedRegistrationItems.length > 1)
      {
        return;
      }

      var selectedRegistrationGUIDs = '';
      for (var i = 0; i < selectedRegistrationItems.length; i++) {
      selectedRegistrationGUIDs += selectedRegistrationItems[i].Id.replace(/[{}]/g, "")
      }
         alert(selectedRegistrationGUIDs);

       var parameters = {};
    var entity = {};
    entity.id = selectedRegistrationGUIDs;
    entity.entityType = "fbr_registration";
    parameters.entity = entity;

    var fbr_MakePaymentRequest = {
        entity: parameters.entity,

        getMetadata: function() {
            return {
                boundParameter: "entity",
                parameterTypes: {
                    "entity": {
                        "typeName": "mscrm.fbr_registration",
                        "structuralProperty": 5
                    }
                },
                operationType: 0,
                operationName: "fbr_MakePayment"
            };
        }
    };

    Xrm.WebApi.online.execute(fbr_MakePaymentRequest).then(
        function success(result) {
            if (result.ok) {
                //Success - No Return Data - Do Something
            }
        },
        function(error) {
            Xrm.Utility.alertDialog(error.message);
        }
    );
    }

    Thanks Rex and Andrew for your help and advice.


    ------------------------------
    Jeebo Mathews
    Agricorp
    ------------------------------

    Conference-CRMUG_200x200


If you've found this thread useful, dive deeper into User Group community content by role