web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Apply to each inside A...
Power Automate
Answered

Apply to each inside Apply to each doubles update action

(0) ShareShare
ReportReport
Posted on by

Hello,
I am trying to create a flow which will compare data from an Excel file with data from a SharePoint list and the update the records in Sharepoint. The common items between the two lists are the Ticket and the Status. 

The comparing part of the flow is ok, but when I'm trying to update the Status in SharePoint, because of the multiple Apply to each actions, the flow tries to update the same line multiple times. 

Basically, what I'm trying to do is: for each different item, where the Ticket is the same (when I'm adding the condition, the second apply to each is added automatically), update the status. 
I've attached a screenshot with this part of the flow.

 

Please, can you help me with an advice on how to improve this flow?
Thank you!





 

Categories:
I have the same question (0)
  • Matthy79 Profile Picture
    4,180 Super User 2024 Season 1 on at

    If you have already a list of items you want to update, why do you need a condition inside the loop. It is never a good idea to have a condition inside an apply to each. Normally you should filter everything before.

     

    The second apply to each is automatically created because you compared to a property inside an array. So power automate says “I see you want to compare the value to each property inside that array” so it automatically creates that second “apply to each”.

  • Community Power Platform Member Profile Picture
    on at

    Thank you for your message.
     As you've said, I filter everything before, but the reason for which I've added the condition was that I needed to know exactly which item needs to be updated.

    For example List 1 has Ticket A with Status Closed and List 2 has Ticket A with Status Open. Therefore, I thought I should have a condition (Ticket In List 1 = Ticket in List 2), to be able to update its Status. 
    Is it wrong? An apply to each would be enough?

  • Matthy79 Profile Picture
    4,180 Super User 2024 Season 1 on at

    Normally it is the best way to use select and filter array and after that apply to each result and do the updates. If you provide sample data of list 1 and list 2 and desired updates/adds/deletes I will see what I can do.

  • Community Power Platform Member Profile Picture
    on at

    I've added two sample lists. The first one - source list, comes as an Excel and from there I get my up-to-date data. The second one - target list, is in fact a Shrepoint list (which I cannot provide, so I've added an Excel file) and it needs to be updated: 
    - if the ticket exists in List 2, check if the status is the same. If yes, no action. If no, update the status with the one from List 1. 

    - if the ticket doesn't exist in List 2, create it there

    Thank you very much for making the time to help me! 

  • Verified answer
    Matthy79 Profile Picture
    4,180 Super User 2024 Season 1 on at

    It's always easier to work with a JSON. It is not important that the values in the JSONs are real but now I provide a sample and you will have to change a lot because the field names are not the same. I hope you can manage it from here or someone else takes it from here if you are having problems doing the changes.

     

    1.) Transform Sharepoint list to xml

    2.) Select all Ticket ids and Status from Excel-List and Status from Sharepoint list

    3.) Filter for changes (Status excel <> Status Sharepoint)

    4.) Filter new (Status Sharepoint = null)

    5.) Now you can apply to each (Step 3 and Step 4) to update or create.

     

    Here is a sample code of the flow you can copy and paste (Press ctrl+v inside the "My clipboard" section of adding a new action) into a dummy flow to see how it works.

     

    {"id":"2ba6aa2d-f31f-4276-aef0-56bfc40ba42d","brandColor":"#8C3900","connectionReferences":{"shared_sharepointonline":{"connection":{"id":"/providers/Microsoft.PowerApps/apis/shared_sharepointonline/connections/shared-sharepointonl-b959cb53-5ecb-4b41-805f-06e7795a0bd2"}},"shared_converterbypower2apps":{"connection":{"id":"/providers/Microsoft.PowerApps/apis/shared_converterbypower2apps/connections/shared-converterbypo-168dd966-d321-48d6-ae65-2bb3363fc29f"}},"shared_powerappsforappmakers":{"connection":{"id":"/providers/Microsoft.PowerApps/apis/shared_powerappsforappmakers/connections/shared-powerappsfora-72ad6d4c-b73e-4db7-a2ac-66bed864efe8"}},"shared_flowmanagement":{"connection":{"id":"/providers/Microsoft.PowerApps/apis/shared_flowmanagement/connections/shared-flowmanagemen-276a16fd-e558-4db2-8c1d-8f153142d8f4"}},"shared_office365":{"connection":{"id":"/providers/Microsoft.PowerApps/apis/shared_office365/connections/1597557c9920416e9a930edfee990646"}},"shared_commondataserviceforapps":{"connection":{"id":"/providers/Microsoft.PowerApps/apis/shared_commondataserviceforapps/connections/62947705b1a746319ca1e59aae985afc"}}},"connectorDisplayName":"Control","icon":"data&colon;image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzIiIGhlaWdodD0iMzIiIHZlcnNpb249IjEuMSIgdmlld0JveD0iMCAwIDMyIDMyIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPg0KIDxwYXRoIGQ9Im0wIDBoMzJ2MzJoLTMyeiIgZmlsbD0iIzhDMzkwMCIvPg0KIDxwYXRoIGQ9Im04IDEwaDE2djEyaC0xNnptMTUgMTF2LTEwaC0xNHYxMHptLTItOHY2aC0xMHYtNnptLTEgNXYtNGgtOHY0eiIgZmlsbD0iI2ZmZiIvPg0KPC9zdmc+DQo=","isTrigger":false,"operationName":"Scope_compare_lists","operationDefinition":{"type":"Scope","actions":{"Compose_List_1":{"type":"Compose","inputs":[{"Ticket":1,"Status":"Open"},{"Ticket":2,"Status":"Closed"},{"Ticket":3,"Status":"On hold"},{"Ticket":4,"Status":"Open"},{"Ticket":5,"Status":"Open"},{"Ticket":6,"Status":"Open"},{"Ticket":7,"Status":"Open"},{"Ticket":8,"Status":"Closed"},{"Ticket":9,"Status":"On hold"},{"Ticket":10,"Status":"New"}],"runAfter":{}},"Compose_List_2":{"type":"Compose","inputs":[{"Ticket":1,"Status":"Open"},{"Ticket":2,"Status":"Open"},{"Ticket":3,"Status":"On hold"},{"Ticket":4,"Status":"Open"},{"Ticket":5,"Status":"On hold"},{"Ticket":6,"Status":"Open"},{"Ticket":7,"Status":"Open"},{"Ticket":8,"Status":"Closed"},{"Ticket":9,"Status":"Open"}],"runAfter":{"Compose_List_1":["Succeeded"]}},"Compose_xml_list2":{"type":"Compose","inputs":"@xml(json(concat('{ \"root\": { \"data\": ', outputs('Compose_List_2'), ' }}')))","runAfter":{"Compose_List_2":["Succeeded"]}},"Select_delta":{"type":"Select","inputs":{"from":"@outputs('Compose_List_1')","select":{"Ticket":"@item()['Ticket']","Status":"@item()['Status']","OldStatus":"@first(xpath(outputs('Compose_xml_list2'), concat('root/data[Ticket=\"', item()['Ticket'], '\"]/Status/text()')))"}},"runAfter":{"Compose_xml_list2":["Succeeded"]}},"Filter_changes":{"type":"Query","inputs":{"from":"@body('Select_delta')","where":"@not(equals(item()['Status'], coalesce(item()['OldStatus'], item()['Status'])))"},"runAfter":{"Select_delta":["Succeeded"]}},"Filter_new":{"type":"Query","inputs":{"from":"@body('Select_delta')","where":"@equals(item()['OldStatus'], null)"},"runAfter":{"Filter_changes":["Succeeded"]}},"Apply_to_each_change":{"type":"Foreach","foreach":"@body('Filter_changes')","actions":{"Compose_dummy_text_for_update":{"type":"Compose","inputs":"Update Ticket @{items('Apply_to_each_change')['Ticket']} from @{items('Apply_to_each_change')['OldStatus']} to @{items('Apply_to_each_change')['Status']}","runAfter":{}}},"runAfter":{"Filter_new":["Succeeded"]}},"Apply_to_each_new_ticket":{"type":"Foreach","foreach":"@body('Filter_new')","actions":{"Compose_dummy_text_for_create":{"type":"Compose","inputs":"Add Ticket @{items('Apply_to_each_new_ticket')['Ticket']} with status @{items('Apply_to_each_new_ticket')['Status']}","runAfter":{}}},"runAfter":{"Apply_to_each_change":["Succeeded"]}}},"runAfter":{}}}

     

  • Community Power Platform Member Profile Picture
    on at

    Thank you!! I've applied your solution to my flow and it worked like a charm! 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 525 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 324 Moderator

#3
abm abm Profile Picture

abm abm 232 Most Valuable Professional

Last 30 days Overall leaderboard