Attila, S/4HANA, BTP Fullstack Developer (EN, DE, HU)
Plan Report / Job in background programmatically
There are cases, when you need to plan reports programatically to run in background in ABAP – providing some selection parameters – such as cleanup reports, generations, data retrieval and storage from external resources and so on. You can react on your application specific events, and plan a job to be processed by a batch work process. If your report is designed to run in background only, you can evaluate the content of system field sy-batch (structure syst).
DATA:
lv_jobcount TYPE btcjobcnt,
lv_jobcnumc TYPE crmcount,
lt_jobcount TYPE STANDARD TABLE OF btcjobcnt,
lv_dummy TYPE string. "#EC NEEDED
SELECT jobcount FROM tbtco INTO TABLE lt_jobcount
WHERE jobname = gc_jobname.
IF lt_jobcount IS NOT INITIAL.
SORT lt_jobcount DESCENDING BY table_line.
READ TABLE lt_jobcount INTO lv_jobcount INDEX 1.
lv_jobcnumc = lv_jobcount.
ENDIF.
ADD 1 TO lv_jobcnumc.
lv_jobcount = lv_jobcnumc.
* Setup Job
CALL FUNCTION 'JOB_OPEN'
EXPORTING
jobname = gc_jobname
IMPORTING
jobcount = lv_jobcount
EXCEPTIONS
cant_create_job = 1
invalid_job_data = 2
jobname_missing = 3
OTHERS = 4.
IF sy-subrc NE 0.
"handle / propagate exception
RETURN.
ENDIF.
SUBMIT cleanup_saacont "<-name of the report
VIA JOB gc_jobname
NUMBER lv_jobcount AND RETURN
WITH testrun EQ abap_true. "<-selection screen parameter
"Release Job
CALL FUNCTION 'JOB_CLOSE'
EXPORTING
jobcount = lv_jobcount
jobname = gc_jobname
strtimmed = 'X'
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.
IF sy-subrc NE 0.
"handle / propagate exception
RETURN.
ENDIF.


