Here is the Single Instance CU that would start from CU 1.
OnRun()
JobSchedMgtSetup.GET;
JobSchedMgtSetup.TESTFIELD("Shut Down Time");
IF ISCLEAR(Timer) THEN
CREATE(Timer);
Timer.StartTimer(6000);
IsRunning := TRUE;
CodeUnitIsRunning() : Boolean
EXIT(IsRunning);
Code()
IF NOT JobSchedMgtSetup.GET THEN
ERROR(Text000,JobSchedMgtSetup.TABLECAPTION);
IF (NOT JobSchedMgtSetup."Log Scheduler Actions") THEN
EXIT;
CheckAndRunScheduler;
CheckAndRunScheduler()
SELECTLATESTVERSION;
JobSchedSetup2.LOCKTABLE;
JobSchedSetup2.RESET;
JobSchedSetup2.SETCURRENTKEY("Next Check Date","Next Check Time");
JobSchedSetup2.SETFILTER("Next Check Date",'<>%1',0D);
JobSchedSetup2.SETRANGE(Enabled,TRUE);
IF JobSchedSetup2.FIND('-') THEN REPEAT
JobSchedSetup := JobSchedSetup2;
IF RunNow(JobSchedSetup."Next Check Date",JobSchedSetup."Next Check Time",JobSchedSetup) THEN
IF AllObj.GET(JobSchedSetup."Object Type",JobSchedSetup."Object No.") THEN BEGIN
COMMIT;
IF JobSchedSetup."Error Handling" = JobSchedSetup."Error Handling"::"Mark as Error and Stop" THEN BEGIN
CASE JobSchedSetup."Object Type" OF
JobSchedSetup."Object Type"::Report: REPORT.RUNMODAL(JobSchedSetup."Object No.",FALSE,FALSE);
JobSchedSetup."Object Type"::Dataport: DATAPORT.RUNMODAL(JobSchedSetup."Object No.",FALSE);
JobSchedSetup."Object Type"::Codeunit: CODEUNIT.RUN(JobSchedSetup."Object No.");
END;
COMMIT;
JobSchedSetup.LOCKTABLE;
JobSchedSetup.GET(JobSchedSetup.ID);
JobSchedSetup."Message Text" := 'Processed successfully.';
JobSchedSetup."Last DateTime Run" := CURRENTDATETIME;
NewDateAndTime(JobSchedSetup);
IF JobSchedSetup.MODIFY THEN;
COMMIT;
END ELSE BEGIN
IF RunObjectFromJobSched.RUN(JobSchedSetup) THEN BEGIN
COMMIT;
JobSchedSetup.LOCKTABLE;
JobSchedSetup.GET(JobSchedSetup.ID);
JobSchedSetup."Message Text" := 'Processed successfully.';
JobSchedSetup."Last DateTime Run" := CURRENTDATETIME;
NewDateAndTime(JobSchedSetup);
IF JobSchedSetup.MODIFY THEN;
COMMIT;
END ELSE BEGIN
JobSchedSetup.LOCKTABLE;
JobSchedSetup.GET(JobSchedSetup.ID);
JobSchedSetup."Message Text" := Text013;
JobSchedSetup."Last DateTime Run" := CURRENTDATETIME;
IF JobSchedSetup.MODIFY THEN;
COMMIT;
END;
END;
END;
UNTIL JobSchedSetup2.NEXT = 0;
RunNow(RunDate : Date;RunTime : Time;JobSchedSetup : Record "Job Scheduler Setup") : Boolean
IF JobSchedSetup."Run Status" = JobSchedSetup."Run Status"::"Special Order" THEN
EXIT(TRUE);
IF (RunDate = 0D) OR (RunTime = 0T) THEN
EXIT(FALSE);
IF ((TODAY - RunDate) * 24) + ((TIME - RunTime) / 3600000) > 0 THEN BEGIN
CASE DATE2DWY(TODAY,1) OF
1:
IF JobSchedSetup."Valid on Mondays" THEN
EXIT(CheckTimeLimit(JobSchedSetup))
ELSE
EXIT(FALSE);
2:
IF JobSchedSetup."Valid on Tuesdays" THEN
EXIT(CheckTimeLimit(JobSchedSetup))
ELSE
EXIT(FALSE);
3:
IF JobSchedSetup."Valid on Wednesdays" THEN
EXIT(CheckTimeLimit(JobSchedSetup))
ELSE
EXIT(FALSE);
4:
IF JobSchedSetup."Valid on Thursdays" THEN
EXIT(CheckTimeLimit(JobSchedSetup))
ELSE
EXIT(FALSE);
5:
IF JobSchedSetup."Valid on Fridays" THEN
EXIT(CheckTimeLimit(JobSchedSetup))
ELSE
EXIT(FALSE);
6:
IF JobSchedSetup."Valid on Saturdays" THEN
EXIT(CheckTimeLimit(JobSchedSetup))
ELSE
EXIT(FALSE);
7:
IF JobSchedSetup."Valid on Sundays" THEN
EXIT(CheckTimeLimit(JobSchedSetup))
ELSE
EXIT(FALSE);
ELSE
EXIT(FALSE);
END;
END ELSE
EXIT(FALSE);
CheckTimeLimit(JobSchedSetup : Record "Job Scheduler Setup") : Boolean
IF (JobSchedSetup."Starting Time" = 0T) OR (JobSchedSetup."Ending Time" = 0T) THEN
EXIT(TRUE);
WTime := TIME;
IF (WTime >= JobSchedSetup."Starting Time") AND (WTime <= JobSchedSetup."Ending Time") THEN
EXIT(TRUE)
ELSE
EXIT(FALSE);
NewDateAndTime(VAR JobSchedSetup2 : Record "Job Scheduler Setup")
JobSchedSetup2."Last Date Checked" := TODAY;
JobSchedSetup2."Last Time Checked" := TIME;
IF JobSchedSetup2."Run Status" <> JobSchedSetup2."Run Status"::"Special Order" THEN
RunObjectFromJobSched.CalcNewDateTime(
JobSchedSetup2."Next Check Date",JobSchedSetup2."Next Check Time",
JobSchedSetup2."Time Between Check",JobSchedSetup2."Unit of Measure");
Timer::TimerEvent()
Code;
COMMIT;
All the variables have been copied from Form.
Ahmed Rashed Amini
Independent Consultant/Developer
Comments
Independent Consultant/Developer
blog: https://dynamicsuser.net/nav/b/ara3n
AP Commerce, Inc. = where I work
Getting Started with Dynamics NAV 2013 Application Development = my book
Implementing Microsoft Dynamics NAV - 3rd Edition = my 2nd book
Please explain how to start timer..........
All the variables have been copied from Form.
Independent Consultant/Developer
blog: https://dynamicsuser.net/nav/b/ara3n
Independent Consultant/Developer
blog: https://dynamicsuser.net/nav/b/ara3n