How to – Delete Audit data by entity / table, date, user access, and specify retention duration – updates in Dataverse (Dynamics 365 / CRM)


Recently we saw a new update in our sandbox environments with regards to Audits.

Login to Power Platform Admin Center, select an environment.

We can see a new section Auditing added.

Click on Delete logs allows us to –

  • Delete logs by Table

We can select the tables there.

  • Delete only access logs data.
  • Delete logs up to the selected date.

And now we can also manage or specify the retention duration for the audit log.

These are very useful updates to the Audit feature in the platform which more or less have been similar since CRM 3.0 days.

Check other posts on Audit – 

https://nishantrana.me/2021/08/31/audit-entity-table-few-key-points-dynamics-365-power-apps/

https://nishantrana.me/2021/05/17/how-to-export-the-audit-history-values-from-dynamics-365/

https://nishantrana.me/2018/10/08/using-kingswaysofts-cds-crm-source-component-to-get-audit-information-in-dynamics-365-ce-ssis/

How to – Use RetrieveAttributeChangeHistoryRequest to get audit data for an attribute (Dataverse/ Dynamics 365/ CRM) – Nishant Rana’s Weblog

Hope it helps..

Advertisements

How to – Use RetrieveAttributeChangeHistoryRequest to get audit data for an attribute (Dataverse/ Dynamics 365/ CRM)


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

Hope it helps..

Advertisements
%d bloggers like this: