Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Answered

Send Alert to mail for New Vendor Creation

Posted on by 200

Hi,

I have a query on How to trigger an alert to mail when a new vendor card is created?

My mail body should have : 

Message: New Vendor card is created.

Table body:  should have these fields , 1) account code 2)Name 3) date of creation 4)Who created the record

Please need help in this how can be done in Al code 

Thankyou.

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: Send Alert to mail for New Vendor Creation

    Good news.

  • Dividutt Profile Picture
    Dividutt 200 on at
    RE: Send Alert to mail for New Vendor Creation

    Thanks Nitin Verma Working now for me.

  • Verified answer
    YUN ZHU Profile Picture
    YUN ZHU 73,696 Super User 2024 Season 2 on at
    RE: Send Alert to mail for New Vendor Creation

    Hi, just adding some info.

    Perhaps you can consider using the Field Monitoring feature, which does not require customization.

    https://yzhums.com/3481/#toc2

    And for automatic emailing, I hope the following examples can give you some new hints.

    https://yzhums.com/7485/

    Hope this helps.

    Thanks.

    ZHU

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: Send Alert to mail for New Vendor Creation

    Hi,

    Can you try this?

    codeunit 50132 "Alert for New Vendor Creation"

    {

       [EventSubscriber(ObjectType::Table, Database::Vendor, 'OnAfterModifyEvent', '', true, true)]

       procedure AlertVendor(var Rec: Record Vendor)

       begin

           if not rec."Mail sent" then begin

               InsertIntoTable(rec_Vendor);

               SendEmail();

               rec."Mail sent" := true;

               rec.Modify();

           end;

       end;

    }

    tableextension 50133 "ModifyVendor" extends Vendor

    {

       fields

       {

           field(50133; "Mail sent"; Boolean)

           {

               DataClassification = ToBeClassified;

           }

       }

    }

  • Dividutt Profile Picture
    Dividutt 200 on at
    RE: Send Alert to mail for New Vendor Creation

    This is my final code, is this correct?

    When I update again name field the name is getting triggered so I don't want this, mail should be triggered only once, 

    So is there any other event where I can maintain that?

    codeunit 50132 "Alert for New Vendor Creation"

    {

       var

           cdu_CommonHelper: Codeunit "Common Helper";

       [EventSubscriber(ObjectType::Table, Database::Vendor, 'OnAfterValidateEvent', 'Name', true, true)]

       procedure AlertVendor(var Rec: Record Vendor)

       var

           rec_Vendor: Record Vendor;

       begin

           rec_Vendor.SetRange("No.", Rec."No.");

           if rec_Vendor.FindSet() then

               repeat

                   if rec.Name <> '' then begin

                       //Message('%1', rec_Vendor.Name);

                       InsertIntoTable(rec_Vendor);

                       SendEmail();

                   end;

               until rec_Vendor.Next() = 0;

       end;

  • Dividutt Profile Picture
    Dividutt 200 on at
    RE: Send Alert to mail for New Vendor Creation

    new-vendor-rated.png

    Hi, this is my output as you mentioned i am getting the name blank after using oninsert event, so use invalidate trigger or use both, 

    Can you explain in my code ,not sure how to do that?

  • Verified answer
    Mohana Yadav Profile Picture
    Mohana Yadav 59,139 Super User 2024 Season 2 on at
    RE: Send Alert to mail for New Vendor Creation
    [quote user="Mohana Yadav"]

    where did you write code?

    You have to use OnInsert triggers but you wont have other data like Name etc. by that time

    [/quote]

    That is what I mentioned here.

    You can try onAfterValidate trigger of Name field but make sure that email will be sent every time Name is modified/changed.

  • Dividutt Profile Picture
    Dividutt 200 on at
    RE: Send Alert to mail for New Vendor Creation

    vendoralert.png

    on-modify-event.png

    Hi Nithin,

    codeunit 50132 "Alert for New Vendor Creation"
    {
        var
            cdu_CommonHelper: Codeunit "Common Helper";

        [EventSubscriber(ObjectType::Table, Database::Vendor, 'OnAfterInsertEvent', '', true, true)]

        procedure AlertVendor(var Rec: Record Vendor)
        var
            rec_Vendor: Record Vendor;
        begin
            rec_Vendor.SetRange("No.", Rec."No.");
            if rec_Vendor.FindSet() then
                repeat
                    Message('%1', rec_Vendor.Name);
                // InsertIntoTable(rec_Vendor);
                //SendEmail();
                until rec_Vendor.Next() = 0;

        end;

    Here I have a issue where I cannot display name, my name column is blank when I call OnAfterInsertEvent so do weed need to call onmodify event or any other event,

    Or can I show name in my email table body how but please help.

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: Send Alert to mail for New Vendor Creation

    Hi,

    You need to write your Event in custom codeunit like below and put your code inside Begin..End.

    [EventSubscriber(ObjectType::table, database::Vendor, 'OnAfterInsertEvent', '', false, false)]

       local procedure OnAfterInsertEventVendor(var Rec: Record Vendor)

       begin

       end;

  • RE: Send Alert to mail for New Vendor Creation

    Hi Divi,

    Seems like you are subscribing the wrong event.  try to subscribe insert record events.

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

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 Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans