Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

CRM Rest API - Get Opportunity Details with Contacts (Stackholders) ... need help to get current StageID and the Process

Posted on by Microsoft Employee

Good Afternoon,

I am using the CRM REST Builder provided by Jason Lattimer (Thank you for creating this) and I have been able to successfully GET Opportunity Details and the linked Contact (Stackholders).  Where I am running into issues is with trying to determine the current StageID an opportunity is on and the Process (Opportunity Sales Process) the opportunity is associated with.  Any idea what I need to expand to get that information?

/* Get opportunity details with the associated contacts (connection1, connection2) */
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/opportunities(2b685c81-4f1c-e511-80d3-3863bb347ba8)" +
    "?$select=description,stepname,estimatedvalue,opportunityid,statuscode,createdon,modifiedon,name,estimatedclosedate,_ownerid_value" +
    "&$expand=opportunity_connections1($select=name,record1objecttypecode,record2objecttypecode,_record2roleid_value,_record2id_value)" +
    ",opportunity_connections2($select=name,record1objecttypecode,record2objecttypecode,_record2roleid_value,_record2id_value)" +
    ",parentaccountid($select=accountid)", 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;
        console.clear();
        console.log(new Date());
        if (this.status === 200) {
            var result = JSON.parse(this.response);
            console.log('Response: ', result);
            console.log('Opportunity Details: ', {
                'AccountId': (result.parentaccountid) ? result.parentaccountid.accountid : '',
                'Description': (result.description) ? result.description : '',
                'StageName': (result.stepname) ? result.stepname.split('-')[1] : '',
                'Amount': result.estimatedvalue,
                'Id': result.opportunityid,
                'IsDeleted': false,
                'IsWon': (result.statuscode === 3) ? true : false,
                'Type': undefined,
                'IsClosed': (result.statuscode === 1) ? false : true,
                'CreatedDate': result.createdon,
                'SystemModstamp': result.modifiedon,
                'Name': result.name,
                'CloseDate': result.estimatedclosedate,
                'LastModifiedDate': result.modifiedon,
                'OwnerId': result._ownerid_value,
                'RecordTypeId': result.processid
            });
            console.log('Opportunity Contacts: ', result['opportunity_connections1']);
        } else {
            console.log('Error: ', JSON.parse(this.response).error.message);
        }
    }
};
req.send();

*This post is locked for comments

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CRM Rest API - Get Opportunity Details with Contacts (Stackholders) ... need help to get current StageID and the Process

    So after following the link provided I had to go to another link found here.  It appears the Process and Stage information for an opportunity are stored in a new entity (opportunitysalesprocesses) which to looks like you can't use $expand to get to the information.  There was also an entity called (lk_leadtoopportunitysalesprocess_opportunityid), but this threw an error when I attempted that.  So I had to do another HttpRequest in order to get that information. Here is the basic logic in how I was able to console.log the information I needed:

    /* Get opportunity details with the associated contacts (connection1, connection2) */
    var req = new XMLHttpRequest();
    var myOpportunityID = '00000000-0000-0000-0000-000000000000';
    
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/opportunities(" + myOpportunityID + ")" +
        "?$select=description,stepname,estimatedvalue,opportunityid,statuscode,createdon,modifiedon,name,estimatedclosedate,_ownerid_value" +
        "&$expand=opportunity_connections1($select=name,record1objecttypecode,record2objecttypecode,_record2roleid_value,_record2id_value)" +
        ",opportunity_connections2($select=name,record1objecttypecode,record2objecttypecode,_record2roleid_value,_record2id_value)" +
        ",parentaccountid($select=accountid)", 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;
            console.clear();
            if (this.status === 200) {
                var result = JSON.parse(this.response);
                
                var subReq = new XMLHttpRequest();
                subReq.open("GET", Xrm.Page.context.getClientUrl() + '/api/data/v9.0/opportunitysalesprocesses' +
                    '?$select=_activestageid_value,_opportunityid_value,_processid_value' +
                    '&$filter=_opportunityid_value eq ' + myOpportunityID, true);
                subReq.setRequestHeader("OData-MaxVersion", "4.0");
                subReq.setRequestHeader("OData-Version", "4.0");
                subReq.setRequestHeader("Accept", "application/json");
                subReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                subReq.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
                subReq.onreadystatechange = function() {
                    if (this.readyState === 4) {
                        subReq.onreadystatechange = null;
                        console.clear();
                        if (this.status === 200) {
                            var subResult = JSON.parse(this.response)['value'][0];
                            console.log('Opportunity Details: ', {
                                'AccountId': (result.parentaccountid) ? result.parentaccountid.accountid : '',
                                'Description': (result.description) ? result.description : '',
                                'StageId': subResult['_activestageid_value'],
                                'StageName': subResult['_activestageid_value@OData.Community.Display.V1.FormattedValue'],
                                'Amount': result.estimatedvalue,
                                'Id': result.opportunityid,
                                'IsDeleted': false,
                                'IsWon': (result.statuscode === 3) ? true : false,
                                'Type': '',
                                'IsClosed': (result.statuscode === 1) ? false : true,
                                'CreatedDate': result.createdon,
                                'SystemModstamp': result.modifiedon,
                                'Name': result.name,
                                'CloseDate': result.estimatedclosedate,
                                'LastModifiedDate': result.modifiedon,
                                'OwnerId': result._ownerid_value,
                                'RecordTypeId': subResult['_processid_value']
                            });
                            console.log('Opportunity Contacts: ', result['opportunity_connections1']);
                        } else {
                            console.log(JSON.parse(this.response)['error']);
                        }
                    }
                };
                subReq.send();
            } else {
                console.log('Error: ', JSON.parse(this.response).error.message);
            }
        }
    };
    req.send();
  • Suggested answer
    Prashant_ Profile Picture
    Prashant_ 1,040 on at
    RE: CRM Rest API - Get Opportunity Details with Contacts (Stackholders) ... need help to get current StageID and the Process

    In dynamics 365 9.0 version stageid and process id is deprecated.now for each proccess new entities created and maintain records of  stage and proccess id . Following thread will provide more information related  to this.

    https://community.dynamics.com/crm/f/117/p/272877/775161#775161

    I hope this will help you.

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans