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

E-mailing a PDF report

(1) ShareShare
ReportReport
Posted on by 789
Using BC 365 SaaS v23.5. I've run across a few threads along these lines, and have tried to incorporate the AL code to no avail. The idea is I have added a custom action to a list page for e-mailing out a specifc custom report I have created. If I leave out the file attachment portion of my code then the e-mail sends out fine. Any suggestions? I'll paste my AL source code, along with a screen shot of the error message that occurs when I include the PDF file attachment.
 
////
 
trigger OnAction()
var
  TxtDefaultCCMailList: List of [Text];
  TxtDefaultBCCMailList: List of [Text];
  TxtRecipientsList: List of [Text];
  AttachmentTempBlob: Codeunit /Temp Blob/;
  AttachmentInStream: InStream;
  AttachmentOutStream: OutStream;
  FileMgt: Codeunit /File Management/;
  SalesInvoiceHdr: Record /Sales Invoice Header/;
  RecRef: RecordRef;
begin
  GetSalespersonEmail();
  GetCustomerEmail();
  if (SalespersonEmail <> '') and (CustomerEmail <> '') then begin
    TxtRecipientsList.Add(CustomerEmail);
    TxtDefaultCCMailList.Add(SalespersonEmail);
    EmailMsg.Create(TxtRecipientsList, 'Thank you for your purchase!', 'Attached is your receipt. Please keep for your records.', false, TxtDefaultCCMailList, TxtDefaultBCCMailList);
    AttachmentTempBlob.CreateOutStream(AttachmentOutStream);
    SalesInvoiceHdr.Reset();
    SalesInvoiceHdr.SetFilter(/No./, '=%1', Rec./Invoice Document No./);
    if SalesInvoiceHdr.FindFirst() then begin
      RecRef.GetTable(SalesInvoiceHdr);
      Report.SaveAs(Report::/DCH Sales Invoice Report/, '', ReportFormat::Pdf, AttachmentOutStream, RecRef);
      AttachmentTempBlob.CreateInStream(AttachmentInStream);
      EmailMsg.AddAttachment(Rec./Invoice Document No./ + '.pdf', 'PDF', AttachmentInStream);
      EmailObj.Send(EmailMsg, Enum::/Email Scenario/::Default);
      Message('Message sent!');
    end;
  end;
end;
 
var
  EmailObj: Codeunit Email;
  EmailMsg: Codeunit /Email Message/;
  SalespersonEmail: Text[80];
  CustomerEmail: Text[80];
I have the same question (0)
  • Verified answer
    Yi Yong Profile Picture
    2,661 Super User 2026 Season 1 on at
    Hello Greg,
     
    Did you try running the DownloadFromStream function to see if the report saved is valid?
  • Suggested answer
    Saurav.Dhyani Profile Picture
    14,419 User Group Leader on at
    Hi Greg,
     
    Please refer to this YouTube playlist for all questions related to Email in Business Central.
     
     
    This have explanation and Source Code on GitHub. This series covers all email related features. 
     
    Let me know if anything is missing in this playlist.
     
    Regards,
    Saurav Dhyani
  • Greg Kujawa Profile Picture
    789 on at
    Appreciate the tip about leveraging the DownloadFromStream function. Indeed the report was empty. I forgot that this report has a globally-defined variable used when it's manually run from the BC page. The fact that this custom action didn't define the variable led to the report being empty. I addressed that and am now good to go.
     
    I tried to mark this as the answer, but the web client just sits there with the spinning wheel. But this indeed was the point in the right direction I was looking for. Thanks so much!

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 March Top 10 Community Leaders

These are the community rock stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,933 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,055 Super User 2026 Season 1

#3
Teagen Boll Profile Picture

Teagen Boll 640 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans