We highly recommend using the newly certified DocumentsCorePack (DCP) connector rather than the alternative method described in this article. Please go to this article to learn how to use this recommended certified connector.
Power Automate is becoming more powerful every day and will potentially replace the long-standing classic Dynamics 365 workflows. In this article, we will showcase how DCP allows us to generate and process documents within Power Automate (Microsoft Flow).
Continue reading if you prefer to learn more about the alternative method.
How it works:
When using DCP, every document that has to be generated is defined by a record of an entity called AutoMergeWorkingItem. Here is the record of all mandatory definitions to generate the document:
- Record to use: Defines what record the document should be generated for (account, quote, etc.).
- Template to execute: Defines the DCP template that should be used to generate the document.
- Output format: The filetype that the document should be generated and returned as (pdf, docx, html, etc.).
- (Optional) Processing settings: DCP allows you to not only generate a document, but to also process the document by definitions on the AutoMerkeWorkingItem record (e.g. Print, Save to SharePoint, Send for e-Signing, etc.).
Once such an AutoMergeWorkingItem is created, the DCP service will generate the document and deliver it back as an attachment to this record. Essentially, in order to generate a document within Flow, you must Create an AutoMergeWorking Item Record.
Note: The mandatory parameters required can be brought into the flow in various ways. For example, you could build a Canvas App that passes the parameters to the Flow, or you can simply use the Dataverse connector or Dynamics 365 connectors within Flow to load them (see figure below).
Figure 1: Document generation request schema of DocumentsCorePack
Step-by-Step: Create an AutoMergeWorkingItem in Power Automate (Microsoft Flow)
Now that we know what is needed: Let’s take a look at what such a document generation request must look like. Our use case is to generate a quote document in PDF format upon activation of the quote.
Note: In our scenario, we are using the Dataverse connector.
The Trigger of the flow: In our scenario, the flow is designed to be triggered by a change of the status field of the quote entity. Start with the When a row is added, modified or deleted step and then fill out the information as follows:
Figure 2: The Trigger of the flow
What’s important: We will need the context of this step as it defines the record our document needs to pull the data from. So, we have the first required parameter: The record to use.
Get Template to execute: Here we use the Dataverse “Get a row by ID” action to load our template. Since we always want to use the same quote-template, we make it simple and “hardcode” our template.
Figure 3: Get template to execute
So where does this GUID in the item identifier come from? To answer this, you must know that every template designed with the DCP Template Designer is stored as a record of the mscrm-addons.com DocumentsCorePack Template entity. As shown in the image below, this is the entity we are getting a record of.
You can open the template record you want to use and copy the GUID from the URL, as highlighted below.
To summarize: This Get a row by ID step will provide us with the second parameter: The template to use. As we know that our output format should be PDF, we are all set to finally define our document generation request.
Create AutoMergeWorking Item: As all parameters are now clear, we can use the Dataverse “Add a new row” action to create our document generation request as follows.
We create a record of the entity MSCRM-ADDONS.COM AutoMergeWorkingItem and choose the action to be Create Document. We can also give it a name to later identify and review the documents generated via this Flow. Now we need to set our parameters.
a. Record to pull data from (PrimaryRecordUrl).
In the PrimaryRecordUrl simply insert the ID of the record (Unique identifier of the quote from our Flow-Trigger action).
b. Template to Execute (TemplateToExecute).
Here we reference the Unique Identifier parameter of our Get a row by ID step, where we loaded the template above. This needs to be specified with the schema name of the template entity ptm_mscrmaddons_dcptemplateses(DocumentsCorePack Template).
c. FileType (SaveAs Value): This is a simple one – we simply select “PDF” from our pick list.
Figure 8: FileType (SaveAs Value)
And that’s it! So every time a quote gets activated, we will now have a document generated.
Naturally you want to do something with the document once it is generated. As outlined above, you can either use the AutoMergeWorkingItem to process the document, or you can process the document within your flow. The generated document can be found on the AutoMergeWorkingItem record -> Created Document section.
Note: This field will be filled when DCP has successfully created the document. You will have to wait for the AutoMergeWorkingItem record to become inactive. This tells you that the document is ready.
Figure 9: AutoMergeWorkingItem information: Inactive status
That’s it! We appreciate your feedback! Please share your thoughts by sending an email to firstname.lastname@example.org.