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
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();
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.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156