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 :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Retrieve value on click of HomepageGrid button

(0) ShareShare
ReportReport
Posted on by 64

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

I have the same question (0)
  • Suggested answer
    cloflyMao Profile Picture
    25,210 on at

    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

  • Shreya kamble Profile Picture
    64 on at

    Hi Clofly,

    Thanks for your response.

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

  • cloflyMao Profile Picture
    25,210 on at

    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

  • cloflyMao Profile Picture
    25,210 on at

    Hi Shreya,

    Could it work for you?

    Regards,

    Clofly

  • Shreya kamble Profile Picture
    64 on at

    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.

  • Suggested answer
    cloflyMao Profile Picture
    25,210 on at

    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

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 74 Super User 2025 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 31 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans