Switch based on contains

Hello,

I am trying to pass an URL to a Switch activity in order that if it contains : facebook it should run an activity , if it contains twitter it should run a different activity, etc.

But i am having issues with syntax.

image

can anyone help me develop a solution for my case?

1 Like

Hi,

So Switches are slightly different than Decisions or If statements. The Cases should equal an Integer or String that the Expression returns (if that makes sense). The Expression should return exactly the string you are going to use as your Case so for example “facebook”.

In order to use .Contains, you can simply put that in the Expression. For example,

Expression:   If( row("URL").ToString.ToUpper.Contains("FACEBOOK"), "facebook", If( row("URL").ToString.ToUpper.Contains("TWITTER"), "twitter", "default" )

Then your cases would just look like:

Case1 facebook
Case2 twitter
Case3 default

I would also recommend using this in a Flow Chart, but that’s my opinion.


You can also consider State Machines. They allow you to use a full condition for the destination of each platform, that is, if each one uses a different process.

Regards.

13 Likes

switch.xaml (6.5 KB)

17 Likes

Hi Clayton,

what will be the condition for the states?

Hi, what do you mean by your question @PriyaRK?
The conditions go in the Destinations part of the State, and you can put anything there that returns a boolean like text.Contains(“abc”), et cetera. When the State that processes is complete it will look at which destination to take in the order from top to bottom as they are listed in the Destinations section, and only if it is True.

Regards.

1 Like

This is awesome ! Thank you !

We now have a new activity Else If that will do what the OP wants.

Hello,

Depending of your use case, you might find handling the different cases in a mapping structure convenient. That way, you don’t have to edit your code if any change occurs but just udpate the dictionnary/datatable/worksheet/…that contains your mapping and add eventually related workflow.

This might evolve:

MyCases = {
  {"facebook" "path_to_facebook_workflow"},
  {"twitter", "path_to_twitter_workflow"},
  {"buzzword", "path_to_buzzword_workflow"},
  {"", "path_to_default_workflow"}
}

But this should remain as it is:

ForEach ThisCase As KeyValuePair in MyCases
  If row('url').ToString.Contains(ThisCase.Key)
    ThisWorkflowPath = ThisCase.Value.ToString
    Break

InvokeWorkflow(filename=ThisWorkflowPath)

You can use a default value for ThisWorkflowPath too (beware of its scope). You have the idea.

@vvaidya
Thanks , it helped me in my use case :slight_smile: