Module 24: Background Jobs & Scheduling
Background jobs allow ABAP programs to run without user interaction, typically for batch processing, interfaces, data loads, and housekeeping tasks.
In real systems, most critical processing happens in background.
1. Why Background Jobs?
Background jobs are used when:
- Processing is long-running
- Large data volumes are involved
- No UI interaction is required
- Execution must be scheduled (daily, weekly, etc.)
Well-designed background jobs are stable, restartable, and monitored.
2. Job Creation
Jobs are created and managed using transaction SM36 (create) and SM37 (monitor).
2.1 Job Lifecycle
Created → Released → Ready → Active → Finished / Cancelled
2.2 Creating a Job (Conceptual Flow)
-
Define job name
-
Assign job steps (ABAP program / external command)
-
Define start condition
-
Release the job
2.3 Programmatic Job Creation (ABAP)
CALL FUNCTION 'JOB_OPEN'
EXPORTING
jobname = lv_jobname
IMPORTING
jobcount = lv_jobcount.
CALL FUNCTION 'JOB_SUBMIT'
EXPORTING
jobname = lv_jobname
jobcount = lv_jobcount
report = 'Z_BATCH_PROGRAM'.
CALL FUNCTION 'JOB_CLOSE'
EXPORTING
jobname = lv_jobname
jobcount = lv_jobcount
strtimmed = abap_true.
Most jobs are created via SM36, not programmatically—ABAP job creation is used only in special cases.
3. Job Classes
Job classes define priority and resource usage.
3.1 Job Class Types
| Job Class | Priority | Usage |
|---|---|---|
| A | High | Critical / urgent jobs |
| B | Medium | Normal business jobs |
| C | Low | Housekeeping / reports |
Job class A must be approved by BASIS.
3.2 Choosing the Right Job Class
-
Financial close → Class A (rare)
-
Daily batch jobs → Class B
-
Reports / cleanup → Class C
4. Spool Handling
Background jobs typically generate spool requests.
4.1 What is a Spool?
A spool is:
-
Output generated by a background job
-
Stored on the application server
-
Viewable via SP01 / SM37
4.2 Accessing Spool Programmatically
NEW-PAGE PRINT ON.
WRITE: 'Batch job output'.
NEW-PAGE PRINT OFF.
4.3 Controlling Spool Behavior
SUBMIT z_report
TO SAP-SPOOL
WITHOUT SPOOL DYNPRO
WITH p_param = lv_value.
Suppress spool output unless business users explicitly need it.
5. Error Handling in Background Jobs
5.1 Why Error Handling is Critical
Background jobs:
-
Have no user interaction
-
Must detect and report errors automatically
-
Must be restartable
A failing job with no error logging is worse than no job.
5.2 Detecting Errors
Common techniques:
-
SY-SUBRC checks
-
TRY…CATCH blocks
-
Return codes
-
Application logs (SLG1)
TRY.
lo_service->process( ).
CATCH cx_root INTO DATA(lo_exc).
" Log error
ENDTRY.
5.3 Logging Errors (Recommended)
Use Application Log (SLG1) for job errors.
CALL FUNCTION 'BAL_LOG_MSG_ADD'
EXPORTING
i_log_handle = lv_log_handle
i_msgty = 'E'
i_msgid = 'ZMSG'
i_msgno = '001'.
Use SLG1 logs, not WRITE statements, for background jobs.
5.4 Job Status on Errors
-
Unhandled exception → Job Cancelled
-
Controlled error → Job Finished, error logged
A job should finish gracefully whenever possible.
6. Restartability & Idempotency
6.1 Restartable Jobs
A good job:
-
Can be restarted without data corruption
-
Handles partial execution
-
Uses checkpoints or status tables
Design jobs to be idempotent wherever possible.
7. Common Mistakes
-
Using WRITE instead of logs
-
No error handling
-
Long-running dialog programs
-
Hard-coded job parameters
-
Excessive spool generation
8. Interview-Grade Answer
Q: How do you design a reliable background job?
Answer:
I ensure the job runs in background-safe mode, uses proper error handling and application logging, minimizes spool output, selects the appropriate job class, and is designed to be restartable without data inconsistencies.
This shows real production experience, not just theory.
9. Summary
-
Background jobs run without user interaction
-
Created via SM36 or ABAP APIs
-
Job classes control priority
-
Spool handles output
-
Error handling and logging are critical
-
Jobs must be restartable
10. Practice Exercises
-
Create a background job using SM36.
-
Run and monitor it in SM37.
-
Suppress unnecessary spool output.
-
Log errors using SLG1.
-
Simulate a job failure and analyze logs.
11. What’s Next?
➡️ Module 25: RFC, BAPI & Integration
Background jobs are the heartbeat of enterprise SAP systems.