We can use RetrieveAttributeChangeHistoryRequest to get the change history of a particular field / attribute of a record.
We need to set the Target and the AttributeLogicalName property of the request.
AuditDetails records of the RetrieveAttributeChangeHistoryResponse contains the detail of the audit records.
The Audit History records in CRM-
AttributeAuditDetail contains the details of the changes made on the field’s value. It contains property like – objectid, userid, operation etc. as well as new value and the old value as shown below.
Sample Code (C#)
string ConnectionString = "AuthType = OAuth; " +
"AppId=51f81489-12ee-4a9e-aaae-a2591f45987d; " +
"Username=User1@xxxx.onmicrosoft.com; " +
"Password=*******; " +
"RedirectUri=app://58145B91-0C36-4500-8554-080854F2AC97;" +
"Url = https://xxx.crm.dynamics.com//;";
CrmServiceClient svc = new CrmServiceClient(ConnectionString);
if (svc.IsReady)
{
var attributeChangeHistoryReq = new RetrieveAttributeChangeHistoryRequest();
attributeChangeHistoryReq.Target =
new EntityReference("incident", new Guid("0a9f62a8-90df-e311-9565-a45d36fc5fe8"));
attributeChangeHistoryReq.AttributeLogicalName = "prioritycode";
var attrChangeResponse = (RetrieveAttributeChangeHistoryResponse)svc.Execute(attributeChangeHistoryReq);
var auditDetailCollection = attrChangeResponse.AuditDetailCollection;
foreach (var auditDetails in auditDetailCollection.AuditDetails)
{
// Type = AttributeAuditDetail, AuditDetail,
var type = auditDetails.GetType();
if (type == typeof(AttributeAuditDetail))
{
var attributeDetail = (AttributeAuditDetail)auditDetails;
var userName = attributeDetail.AuditRecord.GetAttributeValue<EntityReference>("userid").Name;
var operation = attributeDetail.AuditRecord.FormattedValues["operation"];
var action = attributeDetail.AuditRecord.FormattedValues["action"];
var createdOn = attributeDetail.AuditRecord.GetAttributeValue<DateTime>("createdon");
var newValue = attributeDetail.NewValue.FormattedValues["prioritycode"];
var oldValue = attributeDetail.OldValue?.FormattedValues["prioritycode"];
}
}
}
Check other posts on Audit –
- Use RetrieveEntityChangeRequest to track changes
- Audit Entity Key Points
- Using KingswaySoft’s CDS/CRM Source component to get Audit information
- Activity Logging – Dynamics 365 Apps
- Export Audit History
Hope it helps..
5 thoughts on “How to – Use RetrieveAttributeChangeHistoryRequest to get audit data for an attribute (Dataverse/ Dynamics 365/ CRM)”