Skip to Content

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.

SELECTION SCREEN.PNG

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.

job.PNG

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.

 

To report this post you need to login first.

13 Comments

You must be Logged on to comment or reply to a post.

  1. Former Member

    Hi,

    While schduling the job with SM36, we are giving the dates between that only system run the job, what will the advatnage over that. 

       Is there any option to run a job every 1 hour daily but only between 9:00 AM to 6:00PM with in office timing?

    Regards,

    Zafar

    (0) 
    1. Jayashree Desai Post author

      Hi Zafar,

      I have tried that , but unfortunately that will not work.I guess end date what we provide in sm36 is only for one specific instance of periodic job.

      Yes , you can achieve this option also by this report.

      Provide the below inputs .This will run for one month , if you want to execute it continuously , then it requires some minor changes in the report.

      doubts.PNG

        Regards,

        Jayashree

      (0) 
  2. Jelena Perfiljeva

    Hm, an interesting idea. 🙂 There are some limitations (e.g. only one step can be added to the job), but it could probably be expanded. Perhaps this could even be a start of some community project to put Redwood out of business. 🙂

    (0) 
    1. Jayashree Desai Post author

      Hi Stephane,

      Standard Report would do the same job as SM36 , meaning it will just create periodic job and it will be run continuously Like if you schedule job every 3 minutes , it will be run continuously unless you delete it manually .

      But the Report which is suggested in the blog will create a periodic job within particular frame.  Here you can schedule job every 3 minutes from Monday 9.00am to Tuesday 6.00pm.

      (0) 
      1. Former Member

        Hi Jayashree,

        As far as I can tell, report BTCAUX10 does exactly what you want (similar to your program) and not what you’ve described in your comment.

        However, it solves it little differently than you solution:

        The report schedules every time a non-periodic job. This job consists of two steps: the first one is responsible for scheduling of the next job (as long as it may start in the requested timeframe), while the second step is the “real” report to be executed.

        (0) 
  3. SolMan SolMan Project

    We’ve a requirement that there are two jobs A and B which runs daily and every 15 mins respectively. So now, we need to hold both the jobs for about 12 hours end of this month, that is, from 10PM 31st March 2017 to 10AM 1st April 2017. So, how do we achieve this requirement? Could anyone please help me out or give some idea to try it out?

    Thanks in advance.

    (0) 

Leave a Reply