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

Substring Failing in Non-Triggered Branch of Condition

I have a Flow where I am trying to parse a string that is created from a float. The float is converted to a string and then Substring is used to check if the first character is a minus symbol or not (so is is negative or positive?)

 

This is handles by an If statement, and the True and False conditions each contain a Substring. If there is a negative float supplied, the resultant string will be longer due to the minus symbol and the substring character length is adjusted.

 

The problem is this length is out of scope for a positive float. This shouldn't be an issue as positive floats won't trigger the branch of the If statement that contains the substring that will fail, but the entire function is failing.

 

It's as if Flow is evaluating each branch of the If statement and not the pertinent ones. If this was any other language this logic wouldn't be an issue - this is why If statements exist!

2 ACCEPTED SOLUTIONS

Accepted Solutions

Hi @ChrisBenJohnson 

 

if expression evaluates everything at run time and hence it fails

 

y not add a condition branch for if 

2019-08-13_15-53-02.png

 

Regards,

Reza Dorrani

 

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

Hi @ChrisBenJohnson rather than hardcoding the length of the string, you could make it dynamic so that no matter how long the string is it will still figure out the correct length. I have only created a basic example, but it looks like this

 

I have a variable (float), called MyVar, which contains -50000

 

I have a second variable (string) which is going to be the everything from character 1 (5) to the end (the final 0) and uses the expression:

substring(string(variables('MyVar')),1,sub(length(string(variables('MyVar'))),1))
The bit in bold should be what replaces "3" in your example.
 
I hope that helps
 




Did my reply help? Please give it a thumbs up.
Did I answer your question? Please mark my post as a solution!
If you liked my response, please consider giving it a thumbs up. THANKS!


Proud to be a Flownaut!





Community Leader: Black Country PowerApps & Flow User Group

View solution in original post

8 REPLIES 8
Advocate II
Advocate II

I should say I worked around this in my Flow by concatenating a few extra zeroes on the end of the string after converting the float. There will always be chopped off but it prevents the 'out of scope' errors when using substring.

Dual Super User II
Dual Super User II

Hi @ChrisBenJohnson 

 

Can you post screenshot of your Flow run highlighting the if condition checks etc.

Hi @RezaDorrani 

 

I have a simple example where I was able to replicate the issue:

 

Capture.PNG

 

The expression in the final step is below:

 

if(
  equals(
    substring(
      string(mul(sub(div(variables('TW'), variables('LW')), 1), 100)),
      0,
      1
    ),
    '-'
  ),
  substring(
    string(mul(sub(div(variables('TW'), variables('LW')), 1), 100)),
    1,
    3
  ),
  substring(
    string(mul(sub(div(variables('TW'), variables('LW')), 1), 100)),
    0,
    3
  )
)

 

The calculation should run as follows:

  • TW / LW = 6
  • 6 - 1 = 5
  • 5 * 100 = 500
  • Str(500) = "500"

In the 'True' branch of the expression, substring will fail due to position (1, 3) requiring at least 4 characters in the supplied string. However the 'False' branch should be the one triggering, but the error I get back references (1, 3) from the True substring.

Hi @ChrisBenJohnson 

 

if expression evaluates everything at run time and hence it fails

 

y not add a condition branch for if 

2019-08-13_15-53-02.png

 

Regards,

Reza Dorrani

 

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

Hi @ChrisBenJohnson rather than hardcoding the length of the string, you could make it dynamic so that no matter how long the string is it will still figure out the correct length. I have only created a basic example, but it looks like this

 

I have a variable (float), called MyVar, which contains -50000

 

I have a second variable (string) which is going to be the everything from character 1 (5) to the end (the final 0) and uses the expression:

substring(string(variables('MyVar')),1,sub(length(string(variables('MyVar'))),1))
The bit in bold should be what replaces "3" in your example.
 
I hope that helps
 




Did my reply help? Please give it a thumbs up.
Did I answer your question? Please mark my post as a solution!
If you liked my response, please consider giving it a thumbs up. THANKS!


Proud to be a Flownaut!





Community Leader: Black Country PowerApps & Flow User Group

View solution in original post

@MattWeston365 

 

Thanks, this looks good, I am used to more forgiving langauges where substrings won't fail if too many characters are specified. It's sometimes really annoying that Flow won't just ignore the out of scope characters and just return what it has, but I guess that could lead to unexpected results in some situations.

@RezaDorrani 

 

Thanks, I am just scratching the surface of these quirks! I had previously used conditions but in a large Flow expressions save screen space and can be more quickly copied and pasted.

 

This does seem weird behaviour for an If statement though, wouldnt you say?

Hi @ChrisBenJohnson 

 

Yes it is weird 

But I did not notice that the expression syntax does evaluate all the conditions in the expression

 

In this case the If condition branch helps to create the Yes,No branches

 

Helpful resources

Announcements
PP Bootcamp Carousel

Global Power Platform Bootcamp

Dive into the Power Platform stack with hands-on sessions and labs, virtually delivered to you by experts and community leaders.

secondImage

Power Platform Community Conference On Demand

Watch Nick Doelman's session from the 2020 Power Platform Community Conference on demand!

MPA Community Blog

Power Automate Community Blog

Check out the community blog page where you can find valuable learning material from community and product team members!

Users online (11,225)