I'm working with a list that has 30+ fields and I need to capture what fields have been updated and output it to another list or table. Using the Get changes for an item action, is it possible to output only the fields that have changed and their value?
My first thought was to loop on or filter the output of the Get changes action, then somehow get the associated field values.
The Get Changes for an item or file action that can be used to determine which column has changed.
There is a Has Column Changed dynamic content that returns true if the field was changed or false if the field did not change.
Ellis
Right, but retrieving the field values is what I'm looking for.
"Using the Get changes for an item action, is it possible to output only the fields that have changed and their value?"
My hope is to avoid creating a large condition or switch statement.
This is possible and I can offer an approach to the problem - though other contributors with more experience than I should be able offer something better and more refined. But the following should get you started.
If we start with a simple SharePoint list called Routing - I want to know when any of the column values are changed; I want to receive an email showing me a list of the the columns that were changed along with their new values:
If you have versioning enabled on the list, you can view an item's version history and see which columns were changed and what the new values are:
The version history can be accessed by right clicking an item and then selecting Version history.
If we could get access this version history via Power Automate, and access to just that subset of data as shown in the version history then our job is done and we will have what we need. But I haven't found a way to do this or it is likely that this is not currently possible with Power Automate?
For now I will have to try to do this manually, and I will post a possible approach to the issue in the next post.
See also: Get the changes made to SharePoint Items with Power Automate
Ellis
Here is a demo flow along with some description of how it works. If you are able to get it to work, you can refine and merge a number of steps. Unfortunately it looks more complicated than it really is. The flow could work with almost any list.
If we start with a simple SharePoint list called Routing - I want to know when any of the column values are changed; I want to receive an email showing me a list of the the columns that were changed along with their new values. In the example below, the Folder and Preference column have been changed for item ID 14 and I receive an email with the changes that were made:
What I am trying to do is create an array that stores the SharePoint column names and a true/false value indicating if that column was changed or not. The array can be iterated over i.e. accessing each element of the array one by one. The array could be a JSON array but for simplicity I am going to use an array of strings:
Here is the high-level view of the flow:
Part 1: The trigger and get changes action:
Initialise an array variable varListOfColumnsChanged - which will store the SharePoint column names and whether the columns were changed or not (true or false):
We can get a list of all the columns and their changed states from the action Get changes for an item or a file (properties only). This data is given to us as a JSON object from the dynamic content ColumnHasChanged. I convert ColumnHasChanged to a string and store it in varStringData.
Split varStringData string at the comma delimiter ',' - creating an array as you see above, in the Compose Split String at comma action.
outputs('Get_changes_for_an_item_or_a_file_(properties_only)')?['body/ColumnHasChanged']
outputs('ColumnHasChanged_as_JSON_Object_Data')
split(variables('varStringData'),',')
Part 2: After the split action we need to clean up the column names in the array by removing escaped strings (eg. " double-quotes got replaced with \" when the JSON object was converted to string) as well as removing other unwanted characters such as the '{' and '}' characters. The high level view of this section is shown below:
The escaped strings \" are removed along with { and } characters as you see above in the green box.
replace(items('Apply_to_each'),'"','')
replace(replace(outputs('Sanitise_Item_Step_1'),'{',''),'}','')
A little more tidy up to separate the SharePoint column name and its change state value using split:
The split expression is:
split(outputs('Sanitise_Item_Step_2'),':')
The expression for Append to array variable varListOfColumnsChanged is:
first(outputs('Compose_Split_on_Colon'))
last(outputs('Compose_Split_on_Colon')
Note the colon between the two expressions:
Part 3: Finally we check the SharePoint column name (key) and its change state value, and if it was changed, we store this column name in a variable - which I will use to email a list of all the changed columns. The high level view of this section is shown below:
Example of the output that was generated:
The flow could work with almost any list, you would just need to change the List/Library names. In the example below I connected the flow to an Issue tracker SharePoint list:
I get the following results, though you will need to handle the the values in complex columns:
Hope this helps.
Ellis
outstanding post! thank you for taking the time to write all of this up.
I was able to finally refactor the flow to fewer steps. This makes the flow shorter and easier to understand. All credit to Identify which SharePoint item columns were updated in Power Automate by Tom Riha.
(1) Add the trigger and Get changes actions:
(2) Initialise a string variable for the email body:
(3) Convert the object HasColumnChanged to string, and split at the comma delimiter. We filter only the items that contain string ':true'
From:
split(string(outputs('Get_changes_for_an_item_or_a_file_(properties_only)')?['body/ColumnHasChanged']),',')
item()
contains
string(':true')
Sample output:
(3) Use json() to do all the clean-up! From "\"Title":true" to "Title" using json(item()):
Sample output:
(4) Constructs the email body containing the changed columns and their values:
body('Select')
item(): triggerBody()[item()]
Sample output:
Ellis
While this works great, it fails when a field is empty
Hi @msabau ,
At what point in the flow does it fail? We may be able to adjust the expressions to handle missing values.
Ellis
Solved it by following the guide you linked above. The only problem is that that tutorial gets rid of the values. For the moment I don't need them, but if i'll ever need to know the values that were changed, I'll try to figure it out on my own
Basically this is the error:
InvalidTemplate. Unable to process template language expressions in action 'Append_to_string_variable_2' inputs at line '0' and column '0': 'The template language expression 'triggerBody()[item()]' cannot be evaluated because property 'Email' doesn't exist, available properties are 'Ottienielemento_ID'. Please see https://aka.ms/logicexpressions for usage details.'.
It happens when I try to use triggerbody to append the value of the field to the string.
Not really sure how to fix it at the moment.
EDIT: I guess this happens because my trigger is a button in a powerapp, not the same trigger as your guide. I'll try fix it now
Hi @msabau .
Here is something to try. If you update the expression in question (step 4) to the following:
Old: triggerBody()[item()]
Updated: triggerBody()?[item()]
when the property is not found, a null value (empty) should be returned, and the flow should continue. For more info, see How to query JSON data using JSON notation with Power Automate❔❔❓
Ellis
____________________________________
If I have answered your question, please mark the post as Solved.
If you like my response, please give it a Thumbs Up.
Thank you for your reply. With the updated expression the flow doesn't stop, but it doesn't display values
Yes, because the expression triggerBody()?[item()] is evaluating to null or empty. This means that the property triggerBody()?['Email'] does not exist in the triggerBody().
Check your data for the correct property name.
Example, triggerBody()?['Author']:
{
"@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
"Claims": "i:0#.f|membership|ellis.karim@dev365.com",
"DisplayName": "Ellis Karim",
"Email": "ellis.karim@dev365.com",
"Department": null,
"JobTitle": "Business Manager"
}
triggerBody()?['Author/Email']:
ellis.karim@dev365.com
Ellis
____________________________________
If I have answered your question, please mark the post as Solved.
If you like my response, please give it a Thumbs Up.
Thank you for your reply. I'm wondering if there is a way to cycle through all the properties so that I don't have to type them all manually
I finally figured it out. Thank you for your tremendous help
If anybody else has the same issue I had or just want to greatly simplify the flow, instead of Sanitising the JSON, you could just do this:
User | Count |
---|---|
87 | |
37 | |
25 | |
13 | |
12 |
User | Count |
---|---|
117 | |
55 | |
36 | |
23 | |
21 |