Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How can I retrieve the previous values of fields on Opportunity, using API?

(0) ShareShare
ReportReport
Posted on by 5

Hi,

How do I fetch Opportunity field history using Web API?

I'm trying to use RetrieveRecordChangeHistory function, but it gives me errors. This is what I'm doing:

import requests

domain = 'https://[DOMAIN].crm.dynamics.com/"
opportunity_id = 'eeeda8ea-4d1b-e911-a82d-000d3a3122f2'

api_path = "/api/data/v9.0/"
path = "RetrieveRecordChangeHistory(Target=@target)"
params = "@target={'@odata.id':'opportunities(" + opportunity_id + ")'}"

# I also tried this, but I'm getting the same error:
# params = "@target={'@odata.type':'Microsoft.Dynamics.CRM.opportunity','opportunityid':'" + opportunity_id + "'}"

url = domain + api_path + path + '?' + params
print(url)
resp = requests.request('get', url, headers=client.headers())

The above gives me 500 error with this description:

https://twelvezeros.crm.dynamics.com/api/data/v9.0/RetrieveRecordChangeHistory(Target=@target)?@target={'@odata.id':'opportunities(eeeda8ea-4d1b-e911-a82d-000d3a3122f2)'}

{ 'message': 'Object reference not set to an instance of an object.', 'type': 'System.NullReferenceException', 'stacktrace': 'at Microsoft.Crm.Extensibility.OData.TypeConverters.EdmComplexTypeConverter.ConvertToEdmTypeInternal(Object crmTypeValue) at Microsoft.Crm.Extensibility.OData.TypeConverters.EdmTypeConverterBase`2.ConvertToEdmType(Object crmTypeValue) at Microsoft.Crm.Extensibility.OData.TypeConverters.EdmComplexTypeConverter.ConvertToEdmTypeInternal(Object crmTypeValue) at Microsoft.Crm.Extensibility.OData.TypeConverters.EdmTypeConverterBase`2.ConvertToEdmType(Object crmTypeValue) at Microsoft.Crm.Extensibility.OData.TypeConverters.EdmCollectionTypeConverter.ConvertToEdmTypeInternal(Object crmTypeValue) at Microsoft.Crm.Extensibility.OData.TypeConverters.EdmTypeConverterBase`2.ConvertToEdmType(Object crmTypeValue) at Microsoft.Crm.Extensibility.OData.TypeConverters.EdmComplexTypeConverter.ConvertToEdmTypeInternal(Object crmTypeValue) at Microsoft.Crm.Extensibility.OData.TypeConverters.EdmTypeConverterBase`2.ConvertToEdmType(Object crmTypeValue) at Microsoft.Crm.Extensibility.OData.RequestResponseConverter.GetEdmOperationResponse(IEdmModel edmModel, EdmOperation edmOperation, OrganizationResponse orgResponse, SdkResponseDescription responseDescription, Guid organizationId) at Microsoft.Crm.Extensibility.OData.CrmODataServiceDataProvider.ExecuteOperation(CrmODataExecutionContext context, EdmOperation edmOperation, Dictionary`2 parameters, Dictionary`2 boundParameters) at Microsoft.Crm.Extensibility.OData.ActionController.ProcessOperationRequest(String operationName, Dictionary`2 operationParameters, EntityReference entityReference, String boundEntityName, String boundEntityType) at Microsoft.Crm.Extensibility.OData.ActionController.<>c__DisplayClass12_0.<GetUnboundFunction>b__0() at Microsoft.PowerApps.CoreFramework.ActivityLoggerExtensions.Execute[TResult](ILogger logger, EventId eventId, ActivityType activityType, Func`1 func, IEnumerable`1 additionalCustomProperties) at Microsoft.Xrm.Telemetry.XrmTelemetryExtensions.Execute[TResult](ILogger logger, XrmTelemetryActivityType activityType, Func`1 func) at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- (...)

The Opportunity I'm referencing here exist in the CRM. When I log in using the browser, open that Opportunity and go to "Audit History" I see a table of 13 records of ChangeHistory, with all the changes listed nicely.

How can I retrieve the previous values of fields on Opportunity, using API?

*This post is locked for comments

  • Szymon Sobczak Profile Picture
    Szymon Sobczak 5 on at
    RE: How can I retrieve the previous values of fields on Opportunity, using API?

    Hey, that's an interesting one. I was notified by the support recently that it has been fixed, but I never checked (we implemented a workaround in the meantime).

    But if you keep seeing it, I guess it's not fixed....

  • CooksterC Profile Picture
    CooksterC 146 on at
    RE: How can I retrieve the previous values of fields on Opportunity, using API?

    Hey Szymon

    I ran into this problem today, assume this is still outstanding?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How can I retrieve the previous values of fields on Opportunity, using API?

    Thanks Szymon for the update.

  • Szymon Sobczak Profile Picture
    Szymon Sobczak 5 on at
    RE: How can I retrieve the previous values of fields on Opportunity, using API?

    Haha, Steven Gao, you wish! So far it's been only 6 weeks. The support sends me an email with an update every week. And update is:

    "We are still waiting for the ETA from the DEV time regarding the issue"

    6 times. Come back in 2020 I guess?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How can I retrieve the previous values of fields on Opportunity, using API?

    I have got the same error message. Is the problem resolved?

  • Szymon Sobczak Profile Picture
    Szymon Sobczak 5 on at
    RE: How can I retrieve the previous values of fields on Opportunity, using API?

    So this is getting really interesting. I created a new trial account on Dynamics, to test this from a "fresh start". This API call was not throwing any errors on the new account initially, but since Auditing was disabled on the new account, it was not returning any actual data either. But when I enabled auditing of Opportunities, I started getting the same errors on the new account.

    I might have changed something else in the settings, I'm not really sure, but by now I think this is related to account setup.

    I've opened a support ticket too. I'll post the solution here if I end up resolving it. I would still really appreciate any hints on what the solution might be. I'm working on integrating my product with MS Dynamics. I have a big potential client waiting for this, and my time is running out...

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: How can I retrieve the previous values of fields on Opportunity, using API?

    Casing or validity of Guid should not matter. I changed guid to guid of record that doesn't exist in my CRM and it worked anyway.

    To be honest I have no idea what is wrong in your query. Everything looks good for me.

  • Szymon Sobczak Profile Picture
    Szymon Sobczak 5 on at
    RE: How can I retrieve the previous values of fields on Opportunity, using API?

    Hi Andrew, I really appreciate your help on this.

    I tried to reproduce your result exactly, so I moved to Postman, just to make sure I have the same environment. But that didn't help:

    Screen-Shot-2019_2D00_01_2D00_21-at-9.46.02-PM.png

    I have one more idea, your opportunityid is uppercase and mine is lowercase. Where did you copy this ID from? Maybe I'm using wrong ID?

  • Verified answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: How can I retrieve the previous values of fields on Opportunity, using API?

    I don't know what's wrong with your query but mine works as it expected:

    RetrieveDups.png

  • Szymon Sobczak Profile Picture
    Szymon Sobczak 5 on at
    RE: How can I retrieve the previous values of fields on Opportunity, using API?

    Hi Andrew, thank you for your suggestion, unfortunately this doesn't work. I'm still getting error 500: "Object reference not set to an instance of an object.".

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 Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,492 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans