web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Workflow for Automatic Invoice Emails in Business Central

(0) ShareShare
ReportReport
Posted on by 485

Is there a way to make an automatic email system that sends weekly invoices/reports to customers?

I am asking this through a 'Developer's' perspective. Is there a way I can reach this solution with AL or any type of workflow tool?

I have the same question (0)
  • Suggested answer
    IB-29041624-0 Profile Picture
    1,189 Moderator on at

    There are many ways you can do that.

    The simplest way is to write a codeunit that runs the reports you want to send, attach them to an e-mail and send the e-mail.

    Then you can put that codeunit into the job queue to run repeatedly at the frequency you select.

    Here are some sample code that you can look at to get some ideas.

    PROCEDURE ReportSendMailWithExternalAttachment(
    ReportToSend: Integer;
    Recordr: RecordRef;
    TableID: Integer;
    DocNo: Text;
    ToAddr: List of [Text];
    Subject: Text[100];
    Body: Text[200];
    AttachmentName: Text[100];
    Param: Text; var Purchaseheader: Record “Purchase Header”): Boolean
    var
    TempBlob: Codeunit “Temp Blob”;
    outStreamReport: OutStream;
    inStreamReport: InStream;
    TempBlobAtc: Array[10] of Codeunit “Temp Blob”;
    outStreamReportAtc: Array[10] of OutStream;
    inStreamReportAtc: Array[10] of InStream;
    Parameters: Text;
    EmailObj: Codeunit Email;
    EmailMsg: Codeunit “Email Message”;
    Question: Label ‘Are you Sure you want to send an email to %1’;
    SendYesNo: Boolean;
    // Attachments
    FullFileName: Text;
    DocumentAttachment: record “Document Attachment”;
    i: Integer;
    
    begin
    EmailMsg.Create(ToAddr, Subject, Body, false);
    //Generate blob from report
    TempBlob.CreateOutStream(outStreamReport);
    TempBlob.CreateInStream(inStreamReport);
    Report.SaveAs(ReportToSend, Param, ReportFormat::Pdf, outStreamReport, Recordr);
    // Mail.AddAttachmentStream(inStreamReport, AttachmentName);
    i := 1;
    
    //Get attachment from the document – streams
    DocumentAttachment.Reset();
    DocumentAttachment.setrange(“Table ID”, TableID);
    DocumentAttachment.setrange(“No.”, DocNo);
    if DocumentAttachment.FindSet() then begin
    repeat
    if DocumentAttachment.”Document Reference ID”.HasValue then begin
    TempBlobAtc[i].CreateOutStream(outStreamReportAtc[i]);
    TempBlobAtc[i].CreateInStream(inStreamReportAtc[i]);
    FullFileName := DocumentAttachment.”File Name”   ‘.’   DocumentAttachment.”File Extension”;
    if DocumentAttachment.”Document Reference ID”.ExportStream(outStreamReportAtc[i]) then begin
    //Mail Attachments
    EmailMsg.AddAttachment(FullFileName, ‘PDF,’, inStreamReportAtc[i]);
    end;
    i  = 1;
    end;
    until DocumentAttachment.NEXT = 0;
    end;
    
    //Send mail
    exit(EmailObj.Send(EmailMsg));
    end;

    I found the code as a comment in this blog article:

    robertostefanettinavblog.com/.../

  • jdrco Profile Picture
    485 on at

    From the source code of the article, what is that code unit "SMPT Mail" and report "SMTP Mail Setup" that he makes? I cannot find that in my intellisense when creating those vars.

  • Suggested answer
    IB-29041624-0 Profile Picture
    1,189 Moderator on at

    The code is a bit cold so you should use the Email and Email message codeunits instead like in the code i had in my last post.

  • Suggested answer
    YUN ZHU Profile Picture
    101,416 Super User 2026 Season 1 on at

    Hi, Develop a Codeunit or Report that automatically sends emails, set it in the Job Queue, and run it once a week.

    pastedimage1656549890769v1.png

    A simple example of sending an email:

    https://yzhums.com/1799/

    Or, you can try the following standard feature.

    pastedimage1656549963513v2.png

    pastedimage1656549978885v3.png

    Hope this helps.

    Thanks.

    ZHU

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,216 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,471 Super User 2026 Season 1

#3
AndrewThomas81 Profile Picture

AndrewThomas81 1,369

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans