Skip to main content

Notifications

Small and medium business | Business Central, N...
Suggested answer

Generate Emails using word layout

(1) ShareShare
ReportReport
Posted on by 6
Hi 
 
I am working a AL extension to send a reminder Email to due Invoice in BC. For this I am using Report Object and word layout  generate HTML email. To get the email details I am using customer ledger entries. 
 
IF Customer.FINDFIRST THEN
            repeat
                body := '';
                TotalRmtAmt := 0.00;
 
                newdate := (CALCDATE('<+1D>', TODAY));
 
                CustLedgEntry.SETCURRENTKEY("Customer No.", Open, Positive, "Due Date", "Currency Code");
                CustLedgEntry.SETRANGE(CustLedgEntry."Customer No.", Customer."No.");
                CustLedgEntry.SETRANGE(Open, TRUE);
                CustLedgEntry.SETRANGE("Due Date", newdate);
                CustLedgEntry.SETFILTER(CustLedgEntry."Document Type", '%1', CustLedgEntry."Document Type"::Invoice);
 
                IF CustLedgEntry.FINDFIRST THEN BEGIN
                    REPEAT
                        CustLedgEntry.CALCFIELDS("Amount (LCY)");
                        CustLedgEntry.CALCFIELDS("Remaining Amt. (LCY)");
 
                        TotalRmtAmt += CustLedgEntry."Remaining Amt. (LCY)";
 
                    UNTIL (CustLedgEntry.NEXT = 0);
 
                End;
 
                if not CustLedgEntry.IsEmpty Then begin
 
                    Clear(ReportOutStream);
                    Clear(ReportInStream);
                    Clear(body);
                    Clear(TempBlob);
 
                    TempBlob.CreateOutStream(ReportOutStream);
 
                    PaymentReminderInvoice.SetRunningValues(Customer."No.", newdate, TotalRmtAmt);
 
                    if PaymentReminderInvoice.SaveAs('', ReportFormat::Html, ReportOutStream) then begin
                        TempBlob.CreateInStream(ReportInStream);
 
                        ReportInStream.ReadText(body);
 
                        if Customer."E-Mail" <> '' then begin
                            emailMsg.Create(Customer."E-Mail", 'Invoices Due Tomorrow', body, true);
                            email.Send(emailMsg, Enum::"Email Scenario"::"Send Invoice Reminders");
                        end;
 
                    end;
                end;



 
            UNTIL (Customer.NEXT = 0);
 
 
First Customer received the email proper template 
 
Second customer onwards Return FALSE on below statement
 
   if PaymentReminderInvoice.SaveAs('', ReportFormat::Html, ReportOutStream) then begin
 
There is no data level error on 2nd Customer , I have hard corded 2nd customer and check, then successfully sent the email
 
This issue coming when loop thru customer list
 
please provide a solution for this 
 
Thank you
  • gdrenteria Profile Picture
    gdrenteria 12,290 Most Valuable Professional on at
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    Khushbu Rajvi. 4,744 on at
    Generate Emails using word layout
    It seems the PaymentReminderInvoice.SaveAs method returns FALSE for customers after the first one due to potential issues with report parameters or data. Ensure all necessary values are set correctly for each customer, and check that TotalRmtAmt is greater than zero before attempting to generate the report. Additionally, clear variables at the start of each loop iteration and consider adding logging to capture any anomalies that could help diagnose the issue.
  • Khushbu Rajvi. Profile Picture
    Khushbu Rajvi. 4,744 on at
    Generate Emails using word layout
    .

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

News and Announcements

Announcing Category Subscriptions!

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,359 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,370 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans