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