Power Automate’s startsWith function is part of a set of functions that deal with strings. In this case, we’re checking if a string starts with “something” or not. It’s pretty self-explanatory, but there are some things to take into account.
Usage
It follows a simple pattern.
- Text
- Text to search
The startsWith function will always return a true or false result.
Let’s start with a simple example:
startsWith(variables('TEXT'),variables('TEXT_TO_COMPARE'))
'Manuel T. Gomes' - compare with - 'Manuel'
will return
true
It makes sense since it’s the exact string. But let’s do some variations to see what we get. First, the comparison string in a different case:
startsWith(variables('TEXT'),variables('TEXT_TO_COMPARE'))
'Manuel T. Gomes' - compare with - 'manuel'
will return
true
So we can already see that the comparison is case-insensitive. To prove the point.
startsWith(variables('TEXT'),variables('TEXT_TO_COMPARE'))
'Manuel T. Gomes' - compare with - 'mAnUeL'
will return
true
Now let’s add spaces:
startsWith(variables('TEXT'),variables('TEXT_TO_COMPARE'))
'Manuel T. Gomes' - compare with - ' Manuel '
will return
false
Spaces are considered as a character, so the string is not the same. Be careful with this. The trim function can help but always understand what you’re trimming.
Let’s add some numbers:
startsWith(variables('TEXT'),variables('TEXT_TO_COMPARE'))
'1Manuel T. Gomes' - compare with - '1'
will return
true
In this case, we’re still comparing strings, but how about if we have a variable with a number?
startsWith(variables('TEXT'),variables('NUMERIC_VARIABLE'))
will return an error
Power Automate will consider the expression as correct, but it will return the following error when it runs:
Unable to process template language expressions in action 'Test_with_number' inputs at line '1' and column '4721': 'The template language function 'startsWith' expects its second parameter to be of type string. The provided value is of type 'Integer'. Please see https://aka.ms/logicexpressions#startsWith for usage details.'.
The definition is clear, so be aware that there’s no conversion of the datatypes. To “solve” this, you can use the string function that converts the number into the correct datatype. Here’s an example:
startsWith(variables('TEXT'), string(variables('NUMERIC_VARIABLE')))
'1Manuel T. Gomes' - compare with - 1
will return
true
That’s it. The function is quite simple and super useful.
Limitations
Depending on the size of your string, your expression may return an error, even if it’s correct. Please note that the expressions have a max size of 8,192 characters. If you have an expression that is even bigger than 1000, I would strongly advise breaking it into smaller, manageable formulas.
As mentioned before, please don’t forget that the function requires two strings. It would be best if you did the conversions before calling the function. Power Automate won’t return an error when you’re building the Flow, but it will when it’s running.
Recommendations:
Here are some things to keep in mind.
Use “debug” compose actions.
Since the comparison will return true or false, sometimes it’s tricky to understand how the calculation is done, depending on how complex the expression is. So I recommend using Compose actions to have the values that go “in” the function. This way, if the value doesn’t make sense, you can understand, based on the parameters, why it was calculated that way.
Don’t nest
There’s no real reason to do it. If you try to nest “startsWith” functions, you’ll receive an error since boolean values are not comparable. So be very careful to avoid breaking Flows.
Sources:
Microsoft’s startsWith Function Reference
Back to the Power Automate Function Reference.
Photo by Ricardo Rocha on Unsplash