Skip to main content

Notifications

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

  • Verified answer
    Community Member Profile Picture
    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
    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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans