Skip to main content

Notifications

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

Email Notification by a Page Trigger

Posted on by 25

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")

  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 73,035 Super User 2024 Season 2 on at
    RE: Email Notification by a Page Trigger

    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

  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,748 Super User 2024 Season 1 on at
    RE: Email Notification by a Page Trigger

    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".

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 229,928 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans