Loop / Iterate through each record in attribute

Loop / Iterate through each record in attribute

  

I have several different API key's that I must iterate through, performing the same set of actions with each as I iterate through them. I have ran the logic without the Aggregate, the first For Each and the Assign action and everything works perfectly as it should. However, I can only input one API key and would have to copy this action over and over again with a new API key each time to have it work like I would want it to; which I have tried and it also works. 

However, what I would like to do is create a single server action that grabs the Entity Attribute that contains a list of API keys, take the first API key, Assign it to the API key parameter in the PostOrder action, perform the list of actions required afterward and then cycle through the rest of the API keys until there are no more and then End. 

Like I've mentioned before, the actions being performed after the API key has been set are good, I just can't for the life of me figure out how to iterate through all of them and assign them each time I do.

Any assistance would be greatly appreciated! :-)


Tbh, I don't understand what the issue is.

ypu are saying, with 1 key it goes well,

with the aggregate you cannot solve it?


Hi Chris,

From your description I'm unable to tell what you are trying to do, and why it doesn't work as you expect. What is an "API Key"? What is the "PostOrder action"? Etc.

Hello Kilian & J,

The API key is almost like the password needed in order to access information for a particular client. Each API key is intended for one client and one client only. I have multiple clients I have to access information from and in order to pull this data I have to poll an API endpoint URL via a POST method and pass along the API key in the headers for each client I would like to pull this data from. 

The Post Order action is an Outsystems generated REST API action that consumes the data from the endpoint URL. The end point URL is always the same for each client, but the API key is different for each client, and allows me to access only a single clients data via this endpoint URL. All the PostAction does is poll this URL for data, but it differentiates each client and each clients data by their API key.

Do you understand what I am trying to do now? I will rephrase it in a description below. 

- I have 100 clients who submit orders into an external form. The data for these orders is held in a JSON file on an external server until I pull them into my Outsystems platform via an API call using the PostOrder action.  

Step 1: Each time I get a new client, I login to my Outsystems application and add the clients name along with their API key to an Entity called Clients. The API key acts as a password so that I can pull the data for this specific client via a REST API.

Step 2: At the end of the day I login to my Outsystems application and go to the Order Pulling page. I then click a button that says "Pull New Orders". What this does is trigger the PostOrder action, passes in a single API key (password specific to a single client) and then pulls the data for the orders submitted that day for that client via an external form. 

Step 3: I have to go in and change the API key for the PostOrder action so that it pulls the order data for the next client. I then have to do this for all 100 clients in order to get the order data for that day. 

I am trying to only have to "Pull New Orders" once, allowing the action I build to grab an API key, give it to the PostOrder action, grab the data of that specific client, save it to the proper Entity within Outsystems (which it already does), and the loop back and start over... Grab another API key, give it to the PostOrder action, grab the data of that specific client, save it to the proper Entity within Outsystems... Repeat, repeat, repeat until there are no more API keys to grab and at that point End and give me a feedback message of the results. 

Again, any advice that can be given on this matter will be very much appreciated :-D

Hi Chris,

If you just want to pull data for every client, you query the Client Entity, For Each over the result set, and call PostOrder within the For Each loop. Since this is pretty basic, I assume you've tried that? If so, what went wrong?

Secondly, if you want to automate the pulling instead of pressing a button, I'd advise you to create a Timer. You can schedule it at an appropriate time every day, and you do not need to press a button.

Kilian,

Before I used the Aggregate I used an SQL query. Below is a screenshot, if you can view it.

By the way; I like your automation suggestion :-D 

Kilian Hekhuis wrote:

Hi Chris,

If you just want to pull data for every client, you query the Client Entity, For Each over the result set, and call PostOrder within the For Each loop. Since this is pretty basic, I assume you've tried that? If so, what went wrong?

Secondly, if you want to automate the pulling instead of pressing a button, I'd advise you to create a Timer. You can schedule it at an appropriate time every day, and you do not need to press a button.


Oh yes, you also asked what went wrong... Well, I kept getting an error that would not allow me to proceed when using SELECT * FROM {Clients}.[Key]. But it would allow me to use SELECT * FROM {Clients}, although, I still couldn't select just the key. Any idea on that one? This is probably a really simple question, I'm sure.

Hi Chris,

EDIT: Sorry, I didn't read your reply, just looked at the error. You are already using an aggregate. Good!

1) I'd advise you to use Aggregates instead of SQLs. There's absolutely no need for SQL in your case (and there almost never is, except for advanced stuff).

2) Since you obviously are not very versed in SQL*, see point 1 :).

*What you wrote is invalid SQL. Since this is not a SQL forum, and OutSystems has better ways of doing what you tried to do, I'll refrain from spelling out what's wrong.

Kilian Hekhuis wrote:

Hi Chris,

EDIT: Sorry, I didn't read your reply, just looked at the error. You are already using an aggregate. Good!

1) I'd advise you to use Aggregates instead of SQLs. There's absolutely no need for SQL in your case (and there almost never is, except for advanced stuff).

2) Since you obviously are not very versed in SQL*, see point 1 :).

*What you wrote is invalid SQL. Since this is not a SQL forum, and OutSystems has better ways of doing what you tried to do, I'll refrain from spelling out what's wrong.


Kilian,

Thank you for your criticism and advice. You are correct in assuming I am not well versed in SQL. However, I would like to find out exactly how you would use the Aggregate to obtain these results, since I also obviously have not done so properly :-)

Thanks again!

Question comes to mind if you actually watched the online tutorials?

They are quite good and will tell you most stuff.


J. wrote:

Question comes to mind if you actually watched the online tutorials?

They are quite good and will tell you most stuff.


Yes, and I gained a lot of my initial knowledge of the Outsystems platform from the online tutorials. Nevertheless, I have been able to come to a good solution with what I was seeking help on. I just had to lay out the logic in a different way. 

Thanks for your comments though :-)

Hi Chris,

It seems you've already found the solution, but I'm going to answer your question of the previous post anyway :). Since it seems you have a single Entity you want to loop over, the simplest way to create an aggregate is to drag the Entity from the Data tab onto the action canvas. SS will create a query for you that's not much more than a "SELECT * FROM Table", but you can't beat it for quickness :). To loop over it, you add a For Each widget to the canvas, and assign the List of the Aggregate to the For Each's Record List property. That's it!

Hi Kilian,

AH!! Perfect! And then I would need to assign the specific Attribute grabbed from the Aggregate to a local variable, correct? So this way, each time it loops through it will grab the correct Attribute from the Aggregate :-)

So, basically, you you're saying that I should set it up the exact same way I had it setup when I sent my initial message? lol. 

However, you're correct, sir. I can't believe I did this, but when I initially did it the way you're suggesting I made the Aggregate grab the Attribute by Id. I removed that and everything works correctly! 

Thank you so much for your patience with me. That was a silly move on my part.

Hi Chris,

There's no need for a local variable, unless you're doing calculations are conditional assignments with it. You can pass the output of the aggregate (e.g. GetStuff.List.Current.CustomerApiKey) direclty as input to the PostOrder action.