Friday, October 21, 2016

Creating Background Jobs in SAP with ABAP

Starting Jobs

    call function 'JOB_OPEN'
      exporting
        jobname          mv_jobname
      importing
        jobcount         mv_jobcount
      changing
        ret              lv_ret
      exceptions
        cant_create_job  1
        invalid_job_data 2
        jobname_missing  3
        others           4.

    if sy-subrc <> 0.
      " log
      return.
    endif.

*    submit (mo_run->ms_run-client_report)
*      using selection-set mv_variant
*      user sy-uname via job mv_jobname number mv_jobcount
*      and return.
* Using the variant above, SM37 will create a temporary

* Variante ..001 but not the previously created variant

    call function 'JOB_SUBMIT'
      exporting
        authcknam               sy-uname
        jobcount                mv_jobcount
        jobname                 mv_jobname
        report                  mo_run->ms_run-client_report
        variant                 mv_variant
      importing
        step_number             lv_stepcount
      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.
      " log
      return.
    endif.

    call function 'JOB_CLOSE'
      exporting
        jobcount             mv_jobcount
        jobname              mv_jobname
        targetsystem         iv_server
        strtimmed            'X' " start immediately
      importing
        job_was_released     lv_job_was_released
      changing
        ret                  lv_ret
      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.
       " log
      return.
    endif.


Creating Variants

    data ls_varid type varid.

    ls_varid-mandt      sy-mandt" Mandant
    ls_varid-report     mo_run->ms_run-client_report
    ls_varid-variant    mv_variant
    ls_varid-flag1      ''" CHAR01-Datenelement fuer SYST
    ls_varid-flag2      ''" CHAR01-Datenelement fuer SYST
    ls_varid-transport  ''
    ls_varid-environmnt 'B'" Umgebung (B = nur Batch)
    ls_varid-protected  ''
    ls_varid-secu       ''" Berechtigungsgruppe
    ls_varid-version    ''" Versionsnummer der Variante
    ls_varid-ename      sy-uname" Benutzername
    ls_varid-edat       sy-datum" DATE (8-stelig) fuer SYST
    ls_varid-etime      sy-uzeit" TIME-Datenelement fuer SYST
    ls_varid-aename     sy-uname" Benutzername
    ls_varid-aedat      sy-datum" DATE (8-stelig) fuer SYST
    ls_varid-aetime     sy-uzeit" TIME-Datenelement fuer SYST
    ls_varid-mlangu     sy-langu" Sprachenschlüssel
    ls_varid-xflag1     ''" Flag für Variantenkatalog
    ls_varid-xflag2     ''" Flag für Variantenkatalog

    " Varianten Text
    data ls_varit type varit.
    data lt_varit type standard table of varit.

    ls_varit-mandt sy-mandt.
    ls_varit-langu sy-langu.
    ls_varit-report mo_run->ms_run-client_report.
    ls_varit-variant mv_variant.
    ls_varit-vtext |{ sy-datum date iso }|

    append ls_varit to lt_varit.

* Save Variante for Job
    call function 'RS_CREATE_VARIANT'
      exporting
        curr_report               = mv_client_report
        curr_variant              mv_variant
        vari_desc                 ls_varid
      tables
        vari_contents             mt_rsparam
        vari_text                 lt_varit
      exceptions
        illegal_report_or_variant 1
        illegal_variantname       2
        not_authorized            3
        not_executed              4
        report_not_existent       5
        report_not_supplied       6
        variant_exists            7
        variant_locked            8
        others                    9.

    if sy-subrc 0.
      commit work.
    else.
      " log
      clear mv_variant.
    endif.


No comments:

Post a Comment

SAP ABAP: Determine Timezone for Plant

    DATA:       lt_tzone TYPE STANDARD TABLE OF tznzone WITH DEFAULT KEY,       l_tzone  TYPE tznzone.     " get time zone for plant   ...