Skip to main content

Notifications

Business Central forum
Answered

E-mailing a PDF report

editSubscribe (1) ShareShare
ReportReport
Posted on by 553
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];
Categories:
  • Greg Kujawa Profile Picture
    Greg Kujawa 553 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
    Saurav.Dhyani 10,155 User Group Leader 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
    Yi Yong 190 on at
    E-mailing a PDF report
    Hello Greg,
     
    Did you try running the DownloadFromStream function to see if the report saved is valid?

Helpful resources

Quick Links

Take the Community feedback survey!

Answer this brief 15-question survey about your Community experience…

Demystifying Copilot: Service Edition with Sundar Raghavan

Sundar answers more questions about Copilot for Service...

Dynamics 365 Business Central vs Finance and SCM

Take a look at the key differences between Business Central and…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,375 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 223,308 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,140

Featured topics

Product updates

Dynamics 365 release plans