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.
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?
Try adding
Clear(TempBlob);
If I want to combine the pdf attachments into a single pdf and then send it by email, is that possible?
If you use Document Sending Profile, there is a setup to Combine PDF Documents.
You need first to select the Sending Options to Email.
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?
Do you have an issue with creating the XML or attaching the XML file?
it is attaching the file, as you show in the examples the email attach the PDF of the invoice but I need the PDF and the XML
the xml is stored in the signed document xml field in the table sales invoice header