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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

How to retrieve audit history

(0) ShareShare
ReportReport
Posted on by 250

Hello everyone my name is Taniguchi

I'm using C# plugin to retrieve audit history for the opportunity entity and i wish to retrieve every field that were changed and i was able to retrieve audit details like creadon, action and operation now I wish to retrieve the fields that were changed and old and new value. How could can i get this values in plugin ?

My code so far:

string EntitySchemaName = "opportunity";
foreach (var opportunityRecords in result.Entities)
{
    string opportunityGUID = opportunityRecords.Attributes["opportunityid"].ToString();
    string opportunityNumber = opportunityRecords.Attributes["vale_opportunitynumber"].ToString();
    Guid _opportunityID =  new Guid(opportunityGUID);
    // Retrieve the audit history for the account and display it.
    RetrieveRecordChangeHistoryRequest changeRequest = new RetrieveRecordChangeHistoryRequest();
    changeRequest.Target = new EntityReference(EntitySchemaName, _opportunityID);
    RetrieveRecordChangeHistoryResponse changeResponse = (RetrieveRecordChangeHistoryResponse)organizationService.Execute(changeRequest);
     AuditDetailCollection details = changeResponse.AuditDetailCollection;
    foreach (var  attrAuditDetail in details.AuditDetails)
    {
        var detailType2 = attrAuditDetail.GetType();
        if (detailType2 == typeof(Microsoft.Crm.Sdk.Messages.AttributeAuditDetail))
        {
            Microsoft.Crm.Sdk.Messages.AttributeAuditDetail attributeDetail = (Microsoft.Crm.Sdk.Messages.AttributeAuditDetail)attrAuditDetail;
            //Details of Audit
            var auditDetailsRequest = new Microsoft.Crm.Sdk.Messages.RetrieveAuditDetailsRequest
            {
                AuditId = attributeDetail.AuditRecord.Id
            };
            var auditDetailsResponse = (Microsoft.Crm.Sdk.Messages.RetrieveAuditDetailsResponse)organizationService.Execute(auditDetailsRequest);
            if (auditDetailsResponse != null && auditDetailsResponse.AuditDetail != null && auditDetailsResponse.AuditDetail.AuditRecord != null)
            {
                Microsoft.Xrm.Sdk.Entity auditEntity = auditDetailsResponse.AuditDetail.AuditRecord;
                    if (auditEntity.Attributes.Contains("createdon"))
                    {
                        Console.WriteLine(string.Format("Audit2 CreatedOn {0}", auditDetailsResponse.AuditDetail.AuditRecord.Attributes["createdon"]));
                    }
             }
        }
    }
}

I have the same question (0)
  • Suggested answer
    Thomas David Dayman Profile Picture
    11,323 on at

    Hi,

    I have some working code below:

     if (Service != null)
                {
                    RetrieveRecordChangeHistoryRequest changeRequest = new RetrieveRecordChangeHistoryRequest();
                    try
                    {
                        Guid guid = TrimGuid(recordGuid.Text);
    
                        changeRequest.Target = new EntityReference(entitiesList.SelectedItem.ToString(), guid);
                        RetrieveRecordChangeHistoryResponse changeResponse =
                        (RetrieveRecordChangeHistoryResponse)Service.Execute(changeRequest);
    
                        AuditDetailCollection auditDetailCollection = changeResponse.AuditDetailCollection;
    
                        Entity entity = Service.Retrieve(entitiesList.SelectedItem.ToString(), guid, new ColumnSet(true));
    
                        foreach (var attrAuditDetail in auditDetailCollection.AuditDetails)
                        {
                            var detailType = attrAuditDetail.GetType();
                            if (detailType == typeof(AttributeAuditDetail))
                            {
                                var auditRecord = attrAuditDetail.AuditRecord;
                                var attributeDetail = (AttributeAuditDetail)attrAuditDetail;
    
                                var newValueEntity = attributeDetail.NewValue;
                                var oldValueEntity = attributeDetail.OldValue;
                                TargetEntity = new Entity(oldValueEntity.LogicalName, oldValueEntity.Id);
    
                                var action = auditRecord.FormattedValues["action"];
                                if (Array.Exists(ValidActions, x => x == action))
                                {
    
                                    foreach (KeyValuePair attribute in attributeDetail.NewValue.Attributes)
                                    {
                                        string fieldmetadata = "";
                                        string lookupId = "";
                                        string oldValue = "";
                                        string newValue = "";
    
                                        if (attributeDetail.OldValue.Contains(attribute.Key))
                                        {
                                            lookupId = GetLookupId(attribute.Key, attribute.Value, oldValueEntity, attribute.Key);
                                            fieldmetadata = GetFieldMetaData(attribute.Key, attribute.Value, oldValueEntity, attribute.Key);
                                            oldValue = GetFieldValue(attribute.Key, attribute.Value, oldValueEntity, oldValueEntity[attribute.Key].ToString());
                                        }
                                        newValue = GetFieldValue(attribute.Key, attribute.Value, newValueEntity, newValueEntity[attribute.Key].ToString());
    
                                        CreateAuditRecord(auditRecord.Attributes["auditid"], auditRecord.Attributes["createdon"], ((EntityReference)auditRecord.Attributes["userid"]).Name,
                                            auditRecord.FormattedValues["action"], attribute.Key, oldValue, newValue, attribute.Value, fieldmetadata, lookupId);
                                    }
    
                                    foreach (KeyValuePair attribute in attributeDetail.OldValue.Attributes)
                                    {
                                        if (!attributeDetail.NewValue.Contains(attribute.Key))
                                        {
                                            string fieldmetadata = "";
                                            string loookupId = "";
                                            string newValue = "";
    
                                            loookupId = GetLookupId(attribute.Key, attribute.Value, oldValueEntity, attribute.Key);
                                            fieldmetadata = GetFieldMetaData(attribute.Key, attribute.Value, oldValueEntity, attribute.Key);
                                            string oldValue = GetFieldValue(attribute.Key, attribute.Value, oldValueEntity, oldValueEntity[attribute.Key].ToString());
    
                                            CreateAuditRecord(auditRecord.Attributes["auditid"], auditRecord.Attributes["createdon"], ((EntityReference)auditRecord.Attributes["userid"]).Name,
                                              auditRecord.FormattedValues["action"], attribute.Key, oldValue, newValue, attribute.Value, fieldmetadata, loookupId);
    
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (InvalidPluginExecutionException ex)
                    {
                        MessageBox.Show($"An error occurred: {ex.Message}");
                    }
                    catch (FaultException ex)
                    {
                        MessageBox.Show($"An error occurred: {ex.Message}");
                    }
                    catch (TimeoutException ex)
                    {
                        MessageBox.Show($"An error occurred: {ex.Message}");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"An error occurred: {ex.Message}");
                    }
                }
            }

    Hopefully this is helpful

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 171 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 83

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans