D365 Business Central : Send Invoice Email with Attachments

By standard, D365 Business Central can send posted sales invoice email to the customer. What if you want to include other files in the email as well ? I will describe it here on how to customize it to include all the attachments in the sales invoice email.

To achieve this is quite easy, you only need to subscribe to OnBeforeSendEmail on Codeunit Document-Mailing. You can then call the procedure to add the attachments to the Email Item.

[EventSubscriber(ObjectType::Codeunit, Codeunit::"Document-Mailing", 'OnBeforeSendEmail', '', false, false)]
local procedure DocumentMailing_OnBeforeSendEmail(var ReportUsage: Integer; var TempEmailItem: Record "Email Item"; var IsFromPostedDoc: Boolean; var PostedDocNo: Code[20])
begin
    If IsFromPostedDoc And (ReportUsage = Enum::"Report Selection Usage"::"S.Invoice".AsInteger()) then
        SendInvoiceAttachments(PostedDocNo, TempEmailItem);
end;

local procedure SendInvoiceAttachments(PostedSalesInvoiceNo: Code[20]; var TempEmailItem: Record "Email Item")
var
    DocumentAttachment: Record "Document Attachment";
    TempBlob: Codeunit "Temp Blob";
    FileInStream: InStream;
    FileOutStream: OutStream;
begin
    if PostedSalesInvoiceNo = '' then
        exit;

    DocumentAttachment.Reset();
    DocumentAttachment.SetRange("Table ID", Database::"Sales Invoice Header");
    DocumentAttachment.SetRange("No.", PostedSalesInvoiceNo);
    If DocumentAttachment.FindSet() then
        repeat
            If DocumentAttachment."Document Reference ID".HasValue then begin
                Clear(TempBlob);
                TempBlob.CreateOutStream(FileOutStream);
                TempBlob.CreateInStream(FileInStream);
                DocumentAttachment."Document Reference ID".ExportStream(FileOutStream);
                TempEmailItem.AddAttachment(FileInStream, DocumentAttachment."File Name" + '.' + DocumentAttachment."File Extension");
            end;
        until DocumentAttachment.Next() = 0;
end;

Let’s see how it works. Here we have posted sales invoice with two attachments.

Let’s try to send email, using the Email action.

We can see that the two attachments are included in the email.

This method also works when you do Post and Send from sales documents. It will post and send the email with the attachments.

That’s it ! Hope that helps you if you need to send invoice with the attachments.

GitHub Link.

App Release.

thatnavguy

Experienced NZ-based NAV Developer and Consultant with 15+ years of experience leading multiple IT projects, performing business analyst, developing, implementing, and upgrading Dynamics NAV and Business Central. Passionate to deliver solution that focuses on user-friendly interface while keeping high standard of compliance with the needs.

You may also like...

9 Responses

  1. Heather Roggeveen says:

    Hi
    I had a client desperate for this functionality and implemented your code which appeared to work brilliantly. However, we have an issue that when more than 2 attachments, the PDF gets corrupted and can’t be opened. I replicated that in my own environment. Is there any way to resolve that?

  2. Hassan Sadiq says:

    If I want to combine the pdf attachments into a single pdf and then send it by email, is that possible?

  3. ange says:

    Great post it help a lot, there is a way to attach the XML of the invoice? y tried with “modify” the “email” button and using the instream and outstream methods but it doesnยดt works, i used the Signed Document XML, any idea of to attach it?

  1. 12 July 2022

    […] Source : That NAV Guy Read more… […]

Leave a Reply

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