Generate Quote PDF attachments in Dynamics through Flow
When needing to generate PDF's in Dynamics CRM/365, for example on a quote or invoice, you typically need to use a 3rd party solution/add-on to automate this or do it manually.
Dynamics has functionality you can work with but that just doesn't quite fit the requirement...case in point: Word templates. You can use them to generate a quote, but there is still the need for the user to save/convert the word document to PDF manually.
The scenario/business requirement we are looking to solve for is to automatically generate a PDF document based of a quote word template in Dynamics. The solution should be 'code free' where possible and not require any 3rd party add-ons. It should also be as user-friendly as possible. This is the solution I came up with:
- An on-demand workflow that runs on a quote and generates the Word document.
- A Flow that fires of the creation of a note record and does the PDF conversion and attaches the record back against the quote.
Simple eh!
I will elaborate on each of those steps now...
On-demand workflow to generate the document
Within the standard workflow functionality, there are out of the box 'actions' which you can call. The action we are going to call is called 'SetWordTemplate':
We select the document template we want to be generated for the record in context:
The workflow is available on-demand and this 'SetWordTemplate' action generates the Word document as note attachment against the quote record its been run against:
Microsoft Flow
This is now where you can see the benefits of using the Microsoft ecosystem..it makes sense. Dynamics 365 & Office 365 play well together! We have a Flow that triggers whenever a note is created in our Dynamics environment. We do a bit of validation to ensure that:
a.) The note is regarding a quote record and
b.) That it has a document attachment
After that, we do the following actions
1.) Get the original quote record so we can access the custom quote ID field to name the PDF appropriately
2.) Create the file (.docx) in OneDrive - so we can use the function in Step 3
3.) Use the OneDrive 'Convert to PDF' action that's available in Flow
4.) Create the note back in Dynamics against the quote with the PDF attachment
5.) Delete the original Dynamics note with the Word document
6.) Delete the file we created in step 2
Back in Dynamics, we now have the PDF attached against the related quote!
This solution is by no means a complete replacement for the enterprise document generation tools available to Dynamics. Feedback though from the stakeholders has been positive! The ability to run an on-demand workflow and have a PDF generated for you after a few moments, instead of manually converting a doc to PDF, has been a time saver. The only annoyance is having to 'refresh' the notes grid to see if the PDF generation process has completed. If only there was a way to notify users once the PDF is generated 😉
Gotchas
I'll elaborate a bit more on the small tweaks you need to do when mapping attributes between the platforms:
In step 2.)
You need to use the base64ToBinary
function when creating the file in OneDrive. The parameter you pass is the DocumentBody from the note record that triggered the Flow:
In step 3.)
Ensure that the ID you pass as a value is the ID from the file created in step 2:
In step 4.)
Just this week (16/5/2018), the OneDrive team must have changed the structure of the data that is returned back from step 3. Previously you could map directly to the content from step 3 but now you need to access the $content attribute within the body content to map it back into Dynamics. This is what you put in your expression:
body('Convert_file')['$content']