web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    Prashant_ Profile Picture
    1,040 on at

    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.

  • Verified answer
    Community Member Profile Picture
    on at

    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();

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans