62
Views
5
Comments
Solved
ForEach in ServerActions
Service Studio Version
11.54.47 (Build 63164)
  • How Outsystems works for multiples uses of a ServerActions (REST API Get)?
  • How to use a ForEach and save my Responses?

I have a Aggregate with Users ID, and with that list I want to put in a ForEach, to call a REST /get/userid

thats give me a Response of Users Information.

It is possible to use a ForEach to get all the Responses at once, and save in a Variable/Output like a UserInfo List type, so I can use as a Source in the Screen Table?

I don't know what would be the best practice to solve that, I'm trying something like the image below:



foreachrestget.png
2023-05-23 04-55-55
Wasimkhan Syed Abuthahir
Solution

Hi,

Here is the sample of appending usersinfo to list

Reegards,

Wasimkhan S

2024-01-04 09-21-21
Venkatesaiya

Hi bruno Jacby,
Use list Append function in forEach to append the data one by one.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Bruno,

REST methods are called synchronously, so if you call a REST method in a For Each, the next iteration won't commence until the REST method has finished.

As others have commented, you can add each response to a List by using a ListAppend in each iteration. Note however that depending on the number of items in the list, this can take quite some time, as REST methods are typically much slower than server actions.

Ideally, if you have control over the REST API you are calling, the REST method should have a list of Ids as input, and a list of results as output. In that case you only need to call the API once, which will be much faster.

2023-05-02 10-33-02
Madhuri Patil

Hi @Bruno Jacby,

I also agree with @Kilian Hekhuis comment.
Calling API multiple times and that is also in for loop is not good practice.
Check with API Input output structure if it is supporting multiple Input output structure 
Then create array of userID and send it to that API so the number of API calls get reduce.

Also for better performance create batch of 15 to 25 userId at a time according to API performance 
and add exception handling as well as handle the  timeout operations .

Thanks,
Madhuri

2024-01-22 11-37-37
Bruno Cabús Jacby

Thank you all @Madhuri Patil, @Kilian Hekhuis, @Wasim Khan, @Venkatesaiya

solved


Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.