SAP ABAP Background Job Management and RFC Calls: Complete Guide | SAP ABAP
Emre Göçmen
Author

SAP ABAP Background Job Management and RFC Calls
Background Job management and RFC (Remote Function Call) are critical techniques in SAP ABAP systems that improve performance and distribute system load. In this guide, you will learn how to create, manage Background Jobs and use RFC calls with tested and working code in real SAP environments.
What is Background Job Management?
Background Job management is a mechanism that allows long-running processes to run in the background in SAP systems. These processes work efficiently using system resources without requiring user interaction.
Key advantages of Background Jobs:
• Does not block user interface
• Improves system performance
• Optimizes large data processing
• Provides scheduling capability
• Enables parallel processing
• Uses system resources efficiently
Basic Background Job Creation
1. Simple Job Creation
To create a basic background job:
REPORT z_background_job_example.
DATA: lv_jobname TYPE tbtcjob-jobname VALUE 'TEST_JOB',
lv_jobcount TYPE tbtcjob-jobcount.
START-OF-SELECTION.
PERFORM create_background_job USING lv_jobname
lv_jobcount.
FORM create_background_job USING iv_jobname TYPE tbtcjob-jobname
iv_jobcount TYPE tbtcjob-jobcount.
" Open job
CALL FUNCTION 'JOB_OPEN'
EXPORTING
jobname = iv_jobname
IMPORTING
jobcount = iv_jobcount
EXCEPTIONS
cant_create_job = 1
invalid_job_data = 2
jobname_missing = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE 'Job could not be opened' TYPE 'E'.
EXIT.
ENDIF.
" Add program step
CALL FUNCTION 'JOB_SUBMIT'
EXPORTING
jobcount = iv_jobcount
jobname = iv_jobname
report = 'RSUSR003' " User list report
EXCEPTIONS
bad_jobcount = 1
bad_jobname = 2
cant_add_to_job = 3
invalid_parameter = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE 'Program could not be added to job' TYPE 'E'.
EXIT.
ENDIF.
" Start job immediately
CALL FUNCTION 'JOB_CLOSE'
EXPORTING
jobcount = iv_jobcount
jobname = iv_jobname
strtimmed = 'X'
EXCEPTIONS
cant_start_job = 1
invalid_startdate = 2
jobname_missing = 3
OTHERS = 4.
IF sy-subrc = 0.
MESSAGE s001(00) WITH 'Job successfully created:' iv_jobname iv_jobcount.
ELSE.
MESSAGE 'Job could not be started' TYPE 'E'.
ENDIF.
ENDFORM.
2. Parametric Job Creation
To create job with parameters and variants:
FORM create_job_with_parameters USING iv_program TYPE sy-repid
iv_variant TYPE raldb-variant.
DATA: lv_jobname TYPE tbtcjob-jobname,
lv_jobcount TYPE tbtcjob-jobcount.
" Create unique job name
CONCATENATE 'JOB' sy-datum sy-uzeit sy-uname INTO lv_jobname SEPARATED BY '_'.
" Open job
CALL FUNCTION 'JOB_OPEN'
EXPORTING
jobname = lv_jobname
IMPORTING
jobcount = lv_jobcount
EXCEPTIONS
cant_create_job = 1
invalid_job_data = 2
jobname_missing = 3
OTHERS = 4.
CHECK sy-subrc = 0.
" Submit with program and variant
CALL FUNCTION 'JOB_SUBMIT'
EXPORTING
jobcount = lv_jobcount
jobname = lv_jobname
report = iv_program
variant = iv_variant
language = sy-langu
EXCEPTIONS
bad_jobcount = 1
bad_jobname = 2
cant_add_to_job = 3
invalid_parameter = 4
OTHERS = 5.
CHECK sy-subrc = 0.
" Start job
CALL FUNCTION 'JOB_CLOSE'
EXPORTING
jobcount = lv_jobcount
jobname = lv_jobname
strtimmed = 'X'
EXCEPTIONS
cant_start_job = 1
invalid_startdate = 2
jobname_missing = 3
OTHERS = 4.
IF sy-subrc = 0.
MESSAGE s001(00) WITH 'Parametric job created:' lv_jobname.
ENDIF.
ENDFORM.
RFC (Remote Function Call) Management
1. Synchronous RFC (sRFC)
For synchronous RFC calls:
FORM synchronous_rfc_call USING iv_destination TYPE rfcdest.
DATA: lv_date TYPE sy-datum,
lv_time TYPE sy-uzeit.
" Test RFC connection
CALL FUNCTION 'RFC_PING'
DESTINATION iv_destination
EXCEPTIONS
system_failure = 1 MESSAGE 'RFC system error'
communication_failure = 2 MESSAGE 'RFC communication error'
OTHERS = 3.
CASE sy-subrc.
WHEN 0.
MESSAGE s001(00) WITH 'RFC connection successful:' iv_destination.
" Get date/time from remote system
CALL FUNCTION 'SYSTEM_GET_DATE_TIME'
DESTINATION iv_destination
IMPORTING
date = lv_date
time = lv_time
EXCEPTIONS
system_failure = 1
communication_failure = 2
OTHERS = 3.
IF sy-subrc = 0.
MESSAGE s001(00) WITH 'Remote system date:' lv_date lv_time.
ENDIF.
WHEN 1.
MESSAGE 'RFC system error occurred' TYPE 'E'.
WHEN 2.
MESSAGE 'RFC communication error' TYPE 'E'.
WHEN OTHERS.
MESSAGE 'RFC ping failed' TYPE 'E'.
ENDCASE.
ENDFORM.
2. Asynchronous RFC (aRFC)
For asynchronous RFC calls:
DATA: gv_task_counter TYPE i VALUE 0.
FORM asynchronous_rfc_call USING iv_destination TYPE rfcdest.
DATA: lv_task_id TYPE string.
" Create task ID
gv_task_counter = gv_task_counter + 1.
CONCATENATE 'TASK' gv_task_counter INTO lv_task_id.
" Asynchronous RFC call
CALL FUNCTION 'SYSTEM_GET_DATE_TIME'
STARTING NEW TASK lv_task_id
DESTINATION iv_destination
CALLING receive_async_result ON END OF TASK
EXCEPTIONS
system_failure = 1 MESSAGE 'System error'
communication_failure = 2 MESSAGE 'Communication error'
resource_failure = 3 MESSAGE 'Resource error'
OTHERS = 4.
IF sy-subrc = 0.
MESSAGE s001(00) WITH 'Asynchronous RFC started:' lv_task_id.
ELSE.
MESSAGE 'Asynchronous RFC could not be started' TYPE 'E'.
ENDIF.
ENDFORM.
" Callback function
FORM receive_async_result USING iv_task_id TYPE string.
DATA: lv_date TYPE sy-datum,
lv_time TYPE sy-uzeit.
" Receive RFC results
RECEIVE RESULTS FROM FUNCTION 'SYSTEM_GET_DATE_TIME'
IMPORTING
date = lv_date
time = lv_time
EXCEPTIONS
system_failure = 1
communication_failure = 2
OTHERS = 3.
IF sy-subrc = 0.
MESSAGE s001(00) WITH 'Async result received:' lv_date lv_time 'Task:' iv_task_id.
ELSE.
MESSAGE e001(00) WITH 'Asynchronous RFC error, Task:' iv_task_id.
ENDIF.
ENDFORM.
Error Management and Best Practices
1. Job Error Management
For managing job errors:
FORM job_error_handling USING iv_jobname TYPE tbtcjob-jobname.
DATA: lt_failed_jobs TYPE TABLE OF tbtcjob,
ls_failed_job TYPE tbtcjob.
" Find failed jobs
CALL FUNCTION 'BP_JOB_SELECT'
EXPORTING
jobname = iv_jobname
status = 'A' " Aborted
TABLES
job_info = lt_failed_jobs
EXCEPTIONS
no_jobs_found = 1
others = 2.
IF sy-subrc = 0.
LOOP AT lt_failed_jobs INTO ls_failed_job.
MESSAGE w001(00) WITH 'Failed job:' ls_failed_job-jobname ls_failed_job-jobcount.
" Check job log
PERFORM check_job_log USING ls_failed_job-jobname ls_failed_job-jobcount.
ENDLOOP.
ENDIF.
ENDFORM.
2. RFC Error Management
For managing RFC errors:
FORM rfc_error_handling USING iv_destination TYPE rfcdest.
DATA: lv_retry_count TYPE i VALUE 3,
lv_current_try TYPE i VALUE 0,
lv_success TYPE abap_bool VALUE abap_false.
" Retry mechanism
DO lv_retry_count TIMES.
lv_current_try = lv_current_try + 1.
" RFC call
CALL FUNCTION 'RFC_PING'
DESTINATION iv_destination
EXCEPTIONS
system_failure = 1
communication_failure = 2
OTHERS = 3.
CASE sy-subrc.
WHEN 0.
lv_success = abap_true.
MESSAGE s001(00) WITH 'RFC successful, attempt:' lv_current_try.
EXIT.
WHEN 1.
MESSAGE w001(00) WITH 'RFC system error, attempt:' lv_current_try.
WHEN 2.
MESSAGE w001(00) WITH 'RFC communication error, attempt:' lv_current_try.
WHEN OTHERS.
MESSAGE w001(00) WITH 'RFC general error, attempt:' lv_current_try.
ENDCASE.
" Wait a bit if not the last attempt
IF lv_current_try < lv_retry_count.
WAIT UP TO 2 SECONDS.
ENDIF.
ENDDO.
" Result check
IF lv_success = abap_false.
MESSAGE e001(00) WITH 'RFC failed in all attempts:' iv_destination.
ENDIF.
ENDFORM.
Conclusion
Background Job management and RFC calls are critical techniques in SAP ABAP systems that improve performance and distribute system load. With the techniques learned in this guide, you can:
• Create effective background jobs
• Use RFC calls securely
• Implement error management and monitoring
• Manage parallel processing
• Optimize system performance
By applying these techniques in your projects, you can develop more performant and reliable SAP applications.
Comments
No comments yet.
Be the first to comment.



