Skip to main content

Notifications

Community site session details

Community site session details

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

Generate Emails using word layout

(1) ShareShare
ReportReport
Posted on by 12
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
  • Gerardo Rentería García Profile Picture
    19,937 Most Valuable Professional on at
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    17,404 Super User 2025 Season 1 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
    17,404 Super User 2025 Season 1 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

Quick Links

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

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

#1
Sohail Ahmed Profile Picture

Sohail Ahmed 1,452

#2
YUN ZHU Profile Picture

YUN ZHU 1,313 Super User 2025 Season 1

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 1,083 Most Valuable Professional

Featured topics

Product updates

Dynamics 365 release plans