How to retrieve previous variable in for each loop in uipath

Hi team ,
I want to get previous variable value in for loop each.

Actually we are picking employee id from each pdf using for loop each . And any of the pdf don’t have employee id then rename with previous employee id . So we need to get previous employee from the for each loop temporary store in somewhere for reference .

Hi @Krishnakumar_Vasudevan,

You can achieve this in UiPath by using a temporary variable to store the previous Employee ID and update it in each iteration of the For Each loop.

Regards,
Arivu

If my understanding is correct - you are doing For each file in folder and extracting the employee ID??

You can create 2 variables

prevemployeeID
currentEmployeeId

Check employee id available in the file. if yes then
currentEmployeeId = value fetched from the pdf

Else
currentEmployeeId = prevEmployeeID

Let me know if my understanding is not correct and you need something else

Hey @Krishnakumar_Vasudevan

You can use the Index variable in FOR loop and access the previous value using index-1.
E.G. suppose your data is stored in datatable, you can use dt_Datatable.Rows(Index-1).Item(“ColName”).ToString to retrieve your required data fields.

Let me know, if this was helpful by marking the answer as solution.
Thanks.

1 Like

@Krishnakumar_Vasudevan
For Each currentItem in yourCollection
Assign temp_var = “a”
If String.IsNullOrEmpty(currentItem.ToString)
Then → Assign output_var = temp_var
Else → Assign output_var = currentItem.ToString

@Krishnakumar_Vasudevan,

Try this way:

Hi @Krishnakumar_Vasudevan

To explain you with an example of datatable
Input datatable which looks like


Were 3rd row id is empty

Workflow:

Output:


Id replaced with previos one

Hope this helps!

Hi @Krishnakumar_Vasudevan

→ If you are iterating the rows in a datatable by using For each activity. Output of for each is Currentrow.
→ Create a variable called Variable1.
→ Then use the If condition to check the condition as below,

Not(String.isnullorempty(Currentrow("employee id").toString))

→ Inside Then block insert the Assign activity and give as below,

- Assign -> Variable1 = Currentrow("employee id").toString

→ Then use the Variable1 if next row is empty it will store the previous value.

Check the below workflow for better understanding,

Hope it helps!!

Hi ,

I have add assign activity to capture emplid of each iteration in for loop . Emplid =“”

Currently running in 5th iteration but no emplid so I need to use 4 the iteration emplid for my 5 the iteration activity how can I use it ? Do we have solution to retrieve the 4 th iteration employee id .

Hi All ,

I have add assign activity to capture emplid of each iteration in for loop . Emplid =“”

Currently running in 5th iteration but no emplid so I need to use 4 th iteration emplid for my 5 the iteration activity how can I use it ? Do we have solution to retrieve the 4 th iteration employee id .

Yes I already gave you the solution in the above reply… @Krishnakumar_Vasudevan

Follow the below steps,
→ Create a variable to store the Emp id called “Empid”
→ Use the For each activity to iterate the each row in the datatable.
→ Inside for each insert the If activity to give below condition,

Not(String.isnullorempty(Currentrow("employee id").toString))

→ Inside then block insert the assign activity and give expression as below,

- Assign -> empid = Currentrow("employee id").toString

→ Use the empid variable in the outside of If activity.

If 4th row is empty then it empid variable will store the 3rd row value.

Hope it helps!!

FYI , we are not using data table instead we are using core.for each activity … do you have any solutions for this problem?

How is your input data… @Krishnakumar_Vasudevan

Could you share input and expected output

@Krishnakumar_Vasudevan,

Just keep a variable outside of the For Each to keep every non empty employee id. Assign every non empty employee id to this variable so it will be available in next iteration as well. In case of empty employee id, use this variable.

Refer this solution.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.