Skip to main content

Notifications

Announcements

No record found.

Business Central forum
Suggested answer

Add an Email body from Report Selection Email body to Email Job Queue

Posted on by 458

Hi, 

I have created a new report that is sent as an email as soon as a Cash Receipt Journal has been posted. 
The Customer asked us to add an email body that come from a word report layout. 
I will attach a snippet of my code that is responsible for sending the email. 

Question: How do I replace the email body to be retrieved from a Report Layout instead of hardcoded HTML code?

 [EventSubscriber(ObjectType::Codeunit, Codeunit::"Gen. Jnl.-Post Line", 'OnAfterRunWithoutCheck', '', false, false)]
    local procedure OnAfterRunWithoutCheck(var GenJnlLine: Record "Gen. Journal Line")
    var
        Email: Codeunit Email;
        EmailMessage: Codeunit "Email Message";
        AttachmentInstream: InStream;
        ReportSelection: record "Report Selections";
        IntDocHeader: Record "Int Document Header";
        SalesOrder: Record "Sales Header";
        ReportSelectionCustomerRcpt: Record "Report Selections";
        AttachmentTempBlob: Codeunit "Temp Blob";
        ServerAttachmentFilePath: Text[250];
        ReportSelectionUsage: Enum "Report Selection Usage";
        RecVar: Variant;
        RecordRf: RecordRef;
        ToEmailAddress: Text[250];
        ToEmailSubject: Text[250];
        ToEmailBody: Text[250];
        //Convert Quote to Order
        SalesQuote: Record "Sales Header";
        SalesQuoteToOrder: Codeunit "Sales-Quote to Order";
        //API
        APIManagement: Codeunit "BWA API Management";
        UtopiaAPISetup: Record UtopiaAPISetup;
        //Convert Quote to Order
        Customer: Record customer;
        //IntegrationDocumentLink
        IntBusinessProcess: Codeunit "Int Business Process";
        CustomerLedgEntries: Record "Cust. Ledger Entry";
    begin
                //Dummy report to print, to update when new report is ready.
                ToEmailAddress := ReportSelection.GetEmailAddressExt(ReportSelectionUsage::"Customer payment receipt".AsInteger(), RecordRf, GenJnlLine."Account No.", ReportSelectionCustomerRcpt);
                ToEmailBody := 'Dear Customer, please find attached an acknowledgment of payment receipt';
                if ToEmailAddress <> '' then begin
                    ToEmailSubject := GenJnlLine."Account No." + ' - ' + GenJnlLine.UtopianJobDocNo + ' - ' + FORMAT(ReportSelectionUsage::"Customer payment receipt");
                    RecordRf.Get(GenJnlLine.RecordId);
                    RecordRf.SetTable(GenJnlLine);
                    ReportSelection.BWAGetPdfReportForCust(AttachmentTempBlob, ReportSelectionUsage::"Customer payment receipt", RecordRf, GenJnlLine."Account No.");
                    EmailMessage.Create(ToEmailAddress, ToEmailSubject, ToEmailBody);
                    AttachmentTempBlob.CreateInStream(AttachmentInstream);
                    EmailMessage.AddAttachment('Payment Receipt.pdf', 'pdf', AttachmentInstream);
                    Email.Send(EmailMessage, Enum::"Email Scenario"::"Customer payment receipt");
                end;
            end;
    end;
  • Suggested answer
    Marco Mels Profile Picture
    Marco Mels on at
    RE: Add an Email body from Report Selection Email body to Email Job Queue

    Hello,

    We currently do not have dedicated Dev support via the Dynamics 365 Business Central forums, but I wanted to provide you some additional resources to assist. If you need assistance with debugging or coding I would recommend discussing this on one of our communities.

    www.yammer.com/dynamicsnavdev

    www.dynamicsuser.net/.../14

    I will open this up to the community in case they have something to add.

    Thanks.

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,995 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,610 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans