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

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
Dual Super User
Dual Super User

Re: Substring Failing in Non-Triggered Branch of Condition

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

Super User
Super User

Re: Substring Failing in Non-Triggered Branch of Condition

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
ChrisBenJohnson Advocate I
Advocate I

Re: Substring Failing in Non-Triggered Branch of Condition

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
Dual Super User

Re: Substring Failing in Non-Triggered Branch of Condition

Hi @ChrisBenJohnson 

 

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

ChrisBenJohnson Advocate I
Advocate I

Re: Substring Failing in Non-Triggered Branch of Condition

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.

Dual Super User
Dual Super User

Re: Substring Failing in Non-Triggered Branch of Condition

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

Super User
Super User

Re: Substring Failing in Non-Triggered Branch of Condition

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

ChrisBenJohnson Advocate I
Advocate I

Re: Substring Failing in Non-Triggered Branch of Condition

@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.

ChrisBenJohnson Advocate I
Advocate I

Re: Substring Failing in Non-Triggered Branch of Condition

@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?

Dual Super User
Dual Super User

Re: Substring Failing in Non-Triggered Branch of Condition

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
MBAS Gallery 2020

MBAS Gallery 2020

Watch Microsoft Business Applications Summit sessions on-demand.

firstImage

New Ranks and Rank Icons released on April 21!

The time has come: We are finally able to share more details on the brand-new ranks coming to the Power Automate Community!

firstImage

Now Live: Power Virtual Agents Community!

We are excited to announce the launch of Power Virtual Agents Community. Check it out now!

firstImage

New & Improved Power Automate Community Cookbook

We've updated and improved the layout and uploading format of the Power Automate Cookbook!

thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Community Summit North America

The top training and networking event across the globe for Microsoft Business Applications

Users online (5,953)