Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 


Scheduling Periodic Job has one limitation , We can not schedule the job for a particular time frame .

Lets Say if you want to schedule the job every one minute for one week only .In order to achieve this , we have to schedule the periodic job which will run every one minute and then after one week we need to manually delete the job.

So In order to overcome this limitation i have written simple report which will schedule the periodic job for a given time frame.

This report will schedule two jobs , one to run the report periodically and other job to delete the first job after the given time frame.

 

Start Date and Start Time( Low)  implies the start time for the first job ,similarly Start Date and Start Time( High) implies the start time for the second job which will delete the first job.You can also specify the Day on which job should be scheduled.




You can also specify the Job Prefix name , first job will be created with the name of the Prefix followed by '_JOB' followed by the timestamp and second job will have '_DEL'  and the timestamp.

Period in Mnts is for the first job to specify how often it should be run.



Attached Document
*&---------------------------------------------------------------------*
*& Report /tpisp/mvu_schedlue_scan_jobs
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT /tpisp/mvu_schedlue_scan_jobs.
TABLES :progdir. "#EC NEEDED

TYPE-POOLS : abap.
TYPES:BEGIN OF ts_records,
start_date TYPE sy-datum,
start_time TYPE sy-uzeit,
end_time TYPE sy-uzeit,
END OF ts_records.


SELECTION-SCREEN BEGIN OF BLOCK bl1 WITH FRAME TITLE text-001.
SELECT-OPTIONS : s_date FOR sy-datum OBLIGATORY.
SELECT-OPTIONS:s_time FOR sy-uzeit OBLIGATORY.
PARAMETERS :p_mon TYPE c AS CHECKBOX .
PARAMETERS :p_tue TYPE c AS CHECKBOX .
PARAMETERS :p_wed TYPE c AS CHECKBOX .
PARAMETERS :p_thu TYPE c AS CHECKBOX .
PARAMETERS :p_fri TYPE c AS CHECKBOX .
PARAMETERS :p_sat TYPE c AS CHECKBOX .
PARAMETERS :p_sun TYPE c AS CHECKBOX .

SELECTION-SCREEN END OF BLOCK bl1.

SELECTION-SCREEN BEGIN OF BLOCK bl2 WITH FRAME TITLE text-002.
PARAMETERS:p_prg TYPE programm OBLIGATORY DEFAULT '/TPISP/MVU_VIRUS_SCAN_ALL_DOC'.
PARAMETERS:p_varint TYPE variant.
PARAMETERS:p_job TYPE char20 OBLIGATORY DEFAULT 'MVU_SCAN_ALL_DOC'.
PARAMETERS:p_period TYPE num2 OBLIGATORY DEFAULT '3'.
SELECTION-SCREEN END OF BLOCK bl2.

DATA:lt_day_attributes TYPE TABLE OF casdayattr,
ls_day_attributes TYPE casdayattr,
lt_records TYPE TABLE OF ts_records,
lv_message TYPE string,
lv_lines TYPE char4,
lv_start_timestamp TYPE timestamp,
lv_current_timestamp TYPE timestamp,
lv_end_timestamp TYPE timestamp.

FIELD-SYMBOLS: <ls_records> LIKE LINE OF lt_records.

AT SELECTION-SCREEN.

*Date and time Validations
IF s_date-high IS INITIAL.
s_date-high = sy-datum.
APPEND s_date. "#EC *
ENDIF.

IF s_date-low IS INITIAL.
s_date-low = sy-datum.
APPEND s_date. "#EC *
ENDIF.

CONVERT DATE s_date-low TIME s_time-low INTO TIME STAMP lv_start_timestamp TIME ZONE 'UTC '.
GET TIME STAMP FIELD lv_current_timestamp .

IF lv_current_timestamp GT lv_start_timestamp.
MESSAGE e000(/tpwbw/messages) WITH 'Start Date/Time lies in the past'."#EC NOTEXT
ENDIF.

CONVERT DATE s_date-high TIME s_time-high INTO TIME STAMP lv_end_timestamp TIME ZONE 'UTC '.
IF lv_current_timestamp GT lv_end_timestamp.
MESSAGE e000(/tpwbw/messages) WITH 'End Date/Time lies in the past'."#EC NOTEXT
ENDIF.

IF s_time-low IS INITIAL.
MESSAGE e000(/tpwbw/messages) WITH 'Please Enter the Start Time'."#EC NOTEXT
ENDIF.
IF s_time-high IS INITIAL.
MESSAGE e000(/tpwbw/messages) WITH 'Please Enter the End Time'."#EC NOTEXT
ENDIF.

IF p_mon = abap_false AND p_tue = abap_false AND p_wed = abap_false AND p_thu = abap_false AND p_fri = abap_false AND p_sat = abap_false AND p_sun = abap_false.
MESSAGE e000(/tpwbw/messages) WITH 'Please check atleast one day for job execution '."#EC NOTEXT
ENDIF.

* *Check whether the report exists or no
SELECT SINGLE * FROM progdir WHERE name = p_prg
AND state = 'A'. "#EC *

IF sy-subrc NE 0.
CONCATENATE 'Program' p_prg 'does not exist' INTO lv_message SEPARATED BY space."#EC NOTEXT
MESSAGE e000(/tpwbw/messages) WITH lv_message.
ENDIF.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_varint.

PERFORM f4help.

START-OF-SELECTION.

* Get the Dates for which job needs to executed
PERFORM get_records TABLES lt_day_attributes USING s_date-low s_date-high .

LOOP AT lt_day_attributes INTO ls_day_attributes.
APPEND INITIAL LINE TO lt_records ASSIGNING <ls_records>.
<ls_records>-start_date = ls_day_attributes-date.
<ls_records>-start_time = s_time-low.
<ls_records>-end_time = s_time-high.
ENDLOOP.

IF LINES( lt_records ) EQ 0.
RETURN.
ENDIF.

LOOP AT lt_records ASSIGNING <ls_records>.

*Schedule the job
PERFORM schedule_periodic_jobs USING <ls_records>-start_date <ls_records>-start_time .

*Schedule the delete job to delete the periodic job at the end time
PERFORM delete_periodic_jobs USING <ls_records>-start_date <ls_records>-end_time.

ENDLOOP.

CLEAR :lv_message,lv_lines.
DESCRIBE TABLE lt_records LINES lv_lines.
CONCATENATE lv_lines 'Jobs have been scheduled.' INTO lv_message SEPARATED BY space.
MESSAGE s000(/tpwbw/messages) WITH lv_message.

*&---------------------------------------------------------------------*
*& Form get_records
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_LT_DAY_ATTRIBUTES text
*----------------------------------------------------------------------*
FORM get_records TABLES p_lt_day_attributes STRUCTURE casdayattr USING p_start_date TYPE sy-datum p_end_date TYPE sy-datum ."#EC *

DATA:ls_day_attributes TYPE casdayattr.

CALL FUNCTION 'DAY_ATTRIBUTES_GET'
EXPORTING
date_from = p_start_date
date_to = p_end_date
language = sy-langu
TABLES
day_attributes = p_lt_day_attributes.

IF sy-subrc <> 0. "#EC *
RETURN.
ENDIF.

LOOP AT p_lt_day_attributes INTO ls_day_attributes.
CASE ls_day_attributes-weekday_s.
WHEN 'MO'.
IF p_mon = abap_false.
DELETE p_lt_day_attributes.
CONTINUE.
ENDIF.
WHEN 'TU'.
IF p_tue = abap_false.
DELETE p_lt_day_attributes.
CONTINUE.
ENDIF.
WHEN 'WE'.
IF p_wed = abap_false.
DELETE p_lt_day_attributes.
CONTINUE.
ENDIF.
WHEN 'TH'.
IF p_thu = abap_false.
DELETE p_lt_day_attributes.
CONTINUE.
ENDIF.
WHEN 'FR'.
IF p_fri = abap_false.
DELETE p_lt_day_attributes.
CONTINUE.
ENDIF.
WHEN 'SA'.
IF p_sat = abap_false.
DELETE p_lt_day_attributes.
CONTINUE.
ENDIF.
WHEN 'SU'.
IF p_sun = abap_false.
DELETE p_lt_day_attributes.
CONTINUE.
ENDIF.
ENDCASE.
ENDLOOP.
ENDFORM. "get_records
*&---------------------------------------------------------------------*
*& Form schedule_PERIODIC_jobS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM schedule_periodic_jobs USING p_start_date TYPE sy-datum p_start_time TYPE sy-uzeit.

DATA:lv_jobname TYPE char32,
lv_jobcount TYPE char8,
lv_step_number TYPE int4. "#EC NEEDED

CONCATENATE p_job 'JOB' p_start_date INTO lv_jobname SEPARATED BY '_'.

CALL FUNCTION 'JOB_OPEN'
EXPORTING
jobname = lv_jobname
IMPORTING
jobcount = lv_jobcount.
IF sy-subrc <> 0. "#EC *
RETURN.
ENDIF.

CALL FUNCTION 'JOB_SUBMIT'
EXPORTING
authcknam = sy-uname
jobcount = lv_jobcount
jobname = lv_jobname
language = sy-langu
report = p_prg
variant = p_varint
IMPORTING
step_number = lv_step_number.
IF sy-subrc <> 0. "#EC *
RETURN.
ENDIF.

CALL FUNCTION 'JOB_CLOSE'
EXPORTING
jobcount = lv_jobcount
jobname = lv_jobname
prdmins = p_period
sdlstrtdt = p_start_date
sdlstrttm = p_start_time.

IF sy-subrc <> 0. "#EC *
RETURN.
ENDIF.
ENDFORM. "schedule_PERIODIC_jobS

*&---------------------------------------------------------------------*
*& Form delete_PERIODIC_jobS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM delete_periodic_jobs USING p_start_date TYPE sy-datum p_start_time TYPE sy-uzeit.

DATA:lv_jobname TYPE char32,
lv_jobcount TYPE char8,
lv_repname TYPE char32.


CONCATENATE p_job 'DEL' p_start_date INTO lv_jobname SEPARATED BY '_'.

CONCATENATE p_job p_start_date INTO lv_repname SEPARATED BY '_'.

CALL FUNCTION 'JOB_OPEN'
EXPORTING
jobname = lv_jobname
IMPORTING
jobcount = lv_jobcount.
IF sy-subrc <> 0. "#EC *
RETURN.
ENDIF.

SUBMIT rsbtcdel WITH jobname EQ lv_repname
WITH username EQ sy-uname
WITH sched EQ abap_true
WITH fin EQ abap_false
WITH abort EQ abap_false
VIA JOB lv_jobname
NUMBER lv_jobcount
AND RETURN.


CALL FUNCTION 'JOB_CLOSE'
EXPORTING
jobcount = lv_jobcount
jobname = lv_jobname
sdlstrtdt = p_start_date
sdlstrttm = p_start_time.

IF sy-subrc <> 0. "#EC *
RETURN.
ENDIF.
ENDFORM. "delete_PERIODIC_jobS
*&---------------------------------------------------------------------*
*& Form f4help
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM f4help.

TYPES:BEGIN OF ts_variants,
variant TYPE variant,
END OF ts_variants.

DATA:lt_scrfields TYPE TABLE OF dynpread WITH HEADER LINE,
lt_variants TYPE TABLE OF ts_variants,
lv_report TYPE program.
CLEAR lv_report.
REFRESH : lt_scrfields ,lt_variants.

MOVE 'P_PRG' TO lt_scrfields-fieldname.
APPEND lt_scrfields.

CALL FUNCTION 'DYNP_VALUES_READ'
EXPORTING
dyname = sy-repid
dynumb = sy-dynnr
TABLES
dynpfields = lt_scrfields
EXCEPTIONS
invalid_abapworkarea = 1
invalid_dynprofield = 2
invalid_dynproname = 3
invalid_dynpronummer = 4
invalid_request = 5
no_fielddescription = 6
invalid_parameter = 7
undefind_error = 8
double_conversion = 9
stepl_not_found = 10
OTHERS = 11.
IF sy-subrc <> 0.
RETURN.
ENDIF.


CALL FUNCTION 'DYNP_VALUES_READ' "#EC *
EXPORTING
dyname = sy-repid
dynumb = sy-dynnr
TABLES
dynpfields = lt_scrfields.
IF sy-subrc <> 0.
RETURN.
ENDIF.

READ TABLE lt_scrfields INDEX 1.
IF sy-subrc EQ 0.
lv_report = lt_scrfields-fieldvalue.
ENDIF.

SELECT variant FROM varid INTO TABLE lt_variants WHERE report = lv_report.

CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST' "#EC *
EXPORTING
retfield = 'VARIANT'
window_title = 'Variants' "#EC NOTEXT
dynpprog = sy-repid
dynpnr = sy-dynnr
dynprofield = 'P_VARINT'
value_org = 'S'
TABLES
value_tab = lt_variants
EXCEPTIONS
parameter_error = 1
no_values_found = 2
OTHERS = 3.
ENDFORM. "f4help

has the source code for the report.


 
15 Comments