cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
WillPage
Level 8

substring function issue - help please!

I use the following expression to initialise a variable to extract a number (as a string) from a block of text:

substring(body('Html_to_text'),add(indexOf(body('Html_to_text'),'Number of Adults'),18),sub(indexOf(body('Html_to_text'),'Number of Children'),add(indexOf(body('Html_to_text'),'Number of Adults'),18)))

Here is the output of body('Html_to_text'), with some editing to protect privacy.

 

Enquiry from: Joe Bloggs Email: jxxxy.mxxxxn@live.com Phone Number: 027xxxxx99 First Night:
Friday, 19 April 2019 Number of Nights: 3 Number of Adults: 4 Number of
Children: 2 Comment: Hello xxxxxxxxxxxxxxxxxxxxxxxxxxxx

Now, this expression has been working OK for weeks, sucessfully grabbing the number of adults, and it has worked since on a subsequent incoming email but for some reason, on this particular block of text is fails with this:

 

InvalidTemplate. Unable to process template language expressions in action 'Adults' inputs at line '1' and column '2516': 'The template language function 'substring' parameters are out of range: 'start index' and 'length' must be non-negative integers and their sum must be no larger than the length of the string. Please see https://aka.ms/logicexpressions#substring for usage details.'.

I have tried changing Initialize Variable to Compose with the same expression but I get the same result. It's repeatable no matter how many times I resubmit the failed Flow or test using data from the previous run.

 

Earlier in the Flow there are multiple similar expressions that grab the Email address, Phone number, First Night and Number of Nights, and they are all fine when I run this block of text through them.

 

It's driving me mad - how could this expression fail for just this one block of text and not any others before or since? It's possible the number of adults could be a 2-digit number but so far since I created this Flow it's always been a single digit.

1 ACCEPTED SOLUTION

Accepted Solutions
Community Support Team
Community Support Team

Re: substring function issue - help please!

Hi @WillPage,

 

I have made a test on my side, you could refer to my screenshot below to create the flow:

Capture.PNG

 

The expression in the Compose as below:

last(split(body('Html_to_text'),'Number of Adults: '))

The expression in the Compose 2 as below:

first(split(outputs('Compose'),' Number'))

The flow would run successfully as below:

Capture.PNG

 

 

Best regards,

Community Support Team _ Alice Zhang
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

5 REPLIES 5
WillPage
Level 8

Re: substring function issue - help please!

I'vd done a bit more testing and found the problem seems to be that the expression is not finding the string "Number of Adults", it's just counting 18 chars from the start of body('Html_to_text'). Why might this be so?

Community Support Team
Community Support Team

Re: substring function issue - help please!

Hi @WillPage,

 

What are you want to extract from the email body text? Could you please explain more details or share a example about that?

 

I have made a test on your expression that you provided, the result of expression below is "-154",

sub(indexOf(body('Html_to_text'),'Number of Children'),add(indexOf(body('Html_to_text'),'Number of Adults'),18))

So your expression below wouldn't work:

substring(body('Html_to_text'),153,-154)

 

Please share more details about your requirement so we would try to provide a proper workaround for you.

 

Best regards,

Alice

 

 

 

WillPage
Level 8

Re: substring function issue - help please!

@v-yuazh-msft I'm trying to get the number (as a string) after 'Number of Adults: ', which may be a single character or could be two (=> 10).

 

I have managed to kind of work around this fault by doing this:

substring(body('Html_to_text'),add(lastIndexOf(body('Html_to_text'),'Number of Adults'),18),2)

..but if number is a single digit I get a space after the number. I can trim it later I guess.

Community Support Team
Community Support Team

Re: substring function issue - help please!

Hi @WillPage,

 

I have made a test on my side, you could refer to my screenshot below to create the flow:

Capture.PNG

 

The expression in the Compose as below:

last(split(body('Html_to_text'),'Number of Adults: '))

The expression in the Compose 2 as below:

first(split(outputs('Compose'),' Number'))

The flow would run successfully as below:

Capture.PNG

 

 

Best regards,

Community Support Team _ Alice Zhang
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

WillPage
Level 8

Re: substring function issue - help please!

@v-yuazh-msftI marked this as the solution but I had another solution already. I'm sure yours will work, but instead of using Html to text, I run the substring expression on the body of the trigger email.

 

substring(triggerBody()?['Body'],add(indexOf(triggerBody()?['Body'],'Number of Adults'),18),sub(indexOf(triggerBody()?['Body'],'\nNumber of Children'),add(indexOf(triggerBody()?['Body'],'Number of Adults'),18)))

In the particular email that triggers the flow, a new line break is used, which seems to work pretty well with this substring expression compared to the spaces that they are replaced with in the Html to text output. e.g:

 

First Night: Wednesday, 15 May 2019\nNumber of Nights: 3\nNumber of Adults: 2\nNumber of Children: 0\nComment:

 

 

 

Helpful resources

Announcements
firstImage

Power Platform Online Conference

Speakers, submit your sessions now! Call for speakers ends Feb. 10!

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!

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 (Last 30 Days)
Users online (4,128)