Tuesday, December 28, 2010
More than once I've been requested to create a program which is able to schedule itself as a background job, usually for avoid training of the end user.Running a program in background in ABAP is pretty straightforward, and well documented, so the only issue here is passing user selections, possibly whithout hardcoding them.This is quite straightforward as well, but hard to find in the documentation, so I decided to put this code online.

The following form will do the trick: if called foreground schedules a job with the same selections and terminate the program, if run as a job returns control to the caller.

In real application you will likely need some extra feature (more error checking, output handling,...), but should be a good start.
form run_in_background.
data:seltab  type rsparams occurs 0 with header line,
jobnumber type tbtcjob-jobcount,
jobname   type tbtcjob-jobname value sy-repid,
print_parameters type pri_params.
check sy-batch is initial.
call function 'RS_REFRESH_FROM_SELECTOPTIONS'
exporting
curr_report     = sy-repid
tables
selection_table = seltab
exceptions
not_found       = 1
no_report       = 2
others          = 3.
"it shouldn't be possible to have an exception here!
call function 'JOB_OPEN'
exporting
jobname          = jobname
importing
jobcount         = jobnumber
exceptions
cant_create_job  = 1
invalid_job_data = 2
jobname_missing  = 3
others           = 4.
if sy-subrc <> 0.
message 'Can''t create a new job' type 'E'.
else.
submit (sy-repid) with selection-table seltab
via job jobname number jobnumber and return.
if sy-subrc <> 0.
message 'Error adding a step for background execution' type 'E'.
else.
call function 'JOB_CLOSE'
exporting
jobcount             = jobnumber
jobname              = 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 <> 0.
message 'Can''t close the new job' type 'E'.
else.
message 'Background processing successfully scheduled' type 'S'.
endif.
endif.
endif.
leave program.
endform.                    "run_in_background

0 comments:

Post a Comment

Your useful comments, suggestions are appreciated.Your comments are moderated.

Followers

Contact Form

Name

Email *

Message *

Web Dynpro ABAP Book

An SAP Consultant

Follow US


Want to Contribute ?

If you are interested in writing about the new stuff you learn everyday while working, please write to the.sap.consultants@gmail.com.

Click on Contribution for more details.