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

Announcements

News and Announcements icon
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

I have the same question (0)
  • Jeet Gandhi Profile Picture
    320 on at

    Hello,

    There is an in-built function available named "RetrieveRecordChangeHistory". Use this function in the web api call and it will provide you with the audit details of the specified record.

    docs.microsoft.com/.../retrieverecordchangehistory

    For an alternate approach, please refer below link:

    blogs.microsoft.co.il/.../retrieving-audit-history-records-via-api

    Thank you,

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Replace line

    params = "@target={'@odata.id':'opportunities(" + opportunity_id + ")'}"

    with line

    params = "@target={'@odata.type': 'Microsoft.Dynamics.CRM.opportunity', 'opportunityid':'" + opportunity_id + "'}"

  • Szymon Sobczak Profile Picture
    5 on at

    Hi Jeet, I'm using exactly this function, if you take a look at my code. The problem is I can't get it to work correctly :/

  • Szymon Sobczak Profile Picture
    5 on at

    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.".

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

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

    RetrieveDups.png

  • Szymon Sobczak Profile Picture
    5 on at

    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?

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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
    5 on at

    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...

  • Community Member Profile Picture
    on at

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

  • Szymon Sobczak Profile Picture
    5 on at

    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?

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
RichardM Profile Picture

RichardM 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans