The “replace” function is one that shows up frequently in every programming language. The usage is simple but extremely powerful.

Usage

It follows a simple pattern.

  1. Main string
  2. Old Text
  3. New Text

Let’s start with an easy one.

Example:

replace('Hello World', 'World', 'Home')

will return 

'Hello Home'

The function will replace the instances of “World” with the word “Home.”

But what if we have multiple instances o the word.

Example:

replace('Apple#Banana#Pear', '#', ',')

will return 

'Apple,Banana,Pear'

The function will always replace all instances of the word in question.

For the last example, I want to give you a more usable scenario to use in your daily life.

Example:

replace('Manuel ## Gomes', '##', 'T.')

will return 

'Manuel T. Gomes'

I’m giving this example on purpose because this can be quite a nice trick. You can have template strings with placeholders that you can replace with the final values. Think of examples like email templates where you have the full email in a string or a file and only want to “replace” it for the name of the person. You can have placeholders like “###1” and “###2” and then replace them for the real values and send.

Limitations

You can only use strings. It’s not possible to replacement of integer values, for example. You’ll get an error like this:

Example:

replace('Manuel ## Gomes', '##', 2)

will return the following error:

Unable to process template language expressions in action 'Compose_2' inputs at line '1' and column '2853': 'The template language function 'replace' expects its third parameter 'new string' to be a string. The provided value is of type 'Integer'. Please see https://aka.ms/logicexpressions#replace for usage details.'. 

The same error will show up for any other types, so keep this in mind if you have variables and use them as parameters to the function.

Recommendations:

  1. Don’t build everything in the same formula. Ensure that you have only strings and keep the formula as simple as possible; even if you have to make some more steps with variables or composes, you’ll be able to debug and see if any value is not what you’re expecting.
  2. Don’t use this function inside other actions. Other actions have formula fields, but it makes it harder to debug. Do all the parsing before using it.
  3. Don’t nest it. It’s possible, and you won’t get an error, but it will be harder to debug also. Step by step, always.
  4. Please note that formulas may have localization-based differences. For example, you should write “replace” with, “, separating each of the arguments, but if set your regional settings to Portugal, you should use “;” instead.

Sources:

Microsoft’s replace Function Reference.

Back to the Power Automate Function Reference.

Photo by Utopia By Cho on Unsplash

Manuel Gomes

I'm a Project Manager with experience in large projects and companies. I've worked in the past for companies like Bayer, Sybase (now SAP) and I'm currently working for Pestana Hotel Group.

View all posts by Manuel Gomes →

Leave a Reply

%d bloggers like this: