Can't get Logic App Contains to work with array or comma separated string

2.1k views Asked by At

I'm trying to look for specific keywords inside of text from a for each loop.

var text = "The lazy fox jumped over the brown dog."
var keywords = "fox,dog,sun";

If true, I want to do something with the text. If false, I want to ignore the text.

Does anyone know how to use an Array filter, Function, Select, Condition or inline code to check for this? If so, specific examples would be great.

By the way, I have a C# function that handles this extremely well in an ASP.net Core app.

UPDATE 1:

This doesn't work.

enter image description here

UPDATE 2:

The Condition is always false after the for each loop even after changing the settings and parallelism to 1.

Azure Logic App Condition does not work in loop if based on changing values

Thanks in advance!

3

There are 3 answers

0
Dumber_Texan2 On BEST ANSWER

Placing a Compose behind the for each loop and referencing the Output in the Condition is what finally worked for me. I used the toLower() function in my Compose. The Compose looks like this.

toLower(items('For_each_2')?['day']?['longPhrase'])

enter image description here

2
SwethaKandikonda On

One of the workaround would be use of Condition Connector. I have initialized the sentence in a string and then used Condition Connector which will be checking the conditions. enter image description here

Finally, In the true section you can add the connectors accordingly.

2
10p On

There are so many ways to achieve what you need. Here are the 3 options that came to my mind within a minute.

  1. The first one does use a For each loop, but I wouldn't recommend using it as it's not very efficient.

enter image description here

The For each parameter looks like this:

enter image description here

The Condition parameter looks like this:

enter image description here

  1. The second option is much easier - no need for a loop, just filter the array straight away, then you can check whether it's empty or it has some items:

enter image description here

The Filter array parameters look as follows.

enter image description here

The split function is identical to the one used in option 1.

  1. If you know JavaScript, you might decide to use regular expressions in inline code instead, e.g.:

enter image description here

Then you'd just need to check the output of the inline code. JavaScript code used in the example above:

var text = workflowContext.actions.Compose_text.outputs;
var keywords = workflowContext.actions.Compose_keywords.outputs;
return text.match(new RegExp("(" + keywords.split(",").join("|") + ")", "gi"));

My personal preference is option 2. However, please note that all 3 options above would find "sun" in text "The weather was sunny" even though there's no word "sun" in the text. If you do need "sun" to match only word "sun" - not "sunny", "asunder" or "unsung" - then go for option 3, just use a different, more complex regular expression.