Hi,
I have created a certification page in our Business Central Version 21, wherein it serves as monitoring of those expiring company certificates that we have. The page contains the column for Validity of the certificate in months and the email addresses of the concerned personnel to renew the certificate. I want to send an automatic email notification to the email addresses in my certification page if the validity in months equals to 1. I have tried coding it, but it seems not to work. Below are what I have a;ready started. I have global variables for BodyMessage, AddBodyMessage & Recipients. How can I simplify this? Hoping for your kind help. Thank you in advance.
field("Expiration Date"; "Expiration Date")
{
ApplicationArea = All;
trigger OnValidate()
var
myInt: Integer;
Email: Codeunit Email;
EmailMessage: Codeunit "Email Message";
Vendor: Record Vendor;
UserSetup: Record "User Setup";
recipient: Text;
begin
if "Expiration Date" = 0D then
"Validity in Months" := 0 else
"Validity in Months" := DATE2DMY("Expiration Date", 2) - DATE2DMY(TODAY, 2) + 12 * (DATE2DMY("Expiration Date", 3) - DATE2DMY(TODAY, 3));
if "Validity in Months" = 1 then begin
Clear(BodyMessage);
Clear(AddBodyMessage);
Clear(Recipients);
Recipients.Add(rec."Notification Email");
BodyMessage := 'Dear Trade Partner, <br> <br> Your <strong>E%1</strong> is about to expire in one month, kindly arrange the renewal of your certificate. Thank you.';
BodyMessage += '<br><br><hr> This is a system generated email. Please do not reply to this mail.</hr>';
AddBodyMessage := StrSubstNo(BodyMessage, rec."Certification Type");
EmailMessage.Create(Recipients, 'Vendor Certificate Expiry', AddBodyMessage, true);
EmailSend.Send(EmailMessage, Enum::"Email Scenario"::Default);
end;
end;
}
field("Validity in Months"; "Validity in Months")
{
ApplicationArea = All;
}
field("Notification Email"; "Notification Email")
Hi, I also think that it would be better to put it in an Action (Button), and the Action can be associated with the Codeunit, Report or the function.
As for your current code is executed in trigger OnValidate(), if you don't manually update "Expiration Date" or execute Rec.OnValidate(), it will not execute.
Hope this can give you some hints.
Thanks.
ZHU
I do not think you should put this code in a page trigger.
You should either have it in a report and schedule the report to be run in a Job Queue once or day or so or in a codeunit that does the same.
Probably easier to use a processing only report on the data item - then you get the looping for "free".
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,134 Super User 2024 Season 2
Martin Dráb 229,928 Most Valuable Professional
nmaenpaa 101,156