Power Automate is becoming more powerful every day and potentially is about to replace the long-standing classic Dynamics 365 workflows. In this blog, we will present and showcase how DocumentsCorePack allows us to generate and process documents within Power Automate (Microsoft Flow).
How it works:
When using DocumentsCorePack every document that has to be generated is defined by a record of an entity called “AutoMergeWorkingItem”. This record holds all mandatory definitions to generate the document:
1. Record to use: What record should the document be generated for (e.g. account, quote, etc.)
2. Template to execute: Defines the DocumentsCorePack template that should be used to generate the document.
3. Output format: The Filetype the document should be generated and returned as Pdf, docx, Html.
4. (Optional) Processing settings: This is not a necessity but DocumentsCorePack allows to not just generate a document but 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 DocumentsCorePack service will generate the document and deliver it back as an attachment to this record. It becomes clear that in order to generate a document within Flow the only thing that needs to be done is to “Create an AutoMergeWorking Item Record”.
Note: The mandatory parameters required can be brought into flow in various ways. E.g. you could build a Canvas App that passes the parameters to the Flow or you can simply use the Common Data Service or Dynamics 365 connectors within Flow to load them (see figure below).
Step-by-Step: Create an AutoMergeWorkingItem in Power Automate (Microsoft Flow)
Now that we know what is needed: Let’s take a look at how such a document generation request has to 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 common data service connector. Especially with the October 2019 update for the “Common Data Service (current environment)” it became a lot easier to do certain things like creating records. I want to emphasize though that it's of course also possible to use the Dynamics 365 connector.
1. The Trigger of the flow: In our scenario, the flow is defined to trigger a change of the status field of the quote entity as follows:
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!
2. Get Template to execute: Here we use the Common Data Service- 'Get Record' action to load our template. Since we always want to use the same quote-template we make it simple and “hardcode” our template.
So what do we have here? Where does this GUID in the item identifier come from? To answer this we have to know that every template designed with the DocumentsCorePack Template Designer is stored as a record of the “mscrm-addons.com DocumentsCorePack Template” entity. As can be seen on the screenshot this is the entity we are getting a record of.
You can e.g. open the template record you want to use and copy the GUID from the URL as you can see below.
To summarize: This 'Get Record' step will provide us with the second parameter: The template to use! As we also know that our output format should be PDF we are all set to finally define our document generation request!
3. Create AutoMergeWorking Item: As all parameters are now clear we can use the Common Data Service – Create Record 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 that is quite handy to maybe later identify and review the documents generated via this Flow. Now we need to set our parameters:
a. Record to pull data from (PrimaryRecordUrl):
This is kind of challenging at this point. We did copy the URL of a quote we opened in Dynamics and replaced it with the “Quote”-parameter (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 Record” step where we did load the template above.
c. FileType (SaveAs Value): This is a simple one – we simply select “PDF” from our picklist.
And that’s it! We did it. So every time a quote gets activated we will now get a document generated.
Next steps: Naturally you want to do something with the document once generated. As outlined above you can either use the AutoMergeWorkingItem to process the document but you can also 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 DocumentsCorePack successfully created the document. You will have to wait for the AutoMergeWorkingItem record to become “inactive”. This tells you that the document is ready.
That's it! We appreciate your feedback! Please share your thoughts by sending an email to firstname.lastname@example.org.