#PowerFix🛠️ 3 - InvalidTemplate: The length of substring can’t be longer than ‘X’ which is the length of the source string.

A couple of days ago I had a DM from one of my LinkedIn connections asking for a little bit of help with Power Automate. The use case was simple - they had a SharePoint list that they were moving to Dataverse. One of the columns was multiline text, and they wanted to extract 100 characters from it and use it as the title of the record. Some of the descriptions were shorter than 100 characters, and they were getting the following error.

InvalidTemplate: The length of substring can’t be longer than ‘48’ which is the length of the source string.

This error is effectively telling us that you cannot extract 100 characters from a string of text that only has 48 characters. You’d think that Power Automate would simply ignore that, no?! Or would that be too simple?😉

Firstly, let’s have a look at the structure of the source text. For today’s demo, we will use a SharePoint list to get the description from, and then populate the result in a column in that same list.

The most logical way would be to use the Text Function action called Substring, or use the Substring () expression. We would get the description into the text field which will automatically wrap it up in an Apply to Each loop for us, set the starting position to 0 and length of 100, and then set our variable to be the output of that action.

This will however break and throw the error shown above.

The solution is simple - all we need to do is check the number of characters in the original string. If it is longer than 100, we want to extract 100 characters only, if it’s shorter - then we want to leave the description as it is.

In our apply to each action, we will need to add a Condition control. In the value box, we will use an expression to check the length of our source string. For the operator, we will use ‘is less than’ and then 100 in the last box. My expression for this specific case will be:

length(items('Apply_to_each')?['Fulldescription'])

For the true statement (If yes), we just want to leave the text as it is. I will set the variable to be the original source text (in my case, that’s the Full Description column from SharePoint).

For the false statement (if no) we want to perform the substring. To do this, we have two options as mentioned earlier - we can either use the Substring() expression, or the Substring action from the Text Functions. I’ll show you both ways.

Substring() expression

In my case, this will be: substring(items('Apply_to_each')?['Fulldescription'], 0, 100)

Substring action
Search for the Substring action which is one of the Text Functions.

Set the following:

  • Text field to be the original column (in my case - Full Description).

  • Starting position will be 0 as we are starting from the beginning of the text,

  • Length 100 as we want to extract 100 characters

You can then set the variable to be the output of the Substring action.

Your condition statement should look like this:

We then need to save that data somewhere. In my case, I’m updating another column in the same SharePoint list, so the Update Item action will look like below:

We are now ready to run the flow!

Seeing all green ticks with no error is kind of satisfying, isn’t it?


The flow has run successfully and looking at our outputs, we got exactly what we needed. Awesome!😊

If you have any Power Platforms issues or errors, please send them my way and I’ll cover them in my blog posts!

Please don’t forget to subscribe if you would like to receive updates on new blog posts!👇

Previous
Previous

Building beautiful buttons in Canvas Apps part 1 - neumorphic buttons

Next
Next

Building a beautiful Power Apps mobile navigation menu - Components (part 4)