web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

email (auto) when job fails

(0) ShareShare
ReportReport
Posted on by 5,136

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

I have the same question (0)
  • Suggested answer
    Suresh Kulla Profile Picture
    50,243 Super User 2025 Season 2 on at

    You can use CODEUNIT.RUN which will return true if no errors occurred and false if error is occurred, you can also use GETLASTERRORTEXT to get error message.

    Call your job codeunit/report in another codeunit using above logic and you can trigger your auto email function based on the return value.

    Check this blog

    http://vjeko.com/blog/getting-more-insight-into-exceptions-in-nav

  • Suggested answer
    Rajasekhar@MS Profile Picture
    5,569 on at

    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

  • Suggested answer
    Tharanga Chandrasekara Profile Picture
    23,118 on at

    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
    Community Member Profile Picture
    on at

    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
    Tharanga Chandrasekara Profile Picture
    23,118 on at

    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
    on at

    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
    Tharanga Chandrasekara Profile Picture
    23,118 on at

    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. :)

  • mbr Profile Picture
    5,136 on at

    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.

  • Verified answer
    Suresh Kulla Profile Picture
    50,243 Super User 2025 Season 2 on at

    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

  • Suggested answer
    Tharanga Chandrasekara Profile Picture
    23,118 on at

    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.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics NAV (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans