How To Automatically Execute A Batch Session

In this tutorial, we will explain how to automatically execute a batch session via code. It will helpful in scenario where we don\’t want to manually execute batch session from transaction SM35.

 

Step 1: Create a new job:

* The function module \"JOB_OPEN\" will create the job.
  CALL FUNCTION \'JOB_OPEN\'
    EXPORTING
      jobname          = lv_job_name      \"Job name
    IMPORTING
      jobcount         = lv_job_number    \"Internally generated job number
    EXCEPTIONS
      cant_create_job  = 1
      invalid_job_data = 2
      jobname_missing  = 3
      OTHERS           = 4.

 

Step 2: Automatically execute the job created in the step above:

The code logic will execute the program \’RSBDCSUB\’ which will automatically execute the specified batch session. Additionally, the submit statement will execute via a job which will be useful to retrieve the status of the batch session after it has been executed. The job will also us to know if the batch session has been executed successfully or not.

SUBMIT rsbdcsub
WITH mappe     EQ \'ZBATCHSESSION\'   \" Name of the batch session.
WITH von       EQ sy-datum          \" Current Date (Created To).
WITH bis       EQ sy-datum          \" Current Date (Created From).
WITH z_verarb  EQ \'X\'               \" To Process.
WITH fehler    EQ space             \" Execute batch session in error.
WITH batchsys  EQ space             \" Target Host.
WITH logall    EQ space             \" Extended log.
TO SAP-SPOOL
SPOOL PARAMETERS   gst_print_params \" Spool Parameters.
WITHOUT SPOOL DYNPRO
VIA JOB          lv_job_name        \" Job Name.
NUMBER           lv_job_number      \" Job Number.
AND RETURN.

Please note:
The spool parameters mentioned above (in structure \’GST_PRINT_PARAMS) can be filled using the function module \’GET_PRINT_PARAMETERS\’. Please refer to the following code logic:

CALL FUNCTION \'GET_PRINT_PARAMETERS\'
    EXPORTING
      destination            = \'LOCL\'              \" Printer Destination
      layout                 = \'X_65_132\'          \" Printing Layout
      list_text              = \'Batch Input Test\'  \" Text
      mode                   = \'BATCH\'             \" Mode
      no_dialog              = \'X\'                 \" Without Dialog
      report                 = \'RFBIBL00\'          \" Report Name
      user                   = sy-uname            \" Username
    IMPORTING
      out_parameters         = gst_print_params    \" Spool Parameters
    EXCEPTIONS
      archive_info_not_found = 1
      invalid_print_params   = 2
      invalid_archive_params = 3
      OTHERS                 = 4.

Declaration Information:
The type of spool parameters structure \"GST_PRINT_PARAMS\" is \"PRI_PARAMS\".

 

Step 3: Close the job:

We will have to close the job created in step 1 using the function module \’JOB_CLOSE\’.

* The function module JOB_OPEN will close a job.
  CALL FUNCTION \'JOB_CLOSE\'
    EXPORTING
      jobcount             = lv_job_number          \" Job Number (from step 1)
      jobname              = lv_job_name            \" Job Name
      strtimmed            = \'X\'                    \" Immeditate Execution
    EXCEPTIONS
      cant_start_immediate = 1
      invalid_startdate    = 2
      jobname_missing      = 3
      job_close_failed     = 4
      job_nosteps          = 5
      job_notex            = 6
      lock_failed          = 7
      OTHERS               = 8.

 

Step 4: Check the state of the job:

To check the state of the job executed above, we will use the function module \’BP_JOB_CHECKSTATE\’.

* This function module will return the state/status of the job.
  CALL FUNCTION \'BP_JOB_CHECKSTATE\'
    EXPORTING
      dialog                       = \'N\'              \" Dialog
      jobcount                     = lv_job_number    \" Job Number
      jobname                      = lv_job_name      \" Job Name
    IMPORTING
      status_according_to_db       = lv_job_status_db \" Job Status According to DB
      actual_status                = lv_job_status    \" Job Status
    EXCEPTIONS
      checking_of_job_has_failed   = 1
      correcting_job_status_failed = 2
      invalid_dialog_type          = 3
      job_does_not_exist           = 4
      no_check_privilege_given     = 5
      ready_switch_too_dangerous   = 6
      OTHERS                       = 7.

Declaration Information:
lv_job_number     TYPE tbtcjob-jobcount
lv_job_name       TYPE tbtcjob-jobname
lv_job_status_db  TYPE tbtcjob-status             
lv_job_status     TYPE tbtcjob-status

The status of the job executed will be available in the variable \’LV_JOB_STATUS\’.

*If the job status equals to \'F\' (Finished) or \'A\' (Aborted) means that the job execution has finished.

IF lv_job_status EQ \'F\'  \" Job Finished
OR lv_job_status EQ \'A\'.  \" Job Aborted

*Do something.

ENDIF.

 

Step 5: Retrieve the state of the batch session:
In case you want to retrieve the status of the batch session, you will have to make a retrieval in table \’APQI\’, field \’QSTATE\’;

SELECT QID		\"Queue Identification
       QSTATE 	        \"Queue Status
FROM table APQI
INTO table GT_APQI
WHERE DATATYPE	equals \'BDC\'
AND GROUPID	equals \'ZBATCHSESSION\'    \"Session Name
AND PROGID	equals \'RFBIBL01\'
AND USERID	equals SY-UNAME
AND CREDATE	equals SY-DATUM.

If sy-subrc EQ 0.

    * The status of the batch session will be available in the field \'APQI-QSTATE\'
    * If \'APQI-QSTATE\' equals to: 
    * \'F\' means it has \'Finished\'
    * \'A\' means it has \'Aborted\'
    * \'E\' means it was in \'Error\'

End If.

Voila, hope it helps.