top of page
Writer's pictureYash Agarwal

Get Record Change History of audited entities in Power Automate

In this #PowerShot, we will take a look at a setup that allows to get the audit trail of the changes made to an audited field on an audited entity from CDS ('Project Oakdale'). We will use the 'RetrieveRecordChangeHistory' function and get the audit trail and extract the information from there in Power Automate.


Let's Get Started!


Currently, Power Automate supports scenarios to trigger a flow when a record is updated in CDS ('Project Oakdale'). But it does not return the values that were there on the fields on which the change was made. There might be use cases where this information is required and is important for a process to progress. For example, we need to notify a user when a record they own is assigned to another user. In this case, we will enable auditing for the field 'ownerid', the entity on which we need implement this functionality. Once auditing is enabled, we can use the 'RetrieveRecordChangeHistory' function to get the change log and track the changes.

 

Register an app in Azure AD


Step 1: Logon to the Azure Portal and switch to Azure Active Directory.


Step 2: Locate "App Registrations" and create a new app registration.


Step 3: Once the app registration is complete, go to the API permissions of the app registration and click "Add a Permission". From the options, select "Dynamics CRM" and click Add Permission. You should now be able to see the permission to the Dynamics CRM added to the APP Registration. Now click the "Grant Admin Consent" and provide the org consent.

Step 4: Navigate to the "Certificates & Secrets" pane and create a secret that does not expire (as it will be used in Power Automate) and note the secret value.


Step 5: Navigate to the "Overview" pane and note the app id from the app registration (as it will be used while creating the application user).

 

Create an Application User


Step 1: If you have a model driven app setup, open the app and navigate to Advanced Settings by clicking the gear icon. In the advanced settings, navigate to Security and then select Users.


Step 2: Click the '+' sign to open the new user form and then click on the User: User to change the form type to 'Application User'. In the Application user form, enter the app Id from the app registration of the previous section. Create a non user account in the O365 admin center and provide the email address in the primary email field.

Step 3: Once the details are provided, click Save and Close.

 

Enable Auditing


Step 1: If you have a model driven app setup, open the app and navigate to Advanced Settings by clicking the gear icon. In the advanced settings, navigate to Auditing and then Global Audit Settings.


Step 2: In the Global Audit Settings, enable the Start Auditing, Audit User Access and enable auditing for Common Entities.

Step 3: Once done, click on Entity and Field Audit Settings, and search for the entity on which you need to enable the auditing (Claims in this example).


Step 4: On the entity settings, enable auditing for the entity and then in the fields, search for the 'ownerid' field and verify that the field is being audited.

 

Power Automate


Step 1: Trigger: When a record is updated: to trigger the flow when the record is updated. Select the attribute filter and point to the 'ownerid' field to run this flow only when that particular field changes.


Step 2: Action: Send an HTTP Request: to invoke the REST API and call the 'RetrieveRecordChangeHistory' function and get the details of what changed on the record.


For the URI, use the URL: https://yourORG.crm.dynamics.com/api/data/v9.0/RetrieveRecordChangeHistory(Target=@Target)?@Target={%22EntityLogicalNameHere%22:%22SelectRecordIDFromTriggerHere%22,%22@odata.type%22:%22Microsoft.Dynamics.CRM.EntityLogicalNameHere%22}


Select the Active Directory OAuth method for authentication and then provide the Tenant ID, Audience, Client ID and the secret from the App registration created in the first section.

Step 3: Action: Get Record: To get the user record to whom the claim was assigned to (before the change was made). Expression: first(body('HTTP')?['AuditDetailCollection']?['AuditDetails'])?['OldValue']?['_ownerid_value']


Step 4: Action: Get Record: To get the user record to whom the claim is now assigned to. Expression: first(body('HTTP')?['AuditDetailCollection']?['AuditDetails'])?['NewValue']?['_ownerid_value']

Step 5: Action: Send an email: To send an email notification to the users regarding the change.

 

Setup in Action

 

In this post, we saw how to configure a setup to retrieve the audit trail and get the details of the changes to audited fields on audited entities of CDS ('Project Oakdale') in Power Automate. This is useful for change tracking and cases like when a user needs to be notified when the record assignment is updated and etc.


I hope you found this interesting and it helped you. Thank you for reading!

Recent Posts

See All

Comments


bottom of page