Create custom invoices from Microsoft Dynamics 365 CRM in Power Automate

This article shows how to generate customized invoices for a selected order in Dynamics 365 CRM using Power Automate (Microsoft Flow). Sometimes Dynamics 365 functionality for creating invoices is not enough if you need to include some additional information or even a company logo.

In this example, we will use the Plumsail Documents connector for Power Automate to create customized documents populated with Dynamics 365 CRM data.

Start document generation process action

Our Flow to do the job is simple:

Generate custom invoices from D365 orders Flow

Now let’s look into each step.

Configure process

Before you proceed to create the Flow, you need to create and configure the process which will generate PDF invoices from a DOCX template.

To start, register or login to your Plumsail account.

Create new process

Click on the Add Process button.

add process button

Give a name to the process and select DOCX for a template type.

Name process and select template type

Configure template

You clicked the Next button, and found yourself in the first step of the process - Configure template.

It includes two substeps:

  • Editor;

  • Settings.

In Editor, you can compose the template from scratch or upload a pre-made one. It’s also possible to modify the uploaded template online.

Feel free to download an invoice template used in this example.

Then upload it to the process.

upload template file

Templating syntax

To compose a template satisfying your demands and your circumstances, please, get acquainted with how Plumsail Documents Word DOCX templates work. There’s nothing complex here - everything between such curly {{ }} brackets is variables where the templating engine will apply your specified data. The syntax is easy yet powerful - it supports lists, tables, charts, and many more.

Test template

To see how the resulting file will look, click on Test template.

Insert source data in JSON into the appeared dialog. This JSON data tells the templating engine what it should place into {{ }} brackets instead of object names. It’s essential that this data must correspond to tokens from the template.

Test template to see how the resulting file will look

If you’re using the invoice template from this example, you can copy and paste sample JSON presented below.

Note

This is JSON for testing. We will pass data from Dynamics 365 CRM to the process. See the Start process section.

{
  "date": "2020-07-15",
  "email": "[email protected]",
  "customer": "David Navarro",
  "company": "Sample LLC",
  "address": "5820 Eastman Ave, Midland, MI, 48640",
  "phone": "(781) 749-8798",
  "order_details": [
    {
      "productname": "Website development",
      "extendedamount": 3740
    },
    {
      "productname": "Website audit",
      "extendedamount": 1790
    }
  ],
  "amount": "5530",
  "tax": "0",
  "total": "5530"
}

Click Save & Next to proceed to Settings. Here you’ll see the following parameters:

Mode. It’s Testing by default, which means runs of this process are free of charge for you, but the resulting documents will have a Plumsail watermark. To remove it, switch mode to Active.

Output filename. To personalize it, use tokens from the template. They will work the same way as for the template. For example, we have {{customer}} token. The real customer name will replace this token, and we’ll get the file “Invoice for David Navarro”. It will change dynamically according to specified data.

Output type. The default is the same as the template’s format. It’s possible to select PDF.

Test template. Once you’ve customized all the settings, you can test the template to see the result as we did it previously.

Configure template for Dynamics CRM step

When everything is done here, click on Save & Next to set up deliveries.

Delivery

It’s possible to add as many deliveries as you need. For instance, you could save the resulting invoice to your SharePoint library or OneDrive folder, then send as an e-mail attachment to the customer. Check out the full list of available deliveries.

We’ll add an email delivery for demonstrating purpose. It will deliver the invoice completed with Dynamics 365 data to the customer.

We put token {{email}} as a recipient’s email address. So, it will adjust dynamically every time according to the specified data. We’ll pull it from Dynamics 365 CRM.

We filled in the subject and email body.

email delivery

Start process

We’ll start the process from Power Automate (Microsoft Flow).

Create Flow

We’ve decided to trigger the Flow for a selected record (order) in Dynamics 365 CRM.

For that, we use Common data service - When a record is selected.

When a record is selected

The environment parameter is Default, the entity name is Orders:

When a record is selected trigger

Besides, we added an input date to be able to use the trigger date in the template.

The trigger is done, the next step is - Get a record.

Get a record

This action is from Common data service too. We need to assign it to pull the details on the order products and their properties as the trigger alone won’t provide us with this data.

Get a record step
  • Entity name - Orders;

  • Item ID - Select Order from the dynamic content of the trigger output.

  • It’s important to expand advanced settings and customize Odata query. Insert order_details into the Expand Query field. Otherwise, the Get a record action will return the same data as the trigger - without information about products related to the order.

Now save the Flow and launch a test run. After it ran successfully, copy JSON data from outputs of Get a record.

Get a record outputs

You’ll need it in the next action - Parse JSON.

Parse JSON

We assign this action to pull out data on products separately from other data we don’t need.

Parse JSON action
  • Content - Select Body from the dynamic content of Get a record outputs.

  • Schema - Click on Generate from sample and paste JSON you’ve copied earlier into the dialog.

We’re moving to the last step - Start document generation process.

Start document generation process

This is the action from the Plumsail Documents connector for Power Automate - it will start the process we’ve configured.

If it’s your first time using Plumsail Documents actions in Flow, be ready to create a new connection and provide Power Automate with Connection name and Access key:

create flow connection

You can type any name for the connection. For example, Plumsail Documents.

Then create an API key in your Plumsail Account page, copy and paste it to Access Key field.

The Start Document generation process has two parameters:

  • Process name - Just select from the dropdown.

  • Template data - specify it with dynamic content from the trigger, which pulls data from Dynamics CRM. To specify products array, insert order_details - you can find it in dynamic content of the Parse JSON output. No need to wrap it with quotation marks.

Start document generation process action

That’s it. To launch the Flow, select an order you need to generate an invoice for -> in the top navigation menu go to Flows -> and choose the Flow:

Start document generation process action

Once the Flow ran successfully, the customer receives a personalized email with the invoice attached:

invoice created from dynamics 365 CRM

Use ready invoices in Flow

It’s possible to continue working with the generated invoices from Dynamics 365 further in Flow. For example, you can send them for approval. Just add the result file returned by Start document generation process action as an attachment:

Send Dynamics 365 invoice for approval

Conclusion

Now you know how to generate customized invoices with logos from Microsoft Dynamics 365 using Plumsail Documents in Power Automate Flow. Utilize this approach to create any custom documents from any entities in Dynamics 365.