Generate PDF from Document Templates in CDS & Dynamics 365 CE using native Web API with Flow

Posted by

The Create Quote PDF capability and the option to enable PDF generation was introduced in version 9.0.1905.2010 of Dynamics 365 CE. It can be enabled in the App Settings of the Sales Hub and it will add two buttons to the ribbon of the Quote form: ‘Create PDF’ and ‘Email as PDF’ of a selected Word template.

The big question is whether this option is or will be available for other entities as well? The answer is: technically it is. I stumbled upon a blog post on the website of Thrives where the web requests were analysed when calling the ‘Create PDF’ action. It turns out to call the Web API endpoint:

https://{org}.{region}.dynamics.com/api/data/v9.0/ExportPdfDocument

with the following JSON submitted in the body:

{
       "EntityTypeCode": 1084,
       "SelectedTemplate": {
              "@odata.type": "Microsoft.Dynamics.CRM.documenttemplate",
              "documenttemplateid": "153dc496-d79d-e711-8109-e0071b65ce81"
       },
       "SelectedRecords": "[\"{E3A79DA1-9B91-E811-8133-E0071B65CE81}\"]"
}

The response will contain a “PdfFile” attribute which is a base64 representation of the PDF file.

{
     "@odata.context": "https://{org}.{region}.dynamics.com/api/data/v9.0/$metadata#Microsoft.Dynamics.CRM.ExportPdfDocumentResponse",
       "PdfFile": "JVBERi0xLjUNCjQgMCBvYmoNCjw8L1R5cGUgL1BhZ2UvUGFyZW50IDMgMCBSL0..."
}

The blog post ends with the possibilities of adding this code to a workflow assembly…. but our belief is #NoCode so let’s try this with Microsoft Flow. Yes we can just do it with Flow!

Here is the overview of the Flow:

The scenario is to start a Flow from an Opportunity record and select the Word template and the option to export the PDF document: Add to Note, Send by Email, Save to SharePoint.

We use the List records action and set the filter on the Name of the Document Template -assuming it’s unique- to get the id of the selected Document Template with the expression in the Compose action: first(body(‘List_Document_Template_record’)?[‘value’])?[‘documenttemplateid’]
Beware that the @odata.type key must be added like this to prevend an error: “@@odata.type”

To call the Dynamics 365 Web API we use the ‘Invoke an HTTP request’ action from the ‘HTTP with Azure AD’ connector, so we don’t have to deal with the authentication. Just fill out the base URL of your Dynamics 365 CE instance to sign in.

In the ‘Invoke an HTTP request’ action we do a POST request to the Web API endpoint ExportPdfDocument.

Then we parse the JSON of the response to get the “PdfFile” attribute for further processing. In this case I’ve only implemented the ‘Add to Note’ option. The Create record action will create a Note record with the PDF document as attachment and linked to the Opportunity record with the Regarding field.

Now let’s test the Flow by starting it from an Opportunity record.

Run flow will result in a new Note added to the Opportunity record, including the PDF document as attachment.

This will also work for custom entities. With the Metadata Document Generator plugin for XrmToolBox you’ll able to get the EntityTypeCode (Object Type Code) for your custom entities.

What about standard CDS? I’ve copied the Flow to the default CDS environment and modified it to use Word templates for Accounts. And it works, yes! So we have native PDF generation in CDS now.

4 comments

  1. Hi Stefe,
    I am trying to replicate the first step. How are you accessing the Document templates. I don’t find that options in Flow triggers or actions.

    1. I use the List records action of the CDS connector, but you know that now because we’ve got in touch via LinkedIn 😉

  2. As interesting as this may be to nocoders and as good as it is as an example, don’t ever do it. To create a quote document from an opportunity completely flies in the face of what we are doing with CE. Quotes are like contracts. CE enforces versioning on quote records to substantiate the official nature of such a document. Then there is the whole sales projection & planning aspect of the app which runs through the tracking of quotes. This wonderful example is in action just a circumvention of the system and of management.

    1. Good point, the Opportunity Summary is for internal use only. This is just an example to prove that is works.

Leave a Reply

Your email address will not be published. Required fields are marked *