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 289
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];
  • Greg Kujawa Profile Picture
    289 on at
    E-mailing a PDF report
    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!
  • Suggested answer
    Saurav.Dhyani Profile Picture
    14,312 Super User 2025 Season 1 on at
    E-mailing a PDF report
    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
  • Verified answer
    Yi Yong Profile Picture
    1,927 Super User 2025 Season 1 on at
    E-mailing a PDF report
    Hello Greg,
     
    Did you try running the DownloadFromStream function to see if the report saved is valid?

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,280 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,044 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans