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

Notifications

Announcements

No record found.

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 481

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
    Inge M. Bruvik Profile Picture
    1,111 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
    481 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
    Inge M. Bruvik Profile Picture
    1,111 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
    95,597 Super User 2025 Season 2 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,010

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,270 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,085 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans