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 :
Microsoft Dynamics CRM (Archived)

How to find contacts for a given opportunity using REST Web API

(0) ShareShare
ReportReport
Posted on by

I created a new opportunity - let's say "Fred Sanford Salvage" and I added contact "Fred Sanford" as the opportunity contact and contact "Lamont Sanford" as one of the stakeholders on this deal.

Using the RESTful Web API requests how can I find these 2 contacts associated with this opportunity?

I apologize if this has been asked before.  I tried searching and could not find an appropriate answer.

Thanks in advance.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    jlattimer Profile Picture
    24,562 on at

    To retrieve the existing contact:

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/leads(8CE6C6DE-1BFF-E411-80D4-C4346BAC4730)?$select=_parentcontactid_value", 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);
                var _parentcontactid_value = result["_parentcontactid_value"];
                var _parentcontactid_value_formatted = result["_parentcontactid_value@OData.Community.Display.V1.FormattedValue"];
                var _parentcontactid_value_lookuplogicalname = result["_parentcontactid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();

    To retrieve the connections:

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/leads(8CE6C6DE-1BFF-E411-80D4-C4346BAC4730)?$select=leadid,subject&$expand=lead_connections1($select=name)", 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);
    var leadid = result["leadid"];
    var subject = result["subject"];
    for (var a = 0; a < result.lead_connections1.length; a++) {
    var lead_connections1_name = result.lead_connections1[a]["name"];
    }
    } else {
    Xrm.Utility.alertDialog(this.statusText);
    }
    }
    };
    req.send()

    To retrieve the contact from each connection:

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/connections(751b3eae-c1fe-e611-8109-fc15b4286724)?$select=name,_record2id_value,record2objecttypecode,_record2roleid_value", 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);
                var name = result["name"];
                var _record2id_value = result["_record2id_value"];
                var _record2id_value_formatted = result["_record2id_value@OData.Community.Display.V1.FormattedValue"];
                var _record2id_value_lookuplogicalname = result["_record2id_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                var record2objecttypecode = result["record2objecttypecode"];
                var record2objecttypecode_formatted = result["record2objecttypecode@OData.Community.Display.V1.FormattedValue"];
                var _record2roleid_value = result["_record2roleid_value"];
                var _record2roleid_value_formatted = result["_record2roleid_value@OData.Community.Display.V1.FormattedValue"];
                var _record2roleid_value_lookuplogicalname = result["_record2roleid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
  • Community Member Profile Picture
    on at

    Thanks Jason,  I was able to request all opportunity to contact connections for a given opportunity.  That did the trick.  I did not know about the connections entity.

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans