Sending multiple customer invoices in a single email in Dynamics 365 for Finance and Operations

If you need to send out a single email to every customer each month, and this email needs to have all the customer-belonging invoices for the current billing period attached, check out this article that will describe such a solution for Dynamics 365 for Finance and Operations.

You can also download and use the described solution as is, or you can adjust it to your needs.

The prerequisite is to install Docentric AX Free Edition first, and of top of that the solution provided in this article.

The requirements

Let’s say that you have to post each of sales orders separately, which means that one sales order always produces exactly one invoice. This is not a problem by itself – you need to turn off the Summary order functionality, by selecting None for Summary update for on the Posting invoice form. The challenge that may arise is how to distribute all the created invoices at the end of the month to each customer.

Let’s assume that the requirement is the following: sending a single email at the end of each month (i.e. for the current billing period) to every customer that one or more sales orders (i.e. invoices) are created for in this month. So, a customer should receive a single email each month, with all his invoices attached. Such email needs to have a nicely designed body with information from the current billing period.

On the other hand, if your goal is to email multiple sales orders of the same customer posted as a single invoice (so called Summary invoice), this is the standard functionality provided OOTB. Learn more >>

The solution

The solution described here consists of two phases:

  • Post each sales order as a separate invoice and print it to Azure blob storage, using the Docentric File print destination. Store the location of the printed output file in Invoice journal. Store also information about the current billing period.
  • Fetch all printed invoice documents for the selected billing period for each of the customers and email them in the single email, using the selected Email template from Organization administration -> Setup -> Email templates.

The both steps can be run in batch.

Extending Invoice journal

We will extend the CustInvoiceJour table (and the Invoice journal form) with the following fields:

In the Print management setup for the Customer invoice report we will select the Docentric File print destination, and set up Saving to Azure blob storage settings as shown on the pictures below. Docentric print destinations are shipped with Docentric AX Free Edition that is completely free of charge, and you can download and use it right away.

The next step is to introduce an event handler DocReportRunDelegates_reportExecutionEnd for the reportExecutionEnd delegate defined on the DocReportRunDelegates class, which is invoked each time when printing a report to Docentric File print destination completes. In this method, only for invoices, we will store Azure blob path of the printed invoice document and the billing period inferred from the printing invoice date to the corresponding CustInvoiceJour record.

You can also use Microsoft’s Azure storage explorer to browse and manage these printed invoice files if needed.

Post and print sales orders

So, when we post and print a sales order, the extended fields on CustInvoiceJour table get filled as you can see on the pictures below. Also, if you have already posted some sales orders with the different target print destination settings, you can invoke the printing of the corresponding invoices again, by using the Print management from the Invoices journal form, in order to get the new DocReportRunDelegates_reportExecutionEnd event handler executed.

After an invoice document is printed and store on Azure blob storage, you can also download it by clicking the new Download button on the Invoice journal form.

[gallery_bank type=”images” format=”thumbnail” title=”true” desc=”false” responsive=”true” display=”all” sort_by=”pic_name” animation_effect=”bounce” album_title=”false” album_id=”23″]

Creating the email template for emailing multiple invoices

In order to send an arbitrary email using a predefined email subject and body, we will use Organization administration -> Setup -> Email templates.

 

Docentric AX Free Edition for Dynamics 365 for Finance and Operations also improves this Email templates functionality so you can edit email templates using a superior HTML editor with custom placeholders. We will first create an email template handler class to define some custom placeholders (e.g. customer contact name, customer account ID, billing period) we are going to use in our emails when emailing customer invoices.

Monthly emailing multiple invoices in the single email

This is the second phase of the solution. We have our monthly invoices posted and printed to Azure blob storage, and now we are going to create a functionality which will email them in the single email for each customer.

For that purpose we will create a dialog to select some parameters such as billing period, customer contact purpose or email recipients, etc. We will also include a combo box to select the MultiInv email template we have created in the previous step.

Note that this dialog uses the SysLastValue framework to store user choice for the form parameters so he or she doesn’t have to fill them each time the form is open.

Learn how to use the SysLastValue framework with this form >>

Email invoices in batch

Also, there is an option to run the whole operation in batch. You will notice a disabled checkbox Batch processing on the Email multiple invoices form, which is a signal whether the emailing invoices will be performed in batch when you click the OK button. You can turn it on or off on the Batch processing dialog that is shown when the Batch button is clicked. On this form you can schedule and set up a batch job for emailing invoices.

 

Learn in detail on how to add the Batch button to this form >>

Sending emails with invoices

When the OK button is clicked the DocEmailInvoicesManager::sendInvoices() method is called which first filters all CustInvoiceJour records by the selected billing period and groups them by customers. Afterwards, for each customer it fetches all the printed invoices stored on Azure blob storage and sends them as the attachments of the single email. The email is sent to all email addresses which have been resolved from the input parameters such as Purpose, Use primary contact or Send to.

Read how customer’s contact purpose and primary email can be resolved >>

After an email is sent, the information such as Sent via Email (NoYes::Yes), Email sent on (a timestamp), Sent to (resolved To email addresses), etc. are stored in each of the corresponding CustInvoiceJour records. In case that an email failed to be sent, Emailing error message is written to the corresponding CustInvoiceJour records.

 

 

Take a look at the method (a code snippet) for sending an email to the provided customer, with the attached invoices that are fetched from multiple CustInvoiceJour records selected by this customer and the provided billing period.

Solution improvements

There are several options to improve this solution. For example, if you have on-premises environments, you may prefer to use Docentric File print destination to save invoices to File system instead of Azure blob storage. You can even use placeholders to create smartly named folders and sub-folders for each billing period and/or customer, in order to have nicely organized printed invoices on your shared network folders.

You can also add the Cc and Bcc fields for sending emails, and use Number sequence with the Emailing invoices ID parameter.

Resources

Download the solution project >>
Download the solution model >>

Leave a Reply

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

*

Docentric respects your privacy. Learn how your comment data is processed >>

Docentric respects your privacy. Learn how your comment data is processed >>