Send Word Layouts for Email Body

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:

  1. Checks the invoice – ensures the invoice is completed or fully paid before sending.

  2. Finds the right email addresses – uses the customer’s “Sell-to Email”, and if available, also includes an Approver’s email.

  3. Uses a Word layout as the email body – gives your email a polished, branded appearance using your existing Word report layout.

  4. Creates a PDF of the invoice – automatically generates and attaches the PDF invoice to the email.

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

Popular posts from this blog

Run a Dynamics 365 Plugin from JavaScript on Button Click

Data Upload into CRM from Excel Using Power Automate

Add Java Script and CSS in PowerApps Portal