Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Automate - Building Flows
Answered

How to return an empty array within an if function so union function won't complain

(0) ShareShare
ReportReport
Posted on by 396

Hi,

Let me explain my question in detail:

I have a flow that runs every time I receive an email, and I want to save every email that is inside the From, To and CC fields.
So I made a variable of type object, so I don't need a ton of initialize variable actions.
This variable has a bunch of properties, and one of them will hold that list of emails that I want to save.

For this I made a large formula that sets the property to a union of multiple arrays so all emails are merged inside 1 array.
Problem is that the To and CC field is a string with multiple emails inside with ";" as the separator.
So this is the formula I had to come up with:

 

setProperty(

    variables('Flow Variables'),
    'vEmailUsers',
    union(
        variables('Flow Variables')?['vEmailUsers'],
        createArray(triggerOutputs()?['body/from']),
        split(triggerOutputs()?['body/toRecipients'],';'),
        split(triggerOutputs()?['body/ccRecipients'],';')
    )
)

This function works pretty good until i get an email that has an empty cc...
So the error says that the split function can't have null value for the first parameter.
Hence I was thinking to surround it with an if statement so that if it is empty I would return an empty array.

Except I don't know how...
I tried using createArray() without parameters, but that's not possible, and regular brackets won't work either.

So I am wondering how it could be solved?
Any ideas?

Thanks in advance!

  • Billy Profile Picture
    396 on at
    Re: How to return an empty array within an if function so union function won't complain

    Hi @grantjenkins,

    I used to do both checks, but I thought that it wouldn't be necessary since the To field would be a required field?
    After all, an email goes from a person to a person, so from and to would always be filled in.
    Also, it would be weird if to is blank but cc is not blank, as you are basically sending an email to nobody and sending a copy of your message to nobody to someone else...
    But that's my perspective, of course.

    I guess it won't hurt to add an extra check, but then again, it's just weird...

    Nonetheless, thanks for the advice!

  • Verified answer
    grantjenkins Profile Picture
    11,059 Super User 2025 Season 1 on at
    Re: How to return an empty array within an if function so union function won't complain

    @Billy_C @VictorIvanidze brought up a good point about the possibility of the To field being blank if they only used CC. For completeness it should probably be like this:

     

    union(
     createArray(triggerOutputs()?['body/from']), 
     if(empty(triggerOutputs()?['body/toRecipients']), 
     createArray(triggerOutputs()?['body/from']), 
     split(triggerOutputs()?['body/toRecipients'], ';')
     ),
     if(empty(triggerOutputs()?['body/ccRecipients']), 
     createArray(triggerOutputs()?['body/from']), 
     split(triggerOutputs()?['body/ccRecipients'], ';')
     )
    )

     

  • Billy Profile Picture
    396 on at
    Re: How to return an empty array within an if function so union function won't complain

    Hi,

    @VictorIvanidze you are right, I could indeed do an if statement with 2 different unions inside of it.
    Except that @grantjenkins his solution is more practical and efficient and actually was nearly the same as the solution I had except I didn't pass anything in the createArray function.
    That's mainly because I forgot that union automatically removes duplicates, and I could have just added any of the other arrays into it...

    Kinda facepalming here right now lol.

    Nonetheless, thanks guys for your help on this!

    Really appreciate it!

  • VictorIvanidze Profile Picture
    12,515 on at
    Re: How to return an empty array within an if function so union function won't complain

    That's possible To is empty but CC is not empty.

  • Verified answer
    grantjenkins Profile Picture
    11,059 Super User 2025 Season 1 on at
    Re: How to return an empty array within an if function so union function won't complain

    You can check if CC is empty, and if so, just use From in its place since you're using union and any duplicate will be removed.

     

     

    union(
     createArray(triggerOutputs()?['body/from']), 
     split(triggerOutputs()?['body/toRecipients'], ';'), 
     if(empty(triggerOutputs()?['body/ccRecipients']), 
     createArray(triggerOutputs()?['body/from']), 
     split(triggerOutputs()?['body/ccRecipients'], ';')
     )
    )

     


    ----------------------------------------------------------------------
    If I've answered your question, please mark the post as Solved.
    If you like my response, please consider giving it a Thumbs Up.

  • VictorIvanidze Profile Picture
    12,515 on at
    Re: How to return an empty array within an if function so union function won't complain

    Use different union() after checking if To or CC or something is empty.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Paul Stork – Community Spotlight

We are honored to recognize Paul Stork as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 497 Super User 2025 Season 1

#2
David_MA Profile Picture

David_MA 436 Super User 2025 Season 1

#3
Riyaz_riz11 Profile Picture

Riyaz_riz11 244 Super User 2025 Season 1