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.
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
Followers
Popular Posts
- ABAP - ALV Report example with steps
- ABAP - Sending email with pdf attachment
- ABAP - Step by step tutorial on Smart Forms - Template Node
- SAP Adobe Form - Steps to create simple ADOBE Form and calling it from ABAP Program
- SAP ABAP - CL_ABAP_CHAR_UTILITIES class usage
- ABAP - Multiple value selection from F4 help for SELECT-OPTIONS
- Execute ABAP Report using SUBMIT statement
- ABAP - Select all or Deselect all in ALV or Check box handling in ALV
- Web Dynpro ABAP ALV - ON_CLICK event
- ABAP - Dynamic WHERE clause
0 comments:
Post a Comment
Your useful comments, suggestions are appreciated.Your comments are moderated.