Iterating through 25 variables

Hello,

I have a workflow with a large amount of variables. Based on these variables, the workflow prints some values to a Word Document. Currently, the workflow is hardcoded to print “CustomerExists1” / “CustomerAddress1” / “CustomerPrice1” and then “CustomerExists2” / “CustomerAddress2” / CustomerPrice2" in 25 separate Sequences.

Ideally, what i would like to do is to do a Do While i<=25 loop and then dynamically assign the variables as they are all (7) named like “Variable#” from 1-25. This is something I have done before when working with VBA, but I am not sure how to do it in UiPath Studio. What I have tried is something like:

Do While i<=25
Assign CurrentCustomerExists = “CustomerExists”+i.ToString()
Assign CurrentCustomerAddress= “CustomerAddress”+i.ToString()

If CurrentCustomerExists = TRUE Then
Insert CurrentCustomerName to Word

Assign i=i+1

This does not seem to be working, specifically I am getting Compiler errors when i try to use my ‘CurrentCustomerExists’ variable in an If statement, as it looks like UiPath is treating it as a string. Is there any way to accomplish what i want to do?

Thank you in advance.

@Morten_Rasmussen

Instead try to use a dictionary to store you values…so that the keys can be strings and we can use

currentaddess = dict_customer("CustomerAddress"+i.ToString).ToString

cheers

Do i understand you correctly that i create a dictionary variable, and add all my variables there, e.g. Key:“CustomerExists1” Value:CustomerExists1 and then I’ll be able to assign current address to the String Value in the Key and it will fetch the Boolean/String/Whatever value in the Value?

I haven’t worked with Dictionaries before (or much UiPath in general honestly, I’m pretty new to this)

@Morten_Rasmussen

Yes exactly…

So what you can do is

Use assigna ctivity to assign the value

Dict("Constantname" + index.ToString) = "Value"

Here constantname can be any constant you need and index is 1 to 25 or any numbers you want to save

And to access use finalvariable = dict("ConstantName" + index.ToString)

As .ToString casts to string you xan cast to the final respective variable type

Have some starter help here:

Having a bunch of variables like CustomerAddress1, CustomerAddress2, CustomerAddress3 etc is incorrect design. This is what datatables are for. You need to put all the data into a datatable (which is like a spreadsheet) and then loop through it.

https://academy.uipath.com/courses/datatables-and-excel-automation-with-studio

A Dictionary is not a good solution for this. It should be a datatable.

@postwick

Depends on how the values are being assigned…

Both works

Cheers

However they’re being assigned, they could be put into a datatable instead of all the individual variables, and a datatable is clearly the correct design when you have the same values for each customer ie Exists, Address, Price etc.

Using a dictionary with keys like CustomerAddress1, CustomerAddress2, etc doesn’t fix the poor design. It just makes it possible to loop through the poor design. This is very clearly a situation where a datatable should be used.

Hi @Morten_Rasmussen

  1. Create an array or list to store your variables. In this case, you can use a List of a custom data type to store your customer data.
  • Create a custom data type (e.g., CustomerData) with fields for CustomerExists, CustomerAddress, and CustomerPrice.
  • Create a List variable (e.g., customerList) of type CustomerData.
  1. Use a loop to populate the list with your customer data:

For i = 1 To 25
Assign CurrentCustomer = New CustomerData
Assign CurrentCustomer.CustomerExists = True ’ Set the value based on your logic
Assign CurrentCustomer.CustomerAddress = “CustomerAddress” & i.ToString()
Assign CurrentCustomer.CustomerPrice = “CustomerPrice” & i.ToString()
Add To Collection: customerList, CurrentCustomer
Next

Now, you have a list (customerList ) containing your customer data, and you can easily access it dynamically without the need to create individual variables:

For Each customer In customerList
If customer.CustomerExists Then
Insert customer.CustomerAddress & " / " & customer.CustomerPrice to Word
End If
Next

Thanks

I agree it is poor design, and I did not assign it. I am trying to find a way to work with what I have, and what I have currently is 3x7x25 arguments that are populated in one workflow and imported to another workflow :upside_down_face:

I think this sounds like the simplest solution, thank you.

This way I can keep all my variables as is, populate an array with the values, and then iterate through the array when populating my word document.

Thank you Nitya!

*I did not design it

It would be worth the work to convert it to using a datatable instead of all those arguments. Just my opinion.

1 Like

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