azure-logic-apps

Nested For Each creating duplicates in Logic App


I have a problem where my nested For each actions are creating duplicates, and I am not sure how to correct it.

Scenario:

From system 1 we have multiple images coming into the Logic App. It needs to add them onto a record in D365 that can have multiple images already. I'm trying to set up the Logic App so that it will check the images on D365 and only add an image if that image doesn't already exist.

Example of how the images in the http trigger come in- enter image description here

Issue:

I have tried having a For each action for the images on D365 nested in the For each for the images from system 1, but this created duplicates on D365.

Summary of what happens:

enter image description here

I have also tried swapping it around so the For each action for the images from system1 is nested in the For each for the images on D365, but it also creates duplicates on D365.

Summary of what happens:

enter image description here

Any ideas would be appreciated!


Solution

  • Split your Logic App workflow into two logical parts:

    1. The first part will generate a list of images that need to be added to the record in D365.
    2. The second part will add the images from the generated list to the record in D365.

    Your question doesn't clarify how exactly multiple images are coming into the Logic App and how you are checking if they exist in D365.

    If you have a container with images coming into the Logic App and another container with images that already exist in D365, you can easily generate a new container with the items that exist in the first container but not in the second. You can use collection functions such as contains or intersection; you can also use the Filter array action - depending on your needs and the format in which you receive the data.

    In the second part of your Logic App, use a new For each action to add all images from the result container to the record in D365.