use foreach for rest API

  

i get error when im using foreach, im trying to create data from my API, I tried to create data without foreach but I only get 1 data like the picture below, so im trying to use foreach, anyone can help me?

Calvin Adi wrote:

i get error when im using foreach, im trying to create data from my API, I tried to create data without foreach but I only get 1 data like the picture below, so im trying to use foreach, anyone can help me?


Hi Calvin,

you are using it wrongly ,please refer this link for how you should use it 

https://success.outsystems.com/Documentation/10/Developing_an_Application/Implement_Application_Logic/Use_a_For_Each_to_Iterate_a_List


Regards

-PJ-

Hi Calvin Adi,

Please refer the link which is mentioned by Pramod Sir.

Your flow should be like this.


Hope it helps.


Regards,

Benjith Sam

Benjith Sam wrote:

Hi Calvin Adi,

Please refer the link which is mentioned by Pramod Sir.

Your flow should be like this.


Hope it helps.


Regards,

Benjith Sam

um thanks for the help, it works but my data still kinda confusing now


the real data is like this 

whats wrong here?

Hi Calvin Adi,


Everything looks correct as per my understanding. 

Guess: set unique index to the ToDos entity with Title attribute if this attribute value is unique in your case.

How much record is getting stored in your table/entity from the api response?

How you are implementing the whole flow. I mean when are you calling the api to entity data insertion flow.


If you can provide more context, it will be helpful to understand the case.


Kind Regards,

Benjith Sam

Benjith Sam wrote:

Hi Calvin Adi,


Everything looks correct as per my understanding. 

Guess: set unique index to the ToDos entity with Title attribute if this attribute value is unique in your case.

How much record is getting stored in your table/entity from the api response?

How you are implementing the whole flow. I mean when are you calling the api to entity data insertion flow.


If you can provide more context, it will be helpful to understand the case.


Kind Regards,

Benjith Sam


it should be 200 records, but it only stored the first record.

I don't know if title attribute is unique or not. I use https://jsonplaceholder.typicode.com/todos to test rest API

this is in server actions

this is in preparation


I got internal error when opening the browser actually because the preparation is wrong, but right now I want to get the correct data from the API first

Calvin Adi wrote:

Benjith Sam wrote:

Hi Calvin Adi,


Everything looks correct as per my understanding. 

Guess: set unique index to the ToDos entity with Title attribute if this attribute value is unique in your case.

How much record is getting stored in your table/entity from the api response?

How you are implementing the whole flow. I mean when are you calling the api to entity data insertion flow.


If you can provide more context, it will be helpful to understand the case.


Kind Regards,

Benjith Sam


it should be 200 records, but it only stored the first record.

I don't know if title attribute is unique or not. I use https://jsonplaceholder.typicode.com/todos to test rest API

this is in server actions

this is in preparation


I got internal error when opening the browser actually because the preparation is wrong, but right now I want to get the correct data from the API first


Hi Calvin,

You are missing one important step here.

Take one Record variable of your entity.

After foreach loop, assign value from REST API to the record variable, and you need to pass that record variable as source to create statement.




Hope this helps.


Thanks and Regards,

Suraj Borade

Hi Suraj,

This is not always required if the record in the for each list is same as your entity type where you creating the record. we usually do this when the type is different or we want to add any additional value to or calculation.


Regards

-PJ-

Hi Calvin Adi,

Observation:

Screen: 

Preparation:-

1) IF widget condition: GetTodos2.Response.Current.Id = 5 or GetTodos2.Response.Current.Id = 19

2) IF widget condition: GetTodos2.Response.Current.Id = 20


- Observation: some of the api record is not getting inserted in the table because of this I introduced two IF condition and the second IF will break the loop. Below is the occurred error and the API data causing error.

Error: String or binary data would be truncated. The statement has been terminated.


Final List with populated record (intentionally restricted up-to 18 records for testing purpose).

Note: I'm not able to understand why some of the API record is causing error during entity data create operation.


Kind Regards,

Benjith Sam

Benjith Sam wrote:

Hi Calvin Adi,

Observation:

Screen: 

Preparation:-

1) IF widget condition: GetTodos2.Response.Current.Id = 5 or GetTodos2.Response.Current.Id = 19

2) IF widget condition: GetTodos2.Response.Current.Id = 20


- Observation: some of the api record is not getting inserted in the table because of this I introduced two IF condition and the second IF will break the loop. Below is the occurred error and the API data causing error.

Error: String or binary data would be truncated. The statement has been terminated.


Final List with populated record (intentionally restricted up-to 18 records for testing purpose).

Note: I'm not able to understand why some of the API record is causing error during entity data create operation.


Kind Regards,

Benjith Sam


thanks for the reply, i tried it but I'm still getting the same data 

can you look at my oml what's the problem ?

Hi Calvin Adi,

I am not able to open the attached .oml file because of the old service studio installed in my office laptop. If it's convenient for you please share the screen shot. Other than that in my previous post I did put up all the step by step screenshots of my implementation. I'm happy to help.


Kind Regards,

Benjith Sam

my screen got the real data because it using value from the API

this is the preparation page, nothing wrong here

this is the local variable Todo

i follow yours here

the entities data still weird


Hi Calvin Adi,

I observed your screenshot and everything looks correct. I re-implemented it and its working fine and in my case data got successfully populated in the entity.



Kind Regards,

Benjith Sam

Hi Calvin Adi,

Please go through the attached .oml solution. 

Hope it helps you.


Regards,

Benjith Sam

Benjith Sam wrote:

Hi Calvin Adi,

Please go through the attached .oml solution. 

Hope it helps you.


Regards,

Benjith Sam

thanks for ur oml, its works, but I still want to know the problem with my oml, I tried to use the steps that you give, and still no success. maybe its the version problem?


Hi Calvin,

Your GetTodosData entity (I strongly advise you to change this name) does not have an Identifier Attribute defined. Although not necessarily incorrect, in most cases it is not what you would want, especially since you actually have an attribute called Id.

You can define the Id attribute as the Identifier by right-clicking on the Id attribute and selecting the first option of the context-menu: Set as Identifier.

You will immediately see that your entity now has all six entity actions. See if that fixes your problem?...