cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ChrisBenJohnson
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
 

View solution in original post

8 REPLIES 8
ChrisBenJohnson
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.

RezaDorrani
Dual Super User
Dual Super User

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

 

 

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
 

@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
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

New Ideas Forum MPA.jpg

A new place to submit your Ideas for Power Automate

Announcing a new way to share your feedback with the Power Automate Team.

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.

MPA Licensing.jpg

Ask your licensing questions at the Power Automate AMA!

Join Priya Kodukula and the licensing team, super users and MVPs to find answers to your questions on Power Automate licensing.

Users online (2,340)