Automatically Email Sales Invoices with Word Layout Email Body and PDF Attachment in Microsoft Dynamics 365 Business Central.
Automatically emailing invoices with a nicely formatted email body and a PDF attachment is one of the most common Business Central automation needs.
With a simple AL customization, you can make your system send branded invoice emails automatically — saving time, improving customer experience, and ensuring professional communication every time.
What the automation does
When this automation runs, it performs these key steps:
-
Checks the invoice – ensures the invoice is completed or fully paid before sending.
-
Finds the right email addresses – uses the customer’s “Sell-to Email”, and if available, also includes an Approver’s email.
-
Uses a Word layout as the email body – gives your email a polished, branded appearance using your existing Word report layout.
-
Creates a PDF of the invoice – automatically generates and attaches the PDF invoice to the email.
-
Sends the email – uses Business Central’s standard email system to deliver the message directly to the customer.
Below AL code converts your Word layout email body into HTML text, which is then used as the email message body.
var
TempBlobPdf: Codeunit "Temp Blob";
TempBlobHtmlBody: Codeunit "Temp Blob";
EmailSender: Codeunit Email;
EmailMessage: Codeunit "Email Message";
SalesInvoiceRec: Record "Sales Invoice Header";
SalesInvRecRef: RecordRef;
ReportLayoutSelection: Record "Report Layout Selection";
ReportSelections: Record "Report Selections";
OutsHtml: OutStream;
InsHtml: InStream;
begin
if SalesInvoiceHeader."Sell-to E-Mail" <> '' then
Recipients := SalesInvoiceHeader."Sell-to E-Mail";
Subject := CompanyInfo.Name + ' Sales Invoice ' + SalesInvoiceHeader."No.";
if not SalesInvoiceRec.Get(SalesInvoiceHeader."No.") then
Error('Sales Invoice %1 not found.', SalesInvoiceHeader."No.");
SalesInvoiceRec.SETRECFILTER();
SalesInvRecRef.GetTable(SalesInvoiceRec);
ReportSelections.Reset();
ReportSelections.SetRange(Usage, ReportSelections.Usage::"S.Invoice Payment");
ReportSelections.SetRange("Use for Email Body", true);
if ReportSelections.FindFirst() then
LayoutCode := ReportSelections."Email Body Layout Code";
if LayoutCode <> '' then begin
TempBlobHtmlBody.CreateOutStream(OutsHtml);
ReportLayoutSelection.Get(ReportSelections."Report ID", CompanyName);
ReportLayoutSelection.SetTempLayoutSelected(LayoutCode);
Report.SaveAs(ReportSelections."Report ID", '', ReportFormat::Html, OutsHtml, SalesInvRecRef);
ReportLayoutSelection.SetTempLayoutSelected('');
TempBlobHtmlBody.CreateInStream(InsHtml);
InsHtml.ReadText(BodyHtml);
end else begin
BodyHtml := '<p>Dear ' + SalesInvoiceHeader."Sell-to Customer Name" + ',</p>' +
'<p>Please find attached your Sales Invoice ' + SalesInvoiceHeader."No." + '.</p>' +
'<br><br>' + CompanyInfo.Name;
end;
EmailMessage.Create(Recipients, Subject, BodyHtml, true);
TempBlobPdf.CreateOutStream(OutsPdf);
Report.SaveAs(ReportSelections."Report ID", '', ReportFormat::Pdf, OutsPdf, SalesInvRecRef);
TempBlobPdf.CreateInStream(InsPdf);
FileName := 'Sales Invoice ' + SalesInvoiceHeader."No." + '.pdf';
EmailMessage.AddAttachment(FileName, 'application/pdf', InsPdf);
EmailSender.Send(EmailMessage, Enum::"Email Scenario"::Default);
end;
Comments
Post a Comment