cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Bigchaor
Level: Powered On

substring works if true but crashes if false

if(contains(triggerBody()['body']['content'],'toto-'),substring(triggerBody()['body']['content'],indexOf(triggerBody()['body']['content'],'toto-'),15),'N/A')

The substring works if true but ....

It shouldn't even get to the substring statement since the result is false

 

Thank you  in advance

5 REPLIES 5
Super User
Super User

Re: substring works if true but crashes if false

Hi @Bigchaor

It would helpful for more detail, images etc to get a grasp of the conectors, triggers used.

 

Thanks, Alan


Did I answer your question? Mark my post as a solution!

Proud to be a Flownaut!


Super User
Super User

Re: substring works if true but crashes if false

Don’t use contains. It doesn’t do what you think it does!

In this case tying using an IndexOf() > -1 as your test.

Contains is specifically for whether a collection contains an object. Not a string contains that we might be used to.

- Mark
Bigchaor
Level: Powered On

Re: substring works if true but crashes if false

still crashes

 

Capture.PNG 

if(greater(indexOf(triggerBody()['body']['content'],'toto-'),-1),substring(triggerBody()['body']['content'],indexOf(triggerBody()['body']['content'],'toto-'),15),'N/A')
Super User
Super User

Re: substring works if true but crashes if false

 

Ok, I see whats wrong with your substring. The sum of the StartIndex and Legth is going to be greater than the total lenght of the string.

 

because you are asking for the IndexOf "toto-" and asking for exactly 15 characters after that we have to assume your string will always be longer than the sum of those two.

 

The easiest way to resolve this is to throw in a load of compose actions for each part of your expression. write out the "length" of your intial string. Another or the output of the indexOf, etc. You might find you need to start your substring at indexOf()+5 (the length of the toto- string). And also ensure there are 15 characters available in the full string for that to work. Also, bear in mind that when referencing your string in this way the position references are 0-based (so the start of the substring).. that means the first item is zero and the second character is 1... whereas the Length references are 1-based. Just to make everything REALLY confusing.

 

So.. take each atomic part of your expression. Write each bit out into a separate compose action and you should see where your numbers don't add up.  Feel free to paste the output of the Compose actions here for us to help more.

 

By the way, the method isnt crashing, it's working perfectly!!

 

- Mark

 

 

Bigchaor
Level: Powered On

Re: substring works if true but crashes if false

it only crashes when its false and the substring section is the true condition it's not even supposed to consider any of this statement

 

Helpful resources

Announcements
thirdimage

Power Automate Community User Group Member Badge

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

firstImage

Incoming: New and improved badges!

We've given our badges an overhaul and also added some brand new ones!

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

sixthImage

Power Platform World Tour

Find out where you can attend!

seventhimage

Webinars & Video Gallery

Watch & learn from the Power Automate Community Video Gallery!

Top Kudoed Authors
Users Online
Currently online: 318 members 6,245 guests
Please welcome our newest community members: