cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
jernejp
Helper IV
Helper IV

Nested Loops and API calls

Hi,

 

I'm building an application to add employee activities to our ERP, and then assign attendees to those activities.

First I have to make a call to our ERP to add an activity, which then returns the Activity ID from ERP. Then using that, I'm supposed to add all of the attendees to the activity.

Theres multiple activities that can have multiple attendees.

During the process, I'm saving all of the Activities to a table in my database, and Attendees to another table (and they're connected by activity_id field in Attendees table.

 

Then I make a call to get AllActivities into a collection to loop through.

 

This is what I currently have:

 

ForAll(
    AllActivities;
    Collect(
        AddedActivity;
        'My API'.AddActivitiesToBusinessPartner(
            {
/* API CALL HERE */ } ) ) )

 

 

My database schema is as follows, and below is the collection that gets created:

 

 

dbschema.PNG

added1.PNGadded2.PNG

 

What I can't figure out is: how would I go about nesting another ForAll loop in there, that would make the other API call to add attendees to the activity?

So basically:

  1. add activity
  2. get ID from the ERP
  3. loop trough database and see if there's attendees
  4. add attendees to ERP

 

14 REPLIES 14

Hi @jernejp ,

Do you want to use the AddedActivity (rather than AllActivities) within yout second loop?

 

Based on the needs that you mentioned, I afraid that a triple nested loop could not achieve your needs. Please consider take a try with above solution I provided.

 

On your side, you could consider create a ForAll loop firstly to retrieve all added activities using the following formula:

 

ForAll(
    AllActivities;
    Collect(
        AddedActivity;
        'MyApi'.AddActivitiesToBusinessPartner(
            {
	    }
        )
    )
)

Then add another ForAll loop to go through the all attendees that attended the activity, and add them to above retrieved added activities using following formula:

 

 

ForAll(
         AddedActivity,
         ForAll(
                Filter(
                        '[dbo].[attendees]';
                        activity_id = AddedActivity[@id]
                );
                'MyApi'.AddAttendantsToAnExistingActivity(
                {
                  ....
                }
              )
         )
)

 

So your whole formula should look like below:

ForAll(                   /* <-- Seperated ForAll Loop */
    AllActivities;
    Collect(
        AddedActivity;
        'MyApi'.AddActivitiesToBusinessPartner(
            {
	    }
        )
    )
);;
ForAll(                          /* <-- Nested ForAll Loop */
         AddedActivity;
         ForAll(
                Filter(
                        '[dbo].[attendees]';
                        activity_id = AddedActivity[@id]
                );
                'MyApi'.AddAttendantsToAnExistingActivity(
                {
                  ....
                }
              )
         )
)

Please take a try with above solution, check if the issue is solved.

 

Best regards,

 

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @v-xida-msft 

 

The problem is, that in my AllActivities, the ID of activity would be (for example) 60 (as it's taken from my database), but from ERP, the response is an id of (for example) 4000231. So If I understand correctly, the second (nested) loop wouldn't work, because 'activity_id' is not the same as AddedActivity[@id].

Could I add a column to AddedActivity in the first loop, and add AllActivities[@id] to it? How would I go about that? I think if I can add the database's ID of activity to AddedActivity, then the second loop would be finished, right?
Hope I'm making sense

Hi @jernejp ,

Yeah, you are right. I have made a test on my side, please consider take a try with the following formula:

ForAll(                   /* <-- Seperated ForAll Loop */
    AllActivities;
    Collect(
        AddedActivity;
        {
ActivityID: AllActivities[@id], /* <-- add a column to store the id value from the AllActivities */
ResponseRecord: 'MyApi'.AddActivitiesToBusinessPartner( { ... } )
} ) );; ForAll( /* <-- Nested ForAll Loop */ AddedActivity; ForAll( Filter( '[dbo].[attendees]'; activity_id = AddedActivity[@ActivityID] ); 'MyApi'.AddAttendantsToAnExistingActivity( { .... } ) ) )

Please consider take a try with above formula, check if it could help in your scenario.

 

Best regards,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Oh my god, THANK YOU SO MUCH! IT WORKS!

[EDIT] Removed the post, as it was working. The problem was with my internet connection apparently. 

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

Power Apps Community Call

Monthly Power Apps Community Call

Did you miss the call?? Check out the Power Apps Community Call here!

secondImage

Experience what’s next for Power Apps

See the latest Power Apps innovations, updates, and demos from the Microsoft Business Applications Launch Event.

Power Platform ISV STudio

Power Platform ISV Studio

ISV Studio is the go-to Power Platform destination for ISV’s to monitor & manage applications post-AppSource publish.

Top Solution Authors
Top Kudoed Authors
Users online (36,716)