The code is below:
Reminder term code for customers which need to be reminded must be :'REMIND'
This code is checking if an invoice which has remaining balance >0 and is due in 10 days.
It then checks for all the contacts of that customer and sends reminder email to their email addresses. If you want to change the number of days search for 'if (days = 10) ' in the code and update it with number of days before the due date on which you want to send reminders to customers.
For it to work SMTP setup should be working.
If you are mentioning a email address other than the smtp configuration, give the smtp configured user access to the mailbox you are sending from.
Finally, created a job queue that runs this everyday
codeunit 50111 SendReminderEmails
{
// Customer Rewards Install Logic
// Subtype = Normal;
var
remindermanke: Codeunit "Reminder-Make";
customer: record Customer;
customertotal: BigInteger;
invoicestotal: BigInteger;
invoice: record "Sales Invoice Header";
invoice2: record "Sales Invoice Header";
currentinvoice: record "Sales Invoice Header";
duedate: Date;
datetoday: Date;
days: BigInteger;
email: Codeunit "SMTP Mail";
optionName: Option;
typeinvoice: Option;
reminderdate: Date;
reminder2date: Date;
emailBody: text;
contacts: Record Contact;
salesLines: Record "Sales Invoice Line";
InvAmt: Decimal;
UsedCurrency: Text;
company: record "General Ledger Setup";
InvoiceCurrency: text;
FinalCurrency: Text;
// trigger OnRun()
dateCurrent: Date;
contactbusinessrelation: Record "Contact Business Relation";
contactscount: Integer;
salesHeader: record "Sales Header";
trigger OnRun()
begin
datetoday := Today();
dateCurrent := datetoday;
customer.SetFilter("Reminder Terms Code", 'REMIND');
if customer.FindSet()
then begin
customertotal := customer.Count();
repeat
invoice.SetFilter("Bill-to Customer No.", customer."No.");
if invoice.FindSet() then begin
repeat
duedate := invoice."Due Date";
if (duedate <> 0D)
then begin
invoice.CalcFields("Amount Including VAT", "Remaining Amount");
if (invoice."Remaining Amount" > 0) then begin
days := duedate - dateCurrent;
if (invoice."Currency Code" = '')
then begin
company.FindSet();
FinalCurrency := company."LCY Code";
end
else begin
FinalCurrency := invoice."Currency Code";
END;
if (days = 10)
then begin
if (contactbusinessrelation.FindSet()) then begin
contactbusinessrelation.SetFilter("No.", customer."No.");
contacts.SetFilter("Company No.", contactbusinessrelation."Contact No.");
contacts.SetFilter("E-Mail", '<>%1', '');
if contacts.FindSet then begin
emailBody := '<h3>TO MESSRS: ' + invoice."Sell-to Customer Name" + '</h3>' +
'ATT : ACCOUNTING DEPARTMENT ' +
'</br> </h3><h2>Friendly Reminder</h2>' +
' <hr>' +
'</br>Please make sure the following invoice is paid in due time.' +
'</br></br><strong>' + FinalCurrency + ' ' + FORMAT(invoice."Amount Including VAT", 0, 3) +
' for our invoice no. ' + invoice."No." + ' due on ' + Format(invoice."Due Date", 0, '<Month Text> <Closing><Day>, <Year4>') +
'</strong></br></br>Please disregard this email if this invoice has already been paid. </br>' +
'</br><hr></br>Many thanks in advance for your cooperation,' +
'</br>Very best regards,' +
'</br></br>Financial Department' +
'</br>Your company name' +
'</br>' + Format(Today, 0, '<Month Text> <Closing><Day>, <Year4>');
email.CreateMessage('Sender Name', Sender Email(Should have access to it), '', 'Reminder for approaching due date ', emailBody, true);
repeat
if contacts."E-Mail" <> '' then
email.AddRecipients(contacts."E-Mail");
until contacts.Next = 0;
email.AddBCC(BCC EMAIL ADDRESSS);
email.Send();
END;
end;
END;
End;
END;
until invoice.Next = 0;
end;
until customer.Next = 0;
END;
end;
}