334
Views
10
Comments
Solved
For loop in server action

Am trying save more than one record so I am using for loop in server action but I am facing some issue while running this server action, data is not getting saved can anyone tell me how to implement this 

2022-08-03 04-32-50
Ravi Punjwani
Solution

Hi Annish,

Please make sure the following:

  • Your looping variable has as many items as you want to save. You mentioned only first item is created, but can you confirm there were more items in the looping variable when you tried to debug.
  • The looping variable's .Current property is used in the action inside your loop, i.e., CreateOrUpdateCddQuestionTran.

If these aspects are configured correctly, the loop should create multiple items in your entity as required.

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

Hi Annish,

I can't see an obvious reason why this would go wrong, other than that there's a timeout causing a rollback of the database transaction. Have you debugged it?

UserImage.jpg
Annish J K

Hi, I have debugged it I noticed that only the first record is getting saved / updated other record is not getting updated

 this is from database before running the server action

 I have changed the value and run my server action

 this is from database after updating


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

But does the for each run more than once? Is there actually more than one record?

If so, have you checked what the Id of the record is? Is it NullIdentifier() or does it have a value? Are they different values?

Debugging is more than just noticing something goes wrong. It's trying to find why things go wrong.

2024-12-19 07-54-27
Aditya Chinchole

Hi Annish,

Are you passing list of Ids to the input parameter used in for each loop?

Because when only one record gets saved it is usually because it gets only One current Id and not the list of Ids.

And If you are passing the List Of Ids and still getting issue, then can you please share the oml..

Regards

2022-08-03 04-32-50
Ravi Punjwani
Solution

Hi Annish,

Please make sure the following:

  • Your looping variable has as many items as you want to save. You mentioned only first item is created, but can you confirm there were more items in the looping variable when you tried to debug.
  • The looping variable's .Current property is used in the action inside your loop, i.e., CreateOrUpdateCddQuestionTran.

If these aspects are configured correctly, the loop should create multiple items in your entity as required.

UserImage.jpg
Annish J K

Hi I have created another application with different entity and aggregate still facing the same issue, only the first record is getting saved  I have attached the oml file for reference 

FetchingDataOnDemandExercise.oml
2022-08-03 04-32-50
Ravi Punjwani

Hi Annish,


You're looping through In2 variable which is a list. All good with that.

But inside the loop, you're calling CreateOrUpdateEmployee and passing In1 variable which is a variable of type Employee. It will always have same value.

Solution:

  • Remove In1 variable in your Action1
  • In the Entity Action CreateOrUpdateEmployee, pass the value of In2.Current instead of In1

The problem is same as I pointed out in my previous comment. You can refer back to 2nd bulleted point there.


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

Please make sure you understand what went wrong, and how you can detect it the next time something like this happens. If you understand how a For Each works, and what the input to the save action should be, you would've caught this bug when debugging.

2024-12-19 07-54-27
Aditya Chinchole

Hi Annish,

I have updated the Oml. Please Refer to it.

You were passing different inputs to the loop and create action.

Regards,

Aditya

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