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: 
11,443

With the help of report BTC_EVENT_RAISE, we can create chains of jobs that should be started one after another. You can read more about it in SAP note 919458.

Now, how can we trigger a job after two other jobs have finished? Of course, you can use an external scheduler for this purpose. However, with a bit of programming you can create your own mini scheduler that does the trick.

The basic idea is to have a master job that creates the jobs. The second job will have an additional step that checks whether the first job has already finished. If yes, it will start the third job immediately. If no, the third job will be scheduled after the end of the first job.


Let's take a look at the coding of the master report:



INCLUDE
zwo_mini_scheduler_utils.

PARAMETERS report1 TYPE sy-repid OBLIGATORY.
PARAMETERS report2 TYPE sy-repid OBLIGATORY.
PARAMETERS report3 TYPE sy-repid OBLIGATORY.
PARAMETERS jobname1 TYPE btcjob.
PARAMETERS jobname2 TYPE btcjob.
PARAMETERS jobname3 TYPE btcjob.

DATA l_jobname TYPE btcjob.
DATA l_jobcount TYPE btcjobcnt.
DATA jobcount1 TYPE btcjobcnt.
DATA l_report TYPE sy-repid.
DATA lo_util TYPE REF TO lcl_utilities.

START-OF-SELECTION.

  CREATE OBJECT lo_util.

  IF jobname1 IS INITIAL.
    jobname1 = 'JOBNAME1'.
  ENDIF.
  IF jobname2 IS INITIAL.
    jobname2 = 'JOBNAME2'.
  ENDIF.
  IF jobname3 IS INITIAL.
    jobname3 = 'JOBNAME3'.
  ENDIF.

* create job 1 and job 2
  DO 2 TIMES.

    IF sy-index = 1.
      l_jobname = jobname1.
    ELSE.
      l_jobname = jobname2.
    ENDIF.

    CALL FUNCTION 'JOB_OPEN'
      EXPORTING
        jobname          = l_jobname
      IMPORTING
        jobcount         = l_jobcount
      EXCEPTIONS
        cant_create_job  = 1
        invalid_job_data = 2
        jobname_missing  = 3
        OTHERS           = 4.
    IF sy-subrc <> 0.
      MESSAGE e351 WITH 'Error in JOB_OPEN'.
    ENDIF.

    IF sy-index = 1.
      jobcount1 = l_jobcount.
      l_report = report1.
    ELSE.
      l_report = report2.
    ENDIF.

    CALL FUNCTION 'JOB_SUBMIT'
      EXPORTING
        authcknam                         = sy-uname
        jobcount                          = l_jobcount
        jobname                           = l_jobname
        report                            = l_report
*       VARIANT                           = ' '
   EXCEPTIONS
     bad_priparams                     = 1
     bad_xpgflags                      = 2
     invalid_jobdata                   = 3
     jobname_missing                   = 4
     job_notex                         = 5
     job_submit_failed                 = 6
     lock_failed                       = 7
     program_missing                   = 8
     prog_abap_and_extpg_set           = 9
     OTHERS                            = 10
              .
    IF sy-subrc <> 0.
      lo_util->delete_job( im_jn = l_jobname im_jc = l_jobcount ).
      MESSAGE e351 WITH 'Error in JOB_SUBMIT'.
    ENDIF.

    IF sy-index = 2.
* The submitted report will check for the end of job 1 and schedule job 3
      SUBMIT zwo_mini_scheduler_sub VIA JOB l_jobname NUMBER l_jobcount
      WITH im_jn = jobname3
      WITH pred_jn = jobname1
      WITH pred_jc = jobcount1
      WITH im_rep = report3
      AND RETURN.

      IF sy-subrc <> 0.
        lo_util->delete_job( im_jn = l_jobname im_jc = l_jobcount ).
        MESSAGE e351 WITH 'Error in SUBMIT'.
      ENDIF.

    ENDIF.

    CALL FUNCTION 'JOB_CLOSE'
      EXPORTING
        jobcount                          = l_jobcount
        jobname                           = l_jobname
        strtimmed                         = abap_true
     EXCEPTIONS
       cant_start_immediate              = 1
       invalid_startdate                 = 2
       jobname_missing                   = 3
       job_close_failed                  = 4
       job_nosteps                       = 5
       job_notex                         = 6
       lock_failed                       = 7
       invalid_target                    = 8
       OTHERS                            = 9
              .
    IF sy-subrc <> 0.
      lo_util->delete_job( im_jn = l_jobname im_jc = l_jobcount ).
      MESSAGE e351 WITH 'Error in JOB_CLOSE'.
    ENDIF.

  ENDDO.



Here comes the coding of our second report ZWO_MINI_SCHEDULER_SUB:



REPORT ZWO_MINI_SCHEDULER_SUB MESSAGE-ID bt.

INCLUDE lbtchdef.
INCLUDE ZWO_MINI_SCHEDULER_UTILS.



PARAMETERS im_jn TYPE btcjob NO-DISPLAY.
PARAMETERS pred_jn TYPE btcjob NO-DISPLAY.
PARAMETERS pred_jc TYPE btcjobcnt NO-DISPLAY.
PARAMETERS im_rep TYPE sy-repid.

DATA l_status TYPE btcstatus.
DATA l_jobcount TYPE btcjobcnt.
DATA l_immed TYPE c.
DATA l_evparam TYPE btcevtparm.
DATA l_event TYPE btceventid.
DATA lo_util TYPE REF TO lcl_utilities.

START-OF-SELECTION.

  CREATE OBJECT lo_util.

* check for status of predecessor job
  CALL FUNCTION 'BP_JOB_STATUS_GET'
    EXPORTING
      jobcount                   = pred_jc
      jobname                    = pred_jn
*     READ_ONLY_STATUS           =
    IMPORTING
      status                     = l_status
*     HAS_CHILD                  =
    EXCEPTIONS
      job_doesnt_exist           = 1
      unknown_error              = 2
      parent_child_inconsistency = 3
      OTHERS                     = 4.
  IF sy-subrc <> 0.
    MESSAGE e351 WITH 'Error in BP_JOB_STATUS_GET'.
  ENDIF.

  IF l_status = btc_finished.

    l_immed = abap_true.

  ELSEIF l_status = btc_released OR l_status = btc_ready OR l_status = btc_running.
    " predecessor is still running, schedule job after the end of predecessor

    l_event = 'SAP_END_OF_JOB'.

    l_evparam(32) = pred_jn.
    l_evparam+32(8) = pred_jc.

  ENDIF.

  CALL FUNCTION 'JOB_OPEN'
    EXPORTING
      jobname                = im_jn
   IMPORTING
     jobcount               = l_jobcount
*     INFO                   =
*   CHANGING
*     RET                    =
   EXCEPTIONS
     cant_create_job        = 1
     invalid_job_data       = 2
     jobname_missing        = 3
     OTHERS                 = 4
            .
  IF sy-subrc <> 0.
    MESSAGE e351 WITH 'Error in JOB_OPEN'.
  ENDIF.

  CALL FUNCTION 'JOB_SUBMIT'
    EXPORTING
      authcknam                         = sy-uname
      jobcount                          = l_jobcount
      jobname                           = im_jn
      report                            = im_rep
*     VARIANT                           = ' '
   EXCEPTIONS
     bad_priparams                     = 1
     bad_xpgflags                      = 2
     invalid_jobdata                   = 3
     jobname_missing                   = 4
     job_notex                         = 5
     job_submit_failed                 = 6
     lock_failed                       = 7
     program_missing                   = 8
     prog_abap_and_extpg_set           = 9
     OTHERS                            = 10
            .
  IF sy-subrc <> 0.
    lo_util->delete_job( im_jn = im_jn im_jc = l_jobcount ).
    MESSAGE e351 WITH 'Error in JOB_SUBMIT'.
  ENDIF.

  CALL FUNCTION 'JOB_CLOSE'
    EXPORTING
      event_id                          = l_event
     event_param                       = l_evparam
     jobcount                          = l_jobcount
     jobname                           = im_jn
     strtimmed                         = l_immed
   EXCEPTIONS
     cant_start_immediate              = 1
     invalid_startdate                 = 2
     jobname_missing                   = 3
     job_close_failed                  = 4
     job_nosteps                       = 5
     job_notex                         = 6
     lock_failed                       = 7
     invalid_target                    = 8
     OTHERS                            = 9
            .
  IF sy-subrc <> 0.
    lo_util->delete_job( im_jn = im_jn im_jc = l_jobcount ).
    MESSAGE e351 WITH 'Error in JOB_CLOSE'.
  ENDIF.


And finally, here is the coding of the include ZWO_MINI_SCHEDULER_UTILS:



*&---------------------------------------------------------------------*
*&  Include           ZWO_MINI_SCHEDULER_UTILS
*&---------------------------------------------------------------------*

*----------------------------------------------------------------------*
*       CLASS lcl_utilities DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_utilities DEFINITION FINAL.

  PUBLIC SECTION.

    METHODS delete_job IMPORTING im_jn TYPE btcjob
                         im_jc TYPE btcjobcnt.

ENDCLASS.                    "lcl_utilities DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_utilities IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_utilities IMPLEMENTATION.

  METHOD delete_job.

    CALL FUNCTION 'BP_JOB_DELETE'
      EXPORTING
        jobcount                 = im_jc
        jobname                  = im_jn
*       FORCEDMODE               = ' '
*       COMMITMODE               = 'X'
      EXCEPTIONS
        cant_delete_event_entry  = 1
        cant_delete_job          = 2
        cant_delete_joblog       = 3
        cant_delete_steps        = 4
        cant_delete_time_entry   = 5
        cant_derelease_successor = 6
        cant_enq_predecessor     = 7
        cant_enq_successor       = 8
        cant_enq_tbtco_entry     = 9
        cant_update_predecessor  = 10
        cant_update_successor    = 11
        commit_failed            = 12
        jobcount_missing         = 13
        jobname_missing          = 14
        job_does_not_exist       = 15
        job_is_already_running   = 16
        no_delete_authority      = 17
        OTHERS                   = 18.
    IF sy-subrc <> 0.
    ENDIF.

  ENDMETHOD.                    "delete_job

ENDCLASS.                    "lcl_utilities IMPLEMENTATION



For testing, we create a variant that uses report RSWAITSEC for the first job and BTCTEST and BCALV_TEST_LIST for the other jobs.






In SM36, we create a master job with report
ZWO_MINI_SCHEDULER and our variant:





In our example, we want to schedule our job as a daily job:




SM37 will show us four jobs in total, the master jobs and the three spawned jobs.





Labels in this area