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
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.
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.
Related Tools
SAP ECC Background Job History ABAP Query Generator (TBTCO + TBTCP)
Fill in a job name pattern, program, status and date range and get a classic ABAP report that joins TBTCO and TBTCP, computes runtime, reads the job log of cancelled jobs and shows the result in ALV.
SAPSAP Background Job Scheduling Template
A fill-in job request form that captures everything a Basis admin needs to schedule a background job in SM36 and monitor it in SM37.
SAPSAP Background Job Capacity Calculator
Estimate batch window utilisation, elapsed time and how many background work processes you need to finish nightly jobs.
More Developer Tools tools
See all- SAP ECC Enhancement Transactions and Exit Tables Reference (SMOD, CMOD, SE18, MODSAP, SXC_EXIT)
- SAP ECC Data Dictionary and Repository Tables Reference (DD02L, DD03L, T100, E070, TSTC)
- SAP ECC ABAP Runtime Errors (ST22 Dumps) and Error Log Transactions Reference
- SAP ECC IDoc Segment Data Reader ABAP Generator (EDIDC, EDID4, EDIDS)
- SAP ECC Custom Inbound IDoc Function Module Skeleton Generator
- SAP ECC BAPI_SALESORDER_CHANGE ABAP Generator
- SAP ECC BAPI_PO_CHANGE ABAP Generator
- SAP ECC BAPI_MATERIAL_SAVEDATA Material Creation ABAP Generator
- SAP ECC BAPI_ACC_DOCUMENT_POST G/L and Vendor Posting ABAP Generator
Keep exploring
Free tools
- SAP ECC Background Job History ABAP Query Generator (TBTCO + TBTCP)Fill in a job name pattern, program, status and date range and get a classic ABAP report that joins TBTCO and TBTCP, computes runtime, reads the job log of cancelled jobs and shows the result in ALV.
- SAP ECC BAPI_MATERIAL_SAVEDATA Material Creation ABAP GeneratorFill in material type, plant, sales and valuation data and get a complete ABAP report that creates the material with BAPI_MATERIAL_SAVEDATA and commits it.
- SAP ECC BAPI_PO_CHANGE ABAP GeneratorGenerate a working ABAP program that changes an SAP purchase order item with BAPI_PO_CHANGE, with POITEM/POITEMX, schedule line date conversion, test run and commit handling.
ERP fixes and how-tos
- Printer Not Working in SAP: Troubleshooting Spool Requests with SP01 and SPADFix a printer not working in SAP: read the spool request in SP01, check the output device and access method in SPAD, spool work processes and user defaults.
- SAP Short Dump TIME_OUT: Maximum Runtime ExceededSAP TIME_OUT dumps mean a dialog process ran past rdisp/max_wprun_time. Find the real bottleneck in ST22, SM50 and ST05 instead of just raising the limit.
- How to add AI to SAP ECC 6.0 on-prem without moving data to a cloudReference architecture for a private on-prem AI assistant on SAP ECC 6.0: RFC and BAPI reads, SLT replica, retrieval index, local LLM, permission mirroring.
Integrations
- How to connect Claude to SAP ECC 6.0Connect Claude to SAP ECC 6.0 through an MCP connector, the Anthropic API, AWS Bedrock or Google Vertex, and see when an on-prem open model is the better fit.
- How to use ChatGPT with SAP ECC 6.0Using ChatGPT with SAP ECC 6.0: custom GPT actions, exports versus live access, an OData or MCP bridge, enterprise data controls, and when a private model is safer.