cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Advocate II
Advocate II

MS Flow IF statement error due to evaluating both True and False options

Hi,

 

I am building an expression in Flow to parse text in an MS Planner description to pick up values between tags.

 

For example Planner doesn't have a Level of Effort field, so I am putting tags in the description <LOE>12<LOE>, and trying to extract the value of 12 from the descripton. I will reuse the same logic to add any other needed attributes.

 

My problem is MS Flow seems to evalute both the True and False condition when I use an if statement to check if a tag is present (not all Planner tasks will have all/the same tags). If the tag is present, there will be a 3 element array, if not just return 0.

 

 

if(
    greater(length(split(body('Get_task_details')?['description'], '<LOE>')), 1),
    split(body('Get_task_details')?['description'], '<LOE>')[1],
    0
)

 

When no tag is present, I get the error:

 

InvalidTemplate. Unable to process template language expressions in action 'Set_variable' inputs at line '1' and column '2527': 'The template language expression 'if(greater(length(split(body('Get_task_details')?['description'], '<LOE>')), 1), split(body('Get_task_details')?['description'], '<LOE>')[1], 0)' cannot be evaluated because array index '1' is outside bounds (0, 0) of array. 

 

But why is Flow trying to evaluate the TRUE part of the if clause when no <LOE> is present?

 

I tried an alternative approach using subtring and indexing:

 

if(
    and(equals(triggerBody()?['hasDescription'], true), greaterOrEquals(indexOf(body('Get_task_details')?['description'], '<LOE>'), 0)),
    substring(
        body('Get_task_details')?['description'], 
        add(5, indexOf(body('Get_task_details')?['description'], '<LOE>')),
        sub(lastIndexOf(body('Get_task_details')?['description'], '<LOE>'), add(5, indexOf(body('Get_task_details')?['description'], '<LOE>')))),
    0
) 

With the same error, "substring can't have a negative argument", from the -1 being returned when <LOE> not present.

 

Am I missing something?

 

The only way I have gotten this to run is with setting each part of the evalutation statement in parts first, and then adding a Conditional Flow step where if the <LOE> was found go down one path, and if not then use 0. Adding the conditional for each evaluation massively over complicates the flow and is not flexible if we need to add/remove tags in the future.

 

Any help on how to do this is much appreciated.

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Support
Community Support

Hi @Charles_John,

 

Thanks for feedback. I can reproduce this error message on my side.

 

I will help take further investigation on it and back to you once I got a solution on it.

 

Best regards,

Mabel Mao

Community Support Team _ Mabel Mao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
Highlighted
Community Support
Community Support

Hi @Charles_John,

 

Thanks for feedback. I can reproduce this error message on my side.

 

I will help take further investigation on it and back to you once I got a solution on it.

 

Best regards,

Mabel Mao

Community Support Team _ Mabel Mao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Highlighted

Hi Yamao,

 

Just wondering if there has been any progress on this issue. I've consistently encountered this behaviour and it's a serious problem. It runs contrary to all expectations for an IF statement as a logic check, and prevents us from being able to use it to avoid having to introduce unnecessary Conditions. Please let us know if we can expect this to be addressed.

 

Thanks,

Josh

Highlighted

I also have this issue and would like to know when this can be resolved.

Highlighted

@v-yuazh-msft@v-yamao-msftCan you please update on the status of this bug?

 

Thanks,

hux-msft

Highlighted
Advocate V
Advocate V

@Charles_John et all, I was dissatisifed with the silence on this thread, so I converted it to a bug report here:

 

https://powerusers.microsoft.com/t5/I-Found-A-Bug/IF-statement-error-due-to-evaluating-expressions-i...

 

It's not resolved so to speak, but a workaround exists. By inverting your conditional logic and placing your potentially invalid sub-expression last (as the FALSE option), it seems to ensure that MS Flow doesn't attempt to evaluate it unnecessarily. In your case it looks like:

if(
    not(greater(length(split(body('Get_task_details')?['description'], '<LOE>')), 1)),
    0,
    split(body('Get_task_details')?['description'], '<LOE>')[1]
)

 There's likely a briefer way to check the opposite of your conditional statement [like not(contains()) ] but this should work.

Highlighted

That is odd, but the work-around seems to be ok.

 

Changing:

if(greater(length(items('Contact')?['EmailAddresses']),1),items('Contact')?['EmailAddresses'][1]?['Address'],'no 2nd email')

 

to 

 

if(not(greater(length(items('Contact')?['EmailAddresses']),1)),'no 2nd email',items('Contact')?['EmailAddresses'][1]?['Address'])
 
Allows it to work. Not the prettiest thing ever.

Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

Power Platform ISV Studio

Power Platform ISV Studio

ISV Studio is designed to become the go-to Power Platform destination for ISV’s to monitor & manage published applications.

Top Solution Authors
Top Kudoed Authors
Users online (9,541)