Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

If we have account ID then how to get related contacts

Posted on by Microsoft Employee

Hi,

When i open any account entry, then I get accountID using this script:

var accountId=Xrm.Page.data.entity.getId();

and i want related contacts bind in any textbox on the behalf of account.

*This post is locked for comments

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: If we have account ID then how to get related contacts

    Hi,

    This might help you

    docs.microsoft.com/.../retrieve-entity-using-web-api

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: If we have account ID then how to get related contacts

    Hi ,

    You can implement  below code

    var accountId = Xrm.Page.data.entity.getId();

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/contacts?$select=firstname,lastname,fullname&$filter=_accountid_value eq " + accountId + "", 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.maxpagesize=10");

    req.onreadystatechange = function () {

    if (this.readyState === 4) {

    req.onreadystatechange = null;

    if (this.status === 200)

    {

    var results = JSON.parse(this.response);

    for (var i = 0; i < results.value.length; i++)

    {

    var firstnaem = results.value[i]["firstname"];

    }

    }

    else

    {

    alert(this.statusText);

    }

    }

    };

    req.send();

    }

  • Suggested answer
    Preeti Sharma Profile Picture
    Preeti Sharma 2,678 on at
    RE: If we have account ID then how to get related contacts

    Hi,

    You can get it using quick view form as well.Please check below link:

    community.dynamics.com/.../252551

    Hope this helps:)

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: If we have account ID then how to get related contacts

    Hello,

    Here is the code -

           var accountId = Xrm.Page.data.entity.getId();

           var req = new XMLHttpRequest();

           req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/contacts?$select=firstname,lastname&$filter=_accountid_value eq " + accountId + "", 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.onreadystatechange = function () {

               if (this.readyState === 4) {

                   req.onreadystatechange = null;

                   if (this.status === 200)

                   {

                       var results = JSON.parse(this.response);

                       for (var i = 0; i < results.value.length; i++)

                       {

                           var firstname = results.value[i]["firstname"];

                       }

                   }

                   else

                   {

                       //Error

                   }

               }

           };

           req.send();

  • Suggested answer
    Arpit Shrivastava Profile Picture
    Arpit Shrivastava 7,518 User Group Leader on at
    RE: If we have account ID then how to get related contacts

    Hi,

    please take a look - https://community.dynamics.com/crm/f/117/t/268428

    I have shared the code for the similar requirement. Hope it helps

    Mark this an answer, If it helps.

     

    Cheers
    Arpit
    https://arpitmscrmhunt.blogspot.in

     

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: If we have account ID then how to get related contacts

    Hi,

    You would need to write custom javascript code to retrieve the related contacs for that account using Web API.

    refer this-

    community.dynamics.com/.../ms-crm-2016-web-api-operations-retrieve-single-or-multiple-records

    Hope this helps.

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,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans