cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ArchieGoodwin
Advocate I
Advocate I

Using "If" condition in Initialize Variable expression

Hello all. I'm parsing JSON from an online form, and I need to change the date formats for any date fields. That expression works fine (I initialize a variable with the raw data first, then initialize a "fixed" variable underneath that uses FormatDateTime to convert the format). What I did NOT test for were the two optional date fields, which are now returning a "null" value in JSON if not filled out, and it breaks my flow because I get a string error about the data not being in ISO date format (well, there's no data, so duh).

I've tried using the following expression in the Initialize Variable step, and it keeps telling me the expression is invalid:

if(empty(triggerOutputs()?['body/DOM']), 'null', (formatDateTime(variables('Raw_DOM')), 'dd/MM/yyyy'))

Funny thing is, if I remove the bracket in front of formatDateTime and at the end of yyyy, then it allows me to keep the expression, but it breaks when I run the flow because it is thinking it has four elements in the expression instead of three. But when I try to correct it with brackets as above, it won't even let me save the expression because PA says "expression invalid."

Anyone have any solutions for this? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
tom_riha
Super User
Super User

Hello @ArchieGoodwin ,

you've got one extra bracket that closes the formatDateTime(...) expression. FormatDateTime(...) needs 2 parameters, the date and the format, but in your expression you close it before you define the format. It should be:

if(empty(triggerOutputs()?['body/DOM']), 'null', formatDateTime(variables('Raw_DOM'), 'dd/MM/yyyy'))

 



[ If I have answered your question, please Accept the post as a solution. ]
[ If you like my response, please give it a Thumbs Up. ]

[ I also blog about Power Automate solutions even for non-IT people. ]

View solution in original post

2 REPLIES 2
tom_riha
Super User
Super User

Hello @ArchieGoodwin ,

you've got one extra bracket that closes the formatDateTime(...) expression. FormatDateTime(...) needs 2 parameters, the date and the format, but in your expression you close it before you define the format. It should be:

if(empty(triggerOutputs()?['body/DOM']), 'null', formatDateTime(variables('Raw_DOM'), 'dd/MM/yyyy'))

 



[ If I have answered your question, please Accept the post as a solution. ]
[ If you like my response, please give it a Thumbs Up. ]

[ I also blog about Power Automate solutions even for non-IT people. ]
ArchieGoodwin
Advocate I
Advocate I

Thanks, Tom! Clearly, I was not fully functional when I was reviewing that expression!

 

Cheers!

Helpful resources

Announcements
MPA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

Learn to digitize and optimize business processes and connect all your applications to share data in real time.

Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

May UG Leader Call Carousel 768x460.png

What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

PA Survey Carousel Image.png

We want to hear from you!

If you are a small business ISV/Reseller, share your thoughts with our research team.

Users online (2,326)