Multiple if statements - maybe a dev request to UiPath

Hi

I was thinking if this could be achieved in a better and more sufficient way.

I have read some posts about using switch or invoke code, but I can’t get switch to work like I wanted to.

What i’m trying to do is a decision tree that looks something like this

Basically a switch or flow switch where multiple if statements could be used is what i’m looking for.
The flow decisions looks like this;

Var1 = True and Var2 = True and Var3=“AU1901” and Cint(Totalpris) > 1914 and Var4.ToUpper <> “REP”

Next one
Var1 = True and Var2 = True and Var3=“AU1602” and Cint(Totalpris) > 1614 and Var4.ToUpper <> “REP”

Next one
Var1 = True and Var2 = True and Var3=“AU1406” and Cint(Totalpris) > 2058 and Var4.ToUpper <> “REP”

And so on

Any good ideas?

Hi @Michaeljep

How about using many ifs to set get a switch flg before using switch like below.

If Var1 = True and Var2 = True and Var3=“AU1901” and Cint(Totalpris) > 1914 and Var4.ToUpper <> “REP”
Then flg = 1

If Var1 = True and Var2 = True and Var3=“AU1602” and Cint(Totalpris) > 1614 and Var4.ToUpper <> “REP”
Then flg = 2

If Var1 = True and Var2 = True and Var3=“AU1406” and Cint(Totalpris) > 2058 and Var4.ToUpper <> “REP”
Then flg = 3

Switch flg
1:Write “Valg 1”
2:Write “Valg 2”
3:Write “Valg 3”

I’ve thought of using several if’s, but I don’t think that it’s a better way instead of using the flow decisions, the if’s is much similiar to the flow decisions.

Hi,

Can you try the following sample?
This sample uses dictionary to store conditions and keeps maintainability.

Sample20200623-2.zip (13.9 KB)

Regards,

Hi @Michaeljep ,

You can approach this situation by iterating or retrieving from a structure containing your cases. Here, your Var3 seems enough to distinguish each case consequently we can use a dictionary.

If you want to iterate over multiple tests, you can use an array and loop over its items.

I used Dictionary(Of String, Object) as values for the dictionary but you can use better structure if you’re familiar with them.

Variable

  • result As Boolean = False
  • cases As Dictionary(Of String, Dictionary(Of String, Object))
  • thisCase As Dictionary(Of String, Object)

Sequence

Below the TryGetValue doesn’t seem to assign the value to thisCase so I added an extra Assign activity

  • Assign
    cases = New Dictionary(Of String, Dictionary(Of String, Object)) From { _
	    {"AU1901", New Dictionary(Of String, Object) From {{"limit", 1914}, {"message", "message content 1"}}}, _
	    {"AU1602", New Dictionary(Of String, Object) From {{"limit", 1614}, {"message", "message content 2"}}}, _
	    {"AU1406", New Dictionary(Of String, Object) From {{"limit", 2058}, {"message", "message content 3"}}} _
    }
  • If Var1 And Var2 And Var4.ToUpper <> "REP" And cases.TryGetValue(Var3, thisCase)
    • Assign thisCase = cases(Var3)

    • If CInt(Totalpris) > CInt(thisCase("limit"))

      • Assign result = True

      • WriteLine thisCase("message").ToString

I’ll give it a go :slight_smile:
I would just be so much easier if the switch could be used for this

Expression = If statements

Cases
statement 1
statement 2
and so on

Pick the one that fits :slight_smile: