1 1 1 1 1 Rating 0% (0 Votes)
When you need to call a FM with option starting new task there is always a risk that user already has maximum number of sessions opened, so there is a risk that our call will be unsuccessful. There is a way to avoid such risk -> easily call FM  RM_FREE_SESSION_CHECK or TH_USER_INFO directly to check if we can afford to open new session. If yes then we can call our FM in new task, if not then we can raise an error message and stop processing of the function we're currently doing. This is very helpful when you need to call a FM in new task inside user-exit.
Here is example of the code:

data: gt_return type bapiret2_tt.   

call function 'RM_FREE_SESSION_CHECK'
        no_free_session 1
        others          2.
    if sy-subrc eq 0.

"Z FM with standard one iside to be able to do commit & rollback
      call function 'Z_BAPI_GOODSMVT_CREATE' 
      starting new task 'NEW_TASK'
       performing check_return on end of task
        goodsmvt_header               fs_goodsmvt_header
*           GOODSMVT_REF_EWM              =
*         IMPORTING
*           GOODSMVT_HEADRET              =
*           MATERIALDOCUMENT              =
*           MATDOCUMENTYEAR               =
        goodsmvt_item                 ft_goodsmvt_item
*           GOODSMVT_SERIALNUMBER         =
        return                        ft_return
*           GOODSMVT_SERV_PART_DATA       =
*           EXTENSIONIN                   =


      wait until g_separatetask_done eq 'X' up to 120 seconds.

      message e027(14).
*   Maximum number of sessions reached

*   Rest of the code goes here

* form that is called after task is complete

form check_return using taskname.
  receive results from function 'Z_BAPI_GOODSMVT_CREATE'
    return gt_return.
  g_separatetask_done 'X'.

endform.                    "check_return