Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Retrieve value on click of HomepageGrid button

Posted on by 62

Hi,

I have added a button on home page grid which will be visible only when a single record is selected. On click of this button, I want to retrieve value from another entity's record using javascript code. 

Any help would be appreciated !

Thanks,

Shreya

  • Suggested answer
    cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Retrieve value on click of HomepageGrid button

    Hi Shreya,

    We can add following modifications to make the function be a synchronous call.

    1. set the last parameter to "false": req.open("GET", Xrm.Page.context.getClientUrl() "/api/data/v9.1/contacts" query, false);

    2. remove the req.onreadystatechange function.

    var query = "?$filter=contains(lastname,'mao')"
    
    var list = [];
    list = synchronousCall(query);
    console.log(list[0]);
    
    function synchronousCall(queryStr) {
      var arr = [];
      // Native XMLHttpRequest
      var req = new XMLHttpRequest();
      req.open("GET", Xrm.Page.context.getClientUrl()   "/api/data/v9.1/contacts"   queryStr, false);
      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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
      req.send();
      if (req.readyState === 4) {
        //req.onreadystatechange = null;
        if (req.status === 200) {
          var result = JSON.parse(req.response);
          for (var i = 0; i < result.value.length; i  ) {
            arr.push(result.value[i]);
          }
        } else {
          Xrm.Utility.alertDialog((JSON.parse(req.response).error).message);
        }
        return arr;
      };
    }
    

    Result: 

    In Chrome, it will notify us that we sent Synchronous XHR.

    1425.JPG

    Regards,

    Clofly

  • Shreya kamble Profile Picture
    Shreya kamble 62 on at
    RE: Retrieve value on click of HomepageGrid button

    Hi,

    Thanks for your help!!.

    I was looking for a synchronous call but I have managed to get the desired result using Asynchronous Client Web Api.

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Retrieve value on click of HomepageGrid button

    Hi Shreya,

    Could it work for you?

    Regards,

    Clofly

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Retrieve value on click of HomepageGrid button

    Hi Shreya,

    How do you want to retrieve records? For example by Web API?

    We can use Client API(Xrm.Page or Xrm.Utility) in JavaScript web resource(command of button) directly, as what we do in form.

    Sample code below will retrieve contacts whose lastname field contains 'mao'.

    var query = "?$filter=contains(lastname,'mao')"
    
    // Native XMLHttpRequest
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl()   "/api/data/v9.1/contacts"   query, 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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var result = JSON.parse(this.response);
                for (var i = 0;i < result.value.length;i  ) {
                    // console.log(result.value[i]);
                }
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
    
    // Client API SDK
    Xrm.WebApi.retrieveMultipleRecords("contact", query).then(
        function success(result) {
            if (result.entities.length > 0) {
                for (var i = 0; i < result.entities.length; i  ) {
                    console.log(result.entities[i]);
                }
            } else {
                console.log("No data");
            }
        },
        function (error) {
            console.log(error.message);
        }
    );

    Regards,

    Clofly

  • Shreya kamble Profile Picture
    Shreya kamble 62 on at
    RE: Retrieve value on click of HomepageGrid button

    Hi Clofly,

    Thanks for your response.

    I want to retrieve records from an entity on button click irrespective of the selected record.

  • Suggested answer
    cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Retrieve value on click of HomepageGrid button

    Hi Shreya,

    Do you want to pass id of selected record to your JavaScript function when clicking the button?

    If so, you can pass "SelectedControlSelectedItemIds" parameter(Crm Parameter) to the function.

    2742.JPG

    Result:

    pastedimage1593158989582v2.png

    Please take articles below as reference to understand how to use other parameters.

    https://rajeevpentyala.com/tag/selectedcontrolselecteditemids/

    pastedimage1593158418148v1.png

    https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/customize-dev/pass-dynamics-365-data-page-parameter-ribbon-actions#grid-values

    Regards,

    Clofly

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