Ever had a Flow fail because of an invisible space at the end of a string? It happens more often than you'd think. The Power Automate "trim" function solves this by removing leading and trailing whitespace from any string you provide.
Let's see how to use it.
Usage
It follows a simple pattern where we provide the string to trim. For example:
trim(' Manuel T. Gomes ')
will return
'Manuel T. Gomes'
That’s what we expect. So regardless of the number of spaces in the beginning or end, they will all be removed. But what about spaces in the middle of the string? For example:
trim(' Manuel T. Gomes ')
will return
'Manuel T. Gomes'
Removing only the trailing and leading characters is a big difference from Power Apps’ trim function, where the spaces in the middle of the string will be removed. In our case, the spaces won’t be removed.
Limitations
Although there’s no documented limitation for the limit of characters, I would recommend not going over 10000 characters. However, if you’re parsing huge strings, it doesn’t make sense to break them since the trim won’t return the correct values, but be sure to deal with errors if your Flow fails.
Recommendations
Here are some things to keep in mind.
Convert Actions
Power Automate trim function is the kind of function that doesn't hurt to use. What I mean is that worst-case scenario, your input and output strings will be the same. If you're converting text to another type, I recommend always trim the string before converting it, especially if the user provides the string. A space can break a conversion, so using the trim will remove these issues from the start.
Be sure that you're providing a string
Always be sure that you're providing a valid string. For example, if you're using a compose action and the value is null, you'll get something like this.
Unable to process template language expressions in action 'Compose' inputs at line '0' and column '0': 'The provided parameters for template language function 'trim' are not valid.'.
One solution is to use the "coalesce" function like this:
trim(coalesce(<your value>,''))
So if the value is null, it will return an empty string, which is a valid string for Power Automate.
Don't nest
There’s no real reason to do it, but if you find yourself in a situation where you have nested the Power Automate trim function in a formula, you should review it and make everything more straightforward.
Sources
Microsoft’s trim Function Reference
Back to the Power Automate Function Reference.
Photo by Thom Holmes on Unsplash
No comments yet
Be the first to share your thoughts on this article!