How to restart a job in “Error” or “Frozen Completed” status


Sometimes can happen that some recursively scheduled jobs can go wrong or get stuck, you can re-enable them automatically and after consult the log to see errors; it is not a perfect solution but sometimes the error can be a simple error ex: “another user has modified..” etc. This function could be useful for NAV versions up to 2016, NAV 2017 has an improved management and a robust task scheduling system.

Samples of Jobs in “Error” or “Frozen Completed State”

Before Execution

After Execution

The Simple Solution

The solution is based on a codeunit that takes care of reactivating the jobs in the state “error” or “completed”, it can be changed according to the needs.

It can be scheduled:

  • Directly into the NAS (in the same or other NAS instances)
  • On Windows Task Scheduler with codeunit invoked by Powershell

 

Fields Added on Table “Job Queue Entry”

 

Codeunit 50000 “ChangeJobsStatus”

CODE

OnRun()

ChangeJobStatus;

ChangeJobStatus()

Documentation()

“Restart Job” IF in errorin processfinished state

OnRun()

IF ChangeJobStatus() = TRUE THEN

MESSAGE(‘Jobs Status Changed!’)

ELSE

MESSAGE(‘Nothing Changed!’)

LOCAL ChangeJobStatus() JobStatusChanged : Boolean

//”Restart Job” IF in errorin processfinished state

JobStatusChanged := FALSE;

recJobQEntry.RESET;

IF recJobQEntry.FINDSET THEN

REPEAT

CASE recJobQEntry.Status OF

//*** STATE: IN PROCESS

recJobQEntry.Status::”In Process”:

BEGIN

recJobQLogEntry.RESET;

recJobQLogEntry.SETRANGE(“Object Type to Run”,recJobQEntry.”Object Type to Run”); //Check Run.Time

recJobQLogEntry.SETRANGE(“Object ID to Run”,recJobQEntry.”Object ID to Run”);

//Check Duration Process Max

IF recJobQEntry.”Duration Process Max” > 0 THEN

IF recJobQLogEntry.FINDLAST THEN

IF CURRENTDATETIME – recJobQLogEntry.”Start Date/Time” > recJobQEntry.”Duration Process Max” THEN BEGIN //CHECK MAX DURATION TIME

recJobQEntry.Status := recJobQEntry.Status::Ready; // –> SET STATUS TO READY

recJobQEntry.MODIFY(FALSE);

JobStatusChanged := TRUE;

END

END;

//*** STATE: ERROR OR FINISHED

recJobQEntry.Status::Error, recJobQEntry.Status::Finished: //IF “ERROR” OR “FINISHED”

BEGIN

recJobQEntry.Status := recJobQEntry.Status::Ready; // –> SET STATUS TO READY

recJobQEntry.MODIFY(FALSE);

JobStatusChanged := TRUE;

END;

END; //END CASE

COMMIT;

UNTIL recJobQEntry.NEXT = 0;

EXIT(JobStatusChanged);

Download from MSDN https://gallery.technet.microsoft.com/How-to-restart-a-job-in-8aca5516

 

About Scheduling

1) With NAV Codeunit scheduled by NAS

After Execution

2) With NAV Codeunit invoked by Powershell and scheduled by Windows Task Scheduler

Import-Module ‘C:Program FilesMicrosoft Dynamics NAV100ServiceMicrosoft.Dynamics.Nav.Management.dll’

Invoke-NAVCodeunit dynamicsnav100 -CodeunitId 50000 -CompanyName ‘CRONUS Italia S.p.A.’

To Schedule Powershell Script by Windows Task Scheduler

An easy way to schedule a PowerShell script is, to use Windows Task Scheduler, look at this post:

Coffee Break: Proactive Monitoring with Task Scheduler

https://blogs.msdn.microsoft.com/nav/2016/01/19/coffee-break-proactive-monitoring-with-task-scheduler/

My Old Post about NAV 2017 Task Scheduler

https://robertostefanettinavblog.com/2016/11/04/nav-2017-task-scheduler/

Advertisements

“ALL About” C/AL Coding Guidelines


CAL

hi guys, this is another short post of my “ALL About” posts series (the first of this year); in this case i want to write about “C/AL Coding Rules and Guidelines”.

below a short list of documents\pages about this topic,

First “ALL IN ONE” Document but “ALWAYSuseful is:  “C/AL Coding Guidelines used at Microsoft Development Center Copenhagen”  …”The Bible” for each C/AL Developer!

Source: C/AL Coding Guidelines used at Microsoft Development Center

you can download PDF document here: cal-coding-guidelines-at-microsoft-development-center-copenhagen

“ALL IN ONE” Document Vs Wiki & Community Feedback Philosophy

CAL-Coding-Guidelines-at-Microsoft-Development-Center-Copenhagen.pdf” is “all in one document”. In contrast, on this wiki, the coding guidelines are published individually. The reason is: give you the chance to comment and share your oppinion on each one.

Question: Since we’re having the guidelines, how come there is still C/AL code in NAV which doesn’t respect them?

Answer: all new C/AL code is bound to follow the guidelines (else it cannot be checked-in). However, the code that existed before the rules – it does not. We had done cleanup in a certain degree. Now we’re gradually improving the old code base as we visit various objects in order to add new functionality, however chances are that code we didn’t touch in a long time had remained in its old form.

“Microsoft Dynamics Community is looking forward to your comments!  Where you can, do provide concrete examples of the alternatives, Good and Bad”

Wiki Page on Design Patterns

https://community.dynamics.com/nav/w/designpatterns/156.cal-coding-guidelines

other nice resources:

nice doc on Mibuso about “How to work with record-variables”:

http://mibuso.com/howtos/how-to-work-with-record-variables-version-2

Nice general sections on MSDN

General C/AL Programming Format

https://msdn.microsoft.com/en-us/library/ee414237(v=nav.10).aspx

Programming Best Practices

https://msdn.microsoft.com/en-us/library/ee414204(v=nav.90).aspx

C/AL Comments

https://msdn.microsoft.com/en-us/library/dd301180(v=nav.90).aspx

C/AL Coding Guidelines Video