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...
Suggested Answer

Email with attachment is not working or Report as PDF is not working

(0) ShareShare
ReportReport
Posted on by 23

Hi

I have a codunit where I wanted to create a report as PDF and attached it to the email.  It is a custom table and custom report.

can anyone suggest a mistake here?

//-----------------------------------------------

             AttachmentTempBlob.CreateOutStream(OutStr);
            Rep50100.GetArgs(mailRec."Customer No", mailRec."Order No", mailRec."Price Date", WorkDate(), '');
            REPORT.SAVEAS(50100, '', REPORTFORMAT::Pdf, OutStr, RecRef);
            AttachmentTempBlob.CreateInStream(AttachmentInStream);
            EmailMessage.AddAttachment(Filename, 'PDF', AttachmentInStream);
            EmailMessage.Create("Send to", Subject, 'Price Notification');
            Email.Send(EmailMessage, Enum::"Email Scenario"::"Gas Price Email");
//---------------------------------------------
Please suggest the corrections
Warm regards
  • Suggested answer
    Govinda Kumar Profile Picture
    2,310 Moderator on at

    Hi hafiz,

    this is the simple code, you test and modify it according to your requirements

            ReportParameters := ReportExample.RunRequestPage();
            TempBlob.CreateOutStream(OutStr);
            Report.SaveAs(Report::"Standard Sales - Invoice", ReportParameters, ReportFormat::Pdf, OutStr);
            TempBlob.CreateInStream(InStr);
    
            EmailMessage.Create('youremail@gmail.com', 'This is the subject', 'This is the body');
            EmailMessage.AddAttachment('FileName.pdf', 'PDF', InStr);
            Email.Send(EmailMessage, Enum::"Email Scenario"::Default);

    Hope it helps

    Regards

  • Hafiz@d365 Profile Picture
    23 on at

    Thank you

    I wanted to send a record as recordset to report 50100. it is a single record report, that is being formatted in the report, which is our attachment.

    this following syntax may not work, last time I tried

     Report.SaveAs(Report::"Standard Sales - Invoice", ReportParameters, ReportFormat::Pdf, OutStr);

  • Suggested answer
    Saurav.Dhyani Profile Picture
    14,468 User Group Leader on at

    Hi,

    Hope this helps.

  • Suggested answer
    Gianfranco C. Profile Picture
    374 on at

    hi, try this code:

            DataTypeMgt.GetRecordRef(mailRec,RecordRef);  //codeunit "Data Type Management"
    
            AttachmentTempBlob.CreateOutStream(OutStr);
            Report.SaveAs(ReportID, '', ReportFormat::Pdf, OutStr, RecordRef);
            AttachmentTempBlob.CreateInStream(InStr);
            EmailMessage.AddAttachment(Filename, 'PDF', InStr);
            EmailMessage.Create("Send to", Subject, 'Price Notification');
            Email.Send(EmailMessage, Enum::"Email Scenario"::"Gas Price Email");

  • ShanAbeywicrema Profile Picture
    954 on at

    Saurav,

    I am using following code to generate pdf , then attach and send email. It is working fine. But problem is few pdf are corrupted. But when I send these pdf individually (not as bulk) then it is working. Again, corrupted file is working fine in all pdf readers except acrobat reader..

    if SalesInvoiceHdr.FindFirst() then

               repeat

                   clear(RecRef);

                   SalesInvHdr.Reset();

                   SalesInvHdr.SetRange("No.", SalesInvoiceHdr."No.");

                   if SalesInvHdr.FindFirst() then

                       RecRef.GetTable(SalesInvHdr);

                   tempBlob.CreateOutStream(outStreamReport);

                   tempBlob.CreateInStream(inStreamReport);

                   body := '';

                   emailMsg.Create('shan.abeywickrema@abcsd.com.au', 'Your invoice ' + SalesInvHdr."No." + ' has been processed', body, true);//SalesInvoiceHdr."Sell-to E-Mail"

                   Report.SaveAs(Report::"Posted Proforma Invoice", '', format::Pdf, outStreamReport, recRef);

                   //txtB64 := cnv64.ToBase64(inStreamReport);

                   emailMsg.AddAttachment('Sales Invoice ' + SalesInvHdr."No." + '.pdf', 'pdf', inStreamReport);

                   email.Send(emailMsg);

               until SalesInvoiceHdr.Next() = 0;

    Do you find any issue with this code?

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 710 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 387 Super User 2026 Season 2

#3
AndrewThomas81 Profile Picture

AndrewThomas81 385 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans