Original post: Retrieve data from a deleted entity with Microsoft D365 Power Automate
When records are deleted in Power Apps, relevant data for subsequent processes is often lost. This article shows how to retrieve deleted information using auditing - a useful workaround for Power Automate flows.
The use case is to retrieve the Opportunity ID from a quote after it has been deleted.
The challenge here is that the flow is triggered after the deletion process, which means that the data is no longer available for further process steps. The workaround is to enable auditing on the table. This will log all changes, including deletions. The following link shows how to enable auditing. Come back when you are finished:
https://learn.microsoft.com/en-us/power-platform/admin/enable-use-comprehensive-auditing
As shown in the screenshot, select 'Initialize Variable' as the first step. Select 'String' as the data type and add triggerBody() as the value.
Add a new step "List Rows".
Table name: Audits
Select columns: _objectid_value, changedata, changedata
Filter rows: _objectid_value eq 'triggerOutputs()?['body/quoteid']'
Sort By: createdon desc
Add a new step "Apply to each" and add outputs('List_rows_3')?['body/value']
The next step is "Parse JSON" with the Content items('Apply_to_each_3')?['changedata']
The data output during the "List Rows" testing can be copied into the "Generate Sample". In this case, the following text is generated:
Add as next step "Apply to each" with body('Parse_JSON')?['changedAttributes']
There are two ways to get the Opportunity ID. Normally it is always found in oldValue. This means that oldValue alone should be sufficient as a condition. That's where we find the Opportunity ID we need. The second possibility might be logicalName. Just look at the data and see where you can find the ID. That's why we've added a new step Condition.
The Condition contains an OR with:
items('Apply_to_each_4')?['logicalName']
items('Apply_to_each_4')?['oldValue']
If the Opportunity ID is found, we save it into a string variable. The found Opportunity ID is in the following format, which means we need to cut out the "opportunity,"
opportunity,5341c6c2-b539-4f0e-983c-22735e6c774f
We use the following command to remove the unnecessary part:
replace(items('Apply_to_each_4')?['oldValue'],'opportunity,','')
In this use case, the Opportunity ID can now be used to find all other quotes in which the deleted quote was present.
I hope the instructions were able to help some of you.