Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Executing a subroutine in background

Former Member
0 Likes
3,307

I have a small requirement to execute a piece of code in background.

It is basically a subroutine which calls a BAPI to update the database.

the first idea u thought of was to change the subroutine into a FM and call it in background task.

or, i was wondering if there's any other method which best suits my simple criteria.

any help would be appreciated

9 REPLIES 9
Read only

Former Member
0 Likes
1,883

Hi Poornima,

Replace the subroutine with a Function Module and call it in background. That is the SIMPLE and BEST option for the requirement. Dont look for alternatives.

Best Regards,

Ram.

Read only

Former Member
0 Likes
1,883

Is there any way to debug a FM in background task?

I cant do that from SM37 as this doesn't seem to create a job

Read only

0 Likes
1,883

Hi Poornima,

When you are using function module in background and if you want to debug the same

then you have to use " Commit work statement " .

For more info have a glance on the below link

Cheers!!

VEnk@

Read only

Former Member
0 Likes
1,883

Hallo!

Using FM and call it in background task - is the best method for your purpose.

But in some cases is better to use Submit VIA JOB statement.

You can put your subroutine into report and call it like this:

DATA: number          TYPE tbtcjob-jobcount, 
            name             TYPE tbtcjob-jobname VALUE 'JOB_TEST', 
            print_parameters TYPE pri_params. 

... 

CALL FUNCTION 'JOB_OPEN' 
  EXPORTING 
    jobname          = name 
  IMPORTING 
    jobcount         = number 
  EXCEPTIONS 
    cant_create_job  = 1 
    invalid_job_data = 2 
    jobname_missing  = 3 
    OTHERS           = 4. 
IF sy-subrc = 0. 
  SUBMIT submitable TO SAP-SPOOL
 
                    SPOOL PARAMETERS print_parameters
 
                    WITHOUT SPOOL DYNPRO
 
                    VIA JOB name NUMBER number
 
                    AND RETURN.
  IF sy-subrc = 0. 
    CALL FUNCTION 'JOB_CLOSE' 
      EXPORTING 
        jobcount             = number 
        jobname              = name 
        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. 
      ... 
    ENDIF. 
  ENDIF. 
ENDIF.

Read only

Former Member
0 Likes
1,883

Alright, i've created a FM now and called in background. I've used a COMMIT WORK right after the FM call. doesnt update still.

i tried using a COMMIT WORK inside the FM as well just in case, doesnt work either. am i missing something?

Read only

0 Likes
1,883

HI Poornima,

If you can reply to the author who have given the answer then he/she can get back to you soon.

If you are replying to yourself then it will become hard for us to check and reply.

Use commit work as shown

CALL FUNCTION '<NAME Function module>'

EXPORTING

document_data = doc_chng

put_in_outbox = 'X'

commit_work = 'X'

TABLES

packing_list = t_packing_list

Cheers!!

VENk@

Read only

0 Likes
1,883

Hello,

Is COMMIT_WORK an implicit import paramenter for the FM? Could you please explain this bit more? How does this actually work?

Read only

Former Member
0 Likes
1,883

The FM seems to work fine in the foreground. In background task it doesn't execute as i was expecting. how could i debig it?

Read only

0 Likes
1,883

Use the command JDBG to debug the background job. Hope you are not updating any standard table in the FM