Create Project eInvoice with embedded and merged PDF Attachments

Let’s say that we want to send eInvoices from Dynamics 365 for Finance and Operations to our customers. In addition to the invoice data in XML, we also want to send along the invoice document in PDF format and some other related documents (e.g. specifications).

The built-in D365FO functionality supports eInvoice formats from the Electronic reporting framework. You can set up these ER formats in Accounts receivable parameters.

In this article we will use OIOUBL Project Invoice for Norway (used also for Denmark and Austria).

We will first show how to embed multiple Attachments from the Project invoice journal into the output XML file by using the standard D365FO, where one of them is the Project invoice document generated in PDF format during the posting process.

Next, we will explain how to combine the invoice document and the rest of the journal’s Attachments by merging them into a single PDF document, before it gets embedded into the output eInvoice XML file. This will require some custom code.

Embed report PDF and additional Attachments with standard D365FO

On the Customers form under the Invoice and delivery tab you can enable eInvoice and eInvoice attachment for a specific customer.

Set the eInvoice option to Yes to enable electronic invoices to be generated. It will trigger the eInvoice XML generation when posting an invoice.

Set the eInvoice attachment option to Yes to embed the invoice document generated in PDF format into the electronic invoice. It will create an invoice PDF document attached to the journal record, which is later embedded into the outgoing XML file.

The eInvoice and eInvoice attachment options are applicable to Project invoices and credit notes, Sales invoices and credit notes, Free text invoices and credit notes, and Collection letters.

When you enable eInvoice for a customer, you also need to do the eInvoice setup. Here you can find the tutorial how to setup OIOUBL eInvoice for Norway.

The standard schema for OIOUBL Project Invoice XML already supports multiple attachments. Let’s explain how this works.

On the Invoice proposal you can add attachments on two entities: header and transactions. Click on one of the fields of the desired section (below marked in red or blue) to add an attachment on the right entity (header or transaction).

If you want Attachments to be included in the eInvoice XML, you have to add them to ProjProposalJour (red) and set Restriction to External.

During posting all Attachments from Invoice proposal are copied to Invoice journal (ProjProposalJourProjInvoiceJour and ProjProposalOnAccProjInvoiceOnAcc).

The generated invoice document is attached to ProjInvoiceJour (with Restriction = External).

Electronic reporting job (named as Send the eInvoice XML), which generates the XML with all external attachments from ProjInvoiceJour, is created in Organization administration > Electronic reporting > Electronic reporting jobs with Status set to Waiting. After the job is processed and its Status is changed to Finished, the XML file will be created and attached to the Electronic reporting job record and will contain all external Attachment files from ProjInvoiceJour.

 

You can see that the resulting XML file contains all (external only) Attachment files. The last of them is the Project invoice PDF document.

If you are on the D365FO version 10.0.15 or lower the electronic reporting job is created before the ProjInvoice.Report attachment is added to ProjInvoiceJour. So the resulting XML depends on the timing when the job will actually be executed.

If it is executed before the document is attached, this attachment will not be included in the XML. However, if you resend the eInvoice, the XML will be created again with all attachments.

This is a bug and we have submitted it to Microsoft. It has been fixed in version 10.0.16. A similar bug for Sales invoice has been previously reported and fixed in 10.0.9.

Embed report PDF merged with additional Attachments

The standard solution described above works fine, but it has some drawbacks. We have no control over the order of the attachments and there is no information which is the main attachment and what is the purpose of the others. Multiple attachments are also not well supported by the recipient’s software.

A more reliable approach is to combine all attachments into one PDF. This way, the eInvoice XML contains only one embedded PDF and we can control its structure. This gives more confidence that the recipient will see and understand everything correctly.

Let's first describe how this custom solution works and then we will explain the code behind it.

The solution merges all PDF Attachments from ProjInvoiceJour into a single Attachment, which is later embedded into the XML. The steps before posting are the same as already described above for standard.

However, after posting ProjInvoiceJour contains only one merged Attachment that includes (in listed order): invoice document + external ProjInvoiceJour attachments + external ProjInvoiceOnAcc attachments (optional). Original Attachments from ProjInvoiceJour are deleted after merging (to avoid embedding them twice in the XML).

The XML now contains only one merged attachment.

Below is the sample code that implements the described scenario with one possible structure of a merged document. It can be further customized for a more specific use case.

Merging PDFs is implemented with APIs from Docentric Free Edition. You can use the solution from this article completely for free.

Summary

eInvoice XML can include PDF attachments for invoice visualization (printable invoice) and additional specifications of your electronic invoice. The standard D365FO supports one approach, while additional requirements can be covered with custom code presented in this article. The article presents various options that you can further customize for your business requirements.

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 >>