Thursday, 13 April 2017

Background Job Processing

Following is a simple example to show how to perform background job processing:

*&---------------------------------------------------------------------*
*& Report  ZTEST
*&
*&---------------------------------------------------------------------*
*& Background Job Processing
*&---------------------------------------------------------------------*

REPORT ztest.

SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS: p_for RADIOBUTTON GROUP gr1 DEFAULT 'X' USER-COMMAND f1,
            p_back RADIOBUTTON GROUP gr1.
PARAMETERS: p_name TYPE char10.
SELECTION-SCREEN: END OF BLOCK b1.

START-OF-SELECTION.
  IF p_for = 'X'.
    PERFORM disp USING p_name.
  ELSE.
    PERFORM batch_job USING p_name.
  ENDIF.

*&---------------------------------------------------------------------*
*&      Form  batch_job
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM batch_job USING p_name TYPE char10.
  DATA: lv_jobcount TYPE tbtcjob-jobcount,
        lv_jobname  TYPE tbtcjob-jobname VALUE 'ZTEST',
        lv_text     TYPE char200.

  DATA: lv_timestamp TYPE timestampl ##needed,
        lv_time      TYPE char50.

  CONSTANTS: lc_jobclass TYPE tbtcjob-jobclass VALUE 'A',
             lc_sep2     TYPE char3            VALUE  '_'.


  GET TIME STAMP FIELD lv_timestamp.
  lv_time = sy-uzeit.

  CONCATENATE lv_jobname lc_sep2 sy-datum lc_sep2 lv_time
         INTO lv_jobname .


* JOB OPEN
  CALL FUNCTION 'JOB_OPEN'
    EXPORTING
      jobname          = lv_jobname
      jobclass         = lc_jobclass
    IMPORTING
      jobcount         = lv_jobcount
    EXCEPTIONS
      cant_create_job  = 1
      invalid_job_data = 2
      jobname_missing  = 3
      OTHERS           = 4.
  IF sy-subrc <> 0.
    MESSAGE 'Error' TYPE 'S' DISPLAY LIKE 'E'.
    RETURN.
  ELSE.
*    CALLING THE SAME PROGRAM BY PASSING THE INPUT PARAMTERS
    SUBMIT ztest
    WITH p_name EQ p_name
    VIA JOB lv_jobname
    NUMBER lv_jobcount
    AND RETURN.

    IF sy-subrc = 0.
* JOB CLOSE
      CALL FUNCTION 'JOB_CLOSE'
        EXPORTING
          jobcount             = lv_jobcount
          jobname              = lv_jobname
          strtimmed            = 'X'
        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.
        MESSAGE 'Success' TYPE 'S'.
      ELSE.
        MESSAGE 'Error' TYPE 'S' DISPLAY LIKE 'E'.
        RETURN.
      ENDIF.
    ENDIF.
  ENDIF.
ENDFORM.                    "batch_job

*&---------------------------------------------------------------------*
*&      Form  DISP
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM disp USING p_name TYPE char10.
  WRITE: 'Hello', p_name.
ENDFORM.                    " DISP

NOTE: You can view the background job in SM37. Click on execute and then you can view the spool.

No comments:

Post a Comment

Report to find CDS view of Standard Table

A small change has been made to the original program ( SAP YARD Article ) so that it can also display the common CDS used by multiple table...