[Solved] “can’t instantiate template” error in Power Automate

If you think your friends/network would find this useful, please share it with them – I’d really appreciate it.

Have you tried to send an email template with Power Automate and got the error message “can’t instantiate template”? Like the error in the image below. In this post, we look at how we can solve the “can’t instantiate template” error in Power Automate.

Use case behind the error “can’t instantiate template” error

The use case behind this post is to notify owners when it was 30 days until their knowledge base articles expire. And, to solve this Power Automate is a good tool for sending out these notifications. It seems like a straightforward task to do. How difficult can it be? In this case, all previous email notifications were using standard email templates in Dynamics. We wanted to continue using templates for the ease of updating the content and avoid updating content manually in the flow if the content should change. And, we wanted to add the knowledge base article as regarding the email sent to the owner to see the activities in the timeline for the users and the knowledge base article.

After some testing, we found that when trying to set regarding tables that were not in the category list when creating an email template the “can’t instantiate template” error appeared each time. As can be seen in the image below.

Image of the “can’t instantiate template” and the reason why.

When testing with tables that were in the category list when creating email templates it worked without any problems. It looks like the tables that are allowed to be set as regarding when using the “SendEmailFromTemplate” action in Power Automate are the following:

  • Account
  • Campaign Activity
  • Case
  • Contact
  • Contract
  • Entitlement
  • Invoice
  • Lead
  • Opportunity
  • Order
  • Quote
  • Service Activity
  • System Job
  • User
Image of the tables allowed to set as regarding in the “SendEmailFromTemplate” action in Power Automate.

The first flow attempt to solve the task

In the first attempt, we used the Dataverse action step “Perform an unbound action” and the action name “SendEmailFromTemplate”. In this action, we tried to set a knowledge base article as the regarding table to track activities on the timeline of the knowledge base article and the owner. As we looked at earlier it simply wouldn’t work as intended. The image of the flow below is the setup of the first attempt, it is a working flow as long as you don’t refer to tables that are not present in the category list in the email template setup. In the next part we look at how we worked around this issue and managed to set knowledge base articles as regarding the email without having to save the content directly in the flow.

Image of the first Power Automate flow to send email notifications using Dynamics email template.

[Solved] “Can’t instantiate template” Error

In this section, we look at how we solved the problem by adding the knowledge base article to the email sent to the owner using the email template in Dynamics.

The flow looks much the same as the above with some other actions. Instead of using the Dataverse action step “Perform an unbound action” and the action name “SendEmailFromTemplate”. We used the action Get row by id to get the email template with the notification and created a new email message and populated the subject, description, added the knowledge article regarding the email, and then used the action “Perform a bound action” to send the email.

Let’s look more into the flow under the image below.

Overview of the Power Automate flow using an email template.

Walk through the Power Automate flow

In this section we go through the flow in more detail. To start with we look at the first four actions in the flow, more description under the image.

  1. In the first part of the flow, we initialize two variables that are used later in the flow. The reason behind the variables is that it is easier to maintain and update/change values later if they should change. You have one place to change the value and all other places where the variable is referenced are updated as well. The first variable stores the email template id.
  2. The second variable stores the queue id that is used as the sender of the email notification.
  3. In step three we retrieve the notification email template using the Dataverse action Get Row by Id and reference the emailTemplate variable as the Id. The email template may look like this.
Image of the email template used in this flow.

4. In step four we use the Dataverse List Rows action to retrieve the Knowledge Articles that are about to expire that we want the owners to get a notification to review or update. The Fetch XML query looks like this:

<fetch>
  <entity name="knowledgearticle">
     <attribute name="knowledgearticleid" />
    <attribute name="statecode" />
    <attribute name="ownerid" />
    <attribute name="expirationdate" />
    <attribute name="subjectid" />
    <filter>
      <condition attribute="expirationdate" operator="next-x-days" value="31" />
      <condition attribute="statuscode" operator="eq" value="7" />
    </filter>
  </entity>
</fetch>

For the next steps in the flow see the image below and descriptions under the image.

5. In the next step we loop through all knowledge articles from the list rows action.

6. Check if the difference in days between the expiration date and today’s date is equal to 30. The expression on the left calculates the difference between the expiry date and today’s date and finds how many days are different between them and then checks if it is equal to 30. This way we only send one notification when there are 30 days before the expiry date and not every day until the expiry date.

div(
    sub(
        ticks(items('Apply_to_Each_Knowledge_Article')?['expirationdate']),ticks(utcNow())
    ),
        864000000000
)

7. Furthermore, we need to convert HTML to text. The subject field in the template comes in as HTML and must be converted so that the subject field in the e-mail is not filled with HTML code, only text displayed in the subject field in the template.

8. Almost there, in this step we create the email and reference everything we need for the owner to get a notification using email template. The create email step is the record Dataverse Add a row action and we specify the values from the code block below. In the block below the words From, To, Regaring (Knowledge Articles), Subject and Description represents the fields in the action and everything behind is the references to the values from the previous steps in the flow, specified through the Dynamic content dialog.

Creates a new Email Message and specifies the following values from the actions above: 

From: queues(@{variables('queueId')})

To: systemusers(@{items('Apply_to_Each_Knowledge_Article')?['_ownerid_value']})

Regarding (Knowledge Articles): knowledgearticles(@{items('Apply_to_Each_Knowledge_Article')?['knowledgearticleid']})

Subject: @{body('Convert_HTML_to_Text')}

Description: @{outputs('Get_Email_Template_Knowledgebase_Article_Expiration_Date')?['body/safehtml']}

9. The last step is to send the email using the Dataverse action “Perform a bound action” where we reference the newly created email message and the type of action we want.

Image of the Send Email Message action

That was more in dept go through of the flow. Make sure to check out the images along the the description to get the most out of the technical parts.

Result (Gif)

In this section we look at have it looks during a flow run. Presented as a gif below.

A gif showing how it works during the av flow run.

Conclusion

With this solution, the content lives in the email template and the flow gets the email template at each run. If the content in the template is updated it will retrieve the latest content the next time the flow runs. The flow will stay untouched and no need to update the email content within the flow. There might be some limitations to using it this way, that I have not tested. When using dynamic values in your email templates and how this will work using templates in this way I’m not sure how it behaves. I have not tested this yet.

Hope you found the content useful!

Feel free to check out some of the latest posts