Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

email (auto) when job fails

Posted on by 5,124

Hello.  Is there any feature in Nav that will auto email a user (me for instance) when a job fails?  E.g., I have joq queue entries.  When any of these jobs fail, I am not notified.  I know I can email myself by writing that in the codeunit that I am calling in the job.  THe problem is if it fails at a certain point, it will never get to my email code.  Do we have such thing as ON ERROR in C/AL?  How do you guys make sure you are notified when jobs fail?

Please advise.

*This post is locked for comments

  • mbr Profile Picture
    mbr 5,124 on at
    RE: email (auto) when job fails

    Thank you Suresh! It worked! Woohoo!

  • Suggested answer
    TharangaC Profile Picture
    TharangaC 23,116 on at
    RE: email (auto) when job fails

    As Suggested by Suresh, you can have a separate method to set the type and depending on the type set, you can have a if condition in the Run method.  

    Make sure the SetType function is global and all the other functions are local. By doing that you or any other developer will not make a mistakes of calling a function directly other than the SetType function and Run method.

  • Verified answer
    Suresh Kulla Profile Picture
    Suresh Kulla 43,745 on at
    RE: email (auto) when job fails

    You can call different methods in the On Run Trigger and execute different methods.  But you cannot use CU.Method, you have to use CU.RUN, what you can do is create another function in the codeunit to set parameter.

    For example SetType function which will assign a integer to a global variable (FunctionType Integer);

    You can call CU.SetType to define which method you want to run and in the OnRun Trigger you can use CASE statement on FunctionType and call different methods.

          CU.SetType(1);

          IF NOT CU.RUN THEN

  • mbr Profile Picture
    mbr 5,124 on at
    RE: email (auto) when job fails

    Thank you all for the very insightful feedback.  I would like to try this technique:

    if not codeunit.run then

       sendmaail(GETLASTERRORTEXT)

    My only problem is I am pretty much using one codeunit to run different methods.  I don't want to have to create separate codeunits to run different jobs.

    E.g., I am running Codeunit.METHOD whereby codeunit is objid 50001 for example.  Can I still use above technique?  Is there a way I can run a method in a codeunit?

    so I can instantiate codeunit in the local var (e.g., declare a variable CU as codeunit 50001)

    then in the ONRUN event of the main codeunit, I will do below

    if not CU.<METHOD> then

      sendmail(GETLASTERRORTEXT)

    Please advise.

  • Suggested answer
    TharangaC Profile Picture
    TharangaC 23,116 on at
    RE: email (auto) when job fails

    I think you have miss read first two posts. Both the posts we have suggested to have two separate code units to handle the job. However coming back to your suggestion,

    Think your process job run every hour of the day, So at the second hour job get failed and it create a log. After that job didn't get failed and it runs smoothly, but as for your code you will get a email every single time your email management job code get executed.

    Addition to above your code will fit if the user want to get the notification on timely basis. If the user require to get the error notification as soon as error occurred, your suggestion will not work.

    Correct me if I was wrong.

    However it is a very good suggestion. :)

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: email (auto) when job fails

    I cannot agree with Tharanga. As mentioned in the codeunit, the start date and the end date have been initialized with the whole day. (24 hours) Therefore, you do not have to change the codeunit when you change the job queue entry time. I think you have misunderstood the concept.

    We have successfully tested this mechanism in many implementations. When you try to do the business logic and the email sending in a single codeunit, the chances for failure is high. Therefore my recommendation is to segregate the activities and schedule them as different job queue entries.

  • Suggested answer
    TharangaC Profile Picture
    TharangaC 23,116 on at
    RE: email (auto) when job fails

    Good suggestion Thilina.

    However your code will fit if the user want to get the notification on timely basis. If the user require to get the error notification as soon as error occurred, your suggestion will not work.

    Plus in order to filter the log entry table, you need to specify the start time of the job, so every time when a user change the job schedule, you need to modify your code with the start time.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: email (auto) when job fails

    You can create a separate codeunit to send the result checking the log entry. This codeunit can be scheduled using the Job queue setting a start time. It should be executing after the desired codeunit is finished executing. For an instance, lets say the your desired codeunit is running at 22.00 and it is taking 30 mins for execution. The second codeunit which is sending the status of the execution can be scheduled around 22.30.

    We have overcome this issue adopting this mechanism. The codeunit which can be used is given below. Try this.

    CLEAR(strEmailBodyFormatted);

    i := 1;

    StartDateTime := CREATEDATETIME(TODAY,000000.01T);

    EndDateTime := CREATEDATETIME(TODAY,235959T);

    gtJobQueueLogEntry.RESET;

    gtJobQueueLogEntry.SETFILTER(gtJobQueueLogEntry."Object Type to Run", FORMAT(gtJobQueueLogEntry."Object Type to Run"::Report));

    gtJobQueueLogEntry.SETFILTER(gtJobQueueLogEntry."Object ID to Run",'*****');

    gtJobQueueLogEntry.SETRANGE(gtJobQueueLogEntry."Start Date/Time", StartDateTime, EndDateTime);

    IF gtJobQueueLogEntry.FINDLAST THEN

    BEGIN

       gtJobQueueLogEntry.CALCFIELDS(gtJobQueueLogEntry."Object Name to Run");    /

       strEmailBodyFormattedstr := gtJobQueueLogEntry."Object Name to Run"

       +' : Status - '+FORMAT(gtJobQueueLogEntry.Status)

       +' ( '+FORMAT(gtJobQueueLogEntry."Start Date/Time")+' - '+FORMAT(gtJobQueueLogEntry."End Date/Time")+' ) '

       + gtJobQueueLogEntry."Error Message";

       i := i + 1;

    END;

    IF gtJobQueueLogEntry.COUNT = 0 THEN

     strEmailBodyFormattedstr := 'Batch process initialization failed on '+ FORMAT(TODAY) +' !';

    SMTPCU.CreateMessage('Sender and recepient details, TRUE);

    SMTPCU.Send();

  • Suggested answer
    TharangaC Profile Picture
    TharangaC 23,116 on at
    RE: email (auto) when job fails

    Agree with Suresh.

    You can use the CODEUNIT.RUN return value (If error occurred it will return false).

    Once you get the return value you can use GETLASTERRORTEXT to get the error message. GETLASTERRORTEXT will returns the text that was contained in the last error message that was displayed, by using this you will be able to inform the relevant users on the exact information about the error.  

    If you need additional help, please do get back to us.

  • Suggested answer
    Rajasekhar@MS Profile Picture
    Rajasekhar@MS 5,567 on at
    RE: email (auto) when job fails

    As mentioned by Suresh, You need to create a CU inside cu you can write code like below

    IF NOT CU.RUN THEN

        Sendmail;

    schedule the New CU in job queue,whcih will send email notifications if you get any errors.

    Best Regards

    Rajasekhar.Y

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,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans