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

E-mailing a PDF report

(1) ShareShare
ReportReport
Posted on by 583
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,594 Super User 2025 Season 2 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,380 Super User 2025 Season 2 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
    583 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

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 2,116

#2
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 764 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 635 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans