How to split large string with emails in an array?

Summary

The following was the user requirement.

The question was the user had the weird string stored in the Multi People Picker Field.

i:0#|membership|Lisa.simpson@springfield.com;i:0#|membership|Bart.simpson@springfield.com;i:0#|membership|Marj.simpson@springfield.com;i:0#|membership|Homer.simpson@springfield.com

If you look closely at the sample string the “i:0#|membership|” is prefixed before all the emails. The question is how to get just emails with the array of emails. For a developer, it may not be difficult. But for a no-code citizen developer, it will be tricky. This blog post will explain step by step how to extract emails by using two string functions “concat” and “split”.

For other function reference, please book mark url https://aka.ms/LogicExpressions.

Step by Step Solution

Step # 1 Create a manually triggered flow with a Var1 variable.

Step # 2 Now we will add compose to split the string by ‘;’ (semicolon) character.

split(variables('Var1'), ';')

The above expression will provide the following output.

[
  "i:0#|membership|Lisa.simpson@springfield.com",
  "i:0#|membership|Bart.simpson@springfield.com",
  "i:0#|membership|Marj.simpson@springfield.com",
  "i:0#|membership|Homer.simpson@springfield.com"
]

Step # 3 Iterate over the above compose output and split the each item in array on a pipe character ‘|’.

split ( item(), '|') [ 2 ]

The above function will split on a pipe character for each items in the array.
e.g. for "i:0#|membership|Lisa.simpson@springfield.com" the out put will be
just email i.e. "Lisa.simpson@springfield.com".

First the split will create and array as
[
   "i:0#",
   "membership",
   "Lisa.simpson@springfield.com"
]
after that we are selecting the third element of the array. Note: the array is zero indexed so the third element will be 2.

NOTE: The final Compose “FinalArrayOfEmailsCompose” outside of the Apply each is a trick for collecting all values in the array.

The trick is make sure you are using inner loop’s compose as an input to it. Like following

outputs('SplitPipeChrCompose')

So when workflow runs the output of the final compose will look like the following.

Result

The final output is converted to required array with emails. Also we looked at the technique of Compose outside of Apply Each and collecting Compose values defined in the loop.

About Pankaj

I am a Developer and my linked profile is https://www.linkedin.com/in/pankajsurti/
This entry was posted in Power Automate. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s