#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!đ