Developer ToolsFree generatorPDF, Excel, Word exportSAP ECC 6.0All SAP ECC 6.0 tools

Create a Batch Job in SAP ABAP: JOB_OPEN, SUBMIT and JOB_CLOSE Generator

This generator writes an ABAP program for SAP ECC 6.0 that creates a background job in code: JOB_OPEN to get a job count, SUBMIT VIA JOB to add the step with a variant, step user and spool settings, and JOB_CLOSE to release it immediately, at a date and time, or after an event. It is the programmatic equivalent of SM36.

Fill in

I immediately, D at date and time, E after event

Generated code
Download:

Scheduler report

REPORT ZBC_SCHEDULE_STOCK_JOB.

DATA: lv_jobname  TYPE tbtcjob-jobname VALUE 'Z_DAILY_STOCK_EXTRACT',
      lv_jobcount TYPE tbtcjob-jobcount,
      lv_released TYPE c,
      lv_valid    TYPE c,
      ls_params   TYPE pri_params.

PARAMETERS: p_prog  TYPE sy-repid        DEFAULT 'ZMM_STOCK_EXTRACT',
            p_var   TYPE raldb-variant   DEFAULT 'DAILY',
            p_user  TYPE sy-uname        DEFAULT 'BATCH_USER',
            p_mode  TYPE c               DEFAULT 'D',
            p_date  TYPE sy-datum        DEFAULT '20261023',
            p_time  TYPE sy-uzeit        DEFAULT '020000',
            p_event TYPE tbtcjob-eventid DEFAULT 'ZSTOCK_READY',
            p_class TYPE tbtcjob-jobclass DEFAULT 'C'.

START-OF-SELECTION.

* 1. Spool parameters without a dialog
  CALL FUNCTION 'GET_PRINT_PARAMETERS'
    EXPORTING
      destination    = 'LOCL'
      immediately    = ' '
      new_list_id    = 'X'
      no_dialog      = 'X'
    IMPORTING
      out_parameters = ls_params
      valid          = lv_valid
    EXCEPTIONS
      OTHERS         = 1.
  IF sy-subrc <> 0 OR lv_valid <> 'X'.
    MESSAGE 'Print parameters are not valid' TYPE 'E'.
  ENDIF.

* 2. Open the job
  CALL FUNCTION 'JOB_OPEN'
    EXPORTING
      jobname          = lv_jobname
      jobclass         = p_class
    IMPORTING
      jobcount         = lv_jobcount
    EXCEPTIONS
      cant_create_job  = 1
      invalid_job_data = 2
      jobname_missing  = 3
      OTHERS           = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

* 3. Add the step: program, variant, step user, spool
  SUBMIT (p_prog)
    USING SELECTION-SET p_var
    TO SAP-SPOOL
    SPOOL PARAMETERS ls_params
    WITHOUT SPOOL DYNPRO
    USER p_user
    VIA JOB lv_jobname NUMBER lv_jobcount
    AND RETURN.
  IF sy-subrc <> 0.
    MESSAGE 'SUBMIT VIA JOB failed: check program, variant and step user' TYPE 'E'.
  ENDIF.

* 4. Close and release the job
  CASE p_mode.
    WHEN 'I'.
      CALL FUNCTION 'JOB_CLOSE'
        EXPORTING
          jobcount             = lv_jobcount
          jobname              = lv_jobname
          strtimmed            = 'X'
        IMPORTING
          job_was_released     = lv_released
        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.
    WHEN 'D'.
      CALL FUNCTION 'JOB_CLOSE'
        EXPORTING
          jobcount             = lv_jobcount
          jobname              = lv_jobname
          sdlstrtdt            = p_date
          sdlstrttm            = p_time
        IMPORTING
          job_was_released     = lv_released
        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.
    WHEN 'E'.
      CALL FUNCTION 'JOB_CLOSE'
        EXPORTING
          jobcount             = lv_jobcount
          jobname              = lv_jobname
          event_id             = p_event
        IMPORTING
          job_was_released     = lv_released
        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.
    WHEN OTHERS.
      MESSAGE 'Start mode must be I, D or E' TYPE 'E'.
  ENDCASE.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSEIF lv_released <> 'X'.
    MESSAGE 'Job created but not released: user lacks release authorization' TYPE 'W'.
  ELSE.
    MESSAGE 'Job scheduled and released' TYPE 'S'.
  ENDIF.

Optional: raise the event from another program (mode E)

* Run this where the upstream step finishes, for example at the end of a load program
*  CALL FUNCTION 'BP_EVENT_RAISE'
*    EXPORTING
*      eventid                = 'ZSTOCK_READY'
*    EXCEPTIONS
*      bad_eventid            = 1
*      eventid_does_not_exist = 2
*      eventid_missing        = 3
*      raise_failed           = 4
*      OTHERS                 = 5.
*  The event must exist in SM62 before JOB_CLOSE or BP_EVENT_RAISE uses it.

Check in SE37 on your release that JOB_OPEN, JOB_CLOSE and GET_PRINT_PARAMETERS have the parameters used here; they are stable across ECC 6.0 EHPs. The variant must exist for the program (SE38, Goto, Variants). Releasing needs S_BTCH_JOB with JOBACTION RELE (or S_BTCH_ADM), and naming another step user needs S_BTCH_NAM for that user. Job classes A and B are meant for high-priority work and are often reserved for Basis; check your authorization concept before using them.

Get your job chain reviewed

Send us the jobs, events and variants of a nightly chain and we return a dependency map and the scheduling code for it.

Optional. The tool and every export work without this. No spam.

Was this tool useful?

How a batch job is built in ABAP

A background job is a header (TBTCO) with one or more steps (TBTCP). JOB_OPEN creates the header in status scheduled and returns JOBCOUNT, which together with JOBNAME identifies the run. Each SUBMIT ... VIA JOB adds a step; the program is not executed at that point. JOB_CLOSE sets the start condition and, if the caller is allowed, releases the job. Without JOB_CLOSE the job stays scheduled forever and shows as status P in SM37.

  • Start immediately: STRTIMMED = 'X'.
  • Date and time: SDLSTRTDT and SDLSTRTTM.
  • After event: EVENT_ID, with the event defined in SM62.
  • JOB_WAS_RELEASED tells you whether release worked.

Creating the variant for the batch job

SUBMIT USING SELECTION-SET needs an existing variant. Run the program in SE38, fill the selection screen, choose Goto, Variants, Save as variant, and give it a name such as DAILY. For dates that move, use selection variables in the variant (dynamic date calculation from TVARVC or the current date) rather than fixed values. Protect the variant if others should not change it. The generator only references the variant by name; it does not create it.

Worked example

With job name Z_DAILY_STOCK_EXTRACT, program ZMM_STOCK_EXTRACT, variant DAILY, step user BATCH_USER, mode D, date 20261023 and time 020000, the program creates one job that runs at 02:00 under BATCH_USER and writes its list to a new spool request on LOCL. In SM37 the job appears as released. If you choose mode E with event ZSTOCK_READY, the job waits until some program calls BP_EVENT_RAISE for that event, or until you trigger it in SM64.

Gotchas

SUBMIT with USER needs S_BTCH_NAM for the named user, otherwise sy-subrc is not zero and no step is added. JOB_CLOSE fails with JOB_NOSTEPS when SUBMIT failed silently, so always check sy-subrc after SUBMIT. Periodic jobs need the PRDMINS, PRDHOURS, PRDDAYS, PRDWEEKS or PRDMONTHS parameters of JOB_CLOSE; this generator schedules one run. Do not schedule jobs in a loop without a unique job name or a check in TBTCO, or you will create duplicates.

Frequently Asked Questions

What is a batch job in SAP ABAP?

It is a program run in a background work process without a user session, defined by a job header and one or more steps. In ABAP you create it with JOB_OPEN, add steps with SUBMIT VIA JOB and release it with JOB_CLOSE. The job data is stored in TBTCO and TBTCP and monitored in SM37.

How do I schedule a batch job in SAP without SM36?

Use the generated program: JOB_OPEN returns the job count, SUBMIT adds the program and variant, and JOB_CLOSE sets the start time with SDLSTRTDT and SDLSTRTTM, or STRTIMMED for an immediate start. Run the scheduler report once in the foreground, or call the same code from another program.

Why does JOB_CLOSE return job_was_released as blank?

The job was created but the user running the code is not allowed to release it. Release needs S_BTCH_JOB with job action RELE or S_BTCH_ADM. An administrator can release it in SM37, or you can schedule the scheduler program under a user with release rights.

How do I start a job after another job finishes?

Use mode E with a custom event created in SM62 and call BP_EVENT_RAISE at the end of the upstream program, or use the PRED_JOBCOUNT and PRED_JOBNAME parameters of JOB_CLOSE to start after a predecessor job. Event-based starts are easier to reuse across several dependent jobs.

If you want planners to schedule and check approved SAP jobs by asking in plain English, with every action approved by a person, we can build that on your own servers.

ArchRay builder
Design the on-prem AI stack for SAP ECC 6.0: model, GPUs, RAG pipeline, guardrails
See memory, speed and cost as you swap parts, then export the diagram. Free, in your browser.
Open the builder

Keep exploring

Free tools

ERP fixes and how-tos

Runs with your ERP

AI on your ERP

Integrations