How to Use Pre-Image in Power Automate

When working with Dynamics 365 or Power Platform, accessing historical data for record changes, such as pre-images, is essential for tracking updates. One approach to retrieve this pre-update information is by leveraging audit logs combined with Power Automate. This method can help you track the state of a record before an update, which is especially useful for auditing or troubleshooting.

In this blog post, we’ll explore how to retrieve pre-image data using Power Automate with the help of audit logs in Dynamics 365.

Why Use Pre-Images?

Pre-images are snapshots of a record before a plugin or workflow is executed. They help you compare the "before" and "after" states of records, which is crucial when maintaining accurate audit trails, debugging issues, or enforcing business rules.

While plugins typically handle pre-images in real-time, there may be cases where you need to access historical pre-images after the fact. This is where audit logs and Power Automate come in handy.

Step 1: Enable Auditing for Entities

Before you can retrieve any pre-image information, you must enable auditing on the entities you're interested in. Follow these steps:

  1. Navigate to Settings in Dynamics 365.
  2. Select Administration > System Settings.
  3. In the Auditing tab, ensure that auditing is enabled.
  4. Next, go to the specific entity (e.g., Account, Contact) you want to audit.
  5. Enable auditing for the entity and the fields you want to track.

Once this is done, Dynamics 365 will automatically capture changes made to the entity and store them in audit logs.

Step 2: Set Up Power Automate Flow

Now that auditing is enabled, you can create a Power Automate flow to retrieve audit data and extract pre-images.

  1. Create a New Flow

    • Go to Power Automate and create a new Scheduled Flow or Automated Cloud Flow depending on your needs.
    • For an automated flow, use the When a record is updated trigger for the desired entity.
  2. Retrieve Audit Logs Using Dataverse Connector

    • Add a new action called List records using the Dataverse connector.
    • Select the entity Audit from the dropdown list.
    • In the Filter Query, input a filter to fetch the relevant audit logs for the updated record. For instance, you can filter by the target entity's ID and event type (for updates):
      ObjectId eq [Record ID] and Operation eq 'Update'

  3. Parse the Audit Logs

    • Once the audit logs are retrieved, they contain valuable details about the old and new data.
    • Use the Parse JSON action in Power Automate to extract the pre-image data from the audit log’s Old Value field. This will allow you to see the record's previous state before the update.

    Example JSON Structure:

    json
    { "OldValue": { "field1": "Old Value 1", "field2": "Old Value 2" }, "NewValue": { "field1": "New Value 1", "field2": "New Value 2" } }
    first(outputs('Get_Last_Changed_Number_Using_Audit_Logs_2')?['body']?['value'])?['changedata']


    outputs('JSONBody_2')?['changedAttributes'][0]['oldValue']
  4. Store or Use Pre-Image Data

    • Once the old values (pre-image) are parsed, you can use this information in various ways:
      • Store it in another entity.
      • Send an email to notify someone of the changes.
      • Compare the old and new values for further analysis or validation.

Step 3: Test Your Flow

After setting up the flow, test it by updating a record in the entity for which you have enabled auditing. Power Automate will trigger, retrieve the corresponding audit log, and extract the pre-image data for you to view.

Benefits of Using Audit Logs and Power Automate for Pre-Image Retrieval

  • Historical Access: Audit logs provide a historical record of changes, so you can access pre-image data long after a change has been made.
  • Flexibility: Power Automate allows you to automate actions based on changes, making it easier to track and respond to updates in real-time.
  • Ease of Use: Once auditing is enabled, retrieving pre-image data is straightforward and doesn't require complex development.

Limitations and Considerations

  • Audit Log Storage: Audit logs can grow large over time, so ensure your storage capacity is sufficient.
  • Performance: Depending on the size of your data and frequency of updates, querying audit logs may introduce performance overhead.
  • Audit Retention Policies: Dynamics 365 may have retention policies for audit logs, so older logs might be purged if not managed properly.

Comments

Popular posts from this blog

Power Apps - Trigger Power Automate Flow On Approval Reassign

Power Pages - Multi Select Field Show Hide and Set Business Required Using Javascript