Friday, August 2, 2013

Capture error messages for function modules without exceptions

At times we see function modules without any exceptions but still there is an error message being thrown inside the function module if any piece of code fails. If this situation happens then the program terminates at that point. In this case there is no SY-SUBRC other than 0.
To solve this, in the program where you have written your code for the FM, include the following lines of code.

EXCEPTIONS
    error_message = 99.
By doing this you will ensure that whenever there is any error thrown by the function module, SY-SUBRC is set as 99 and you can then capture the message in system fields i.e. SY-MSGID, SY-MSGTY, SY-MSGNO, SY-MSGV1, SY-MSGV2, SY-MSGV3, SY-MSGV4, and SY-MSGV5.
SAMPLE CODE:
CALL FUNCTION 'RV_CUSTOMER_MATERIAL_UPDATE'
  
TABLES
    xknmt_tab     = lt_xknmt
    yknmt_tab     = lt_ykmt
    tcatalog_tab  = lt_cat
  
EXCEPTIONS
    error_message = 
99.
  IF sy-subrc <> 0.
     
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  
ENDIF.

The above logic is taken from the link http://sapabapbysrini.blogspot.com/2012/07/capture-error-messages-for-function.html

How to export data to memory ID of a update task.

Export data to a memory ID works fine between programs but what if there is a requirement to export memory to update task?

Solution: Write a logic to export the data to memory inside a Function module and in the attributes select the radio button 'Update Module'. Then call this Function module with addition 'IN UPDATE TASK'  in the beginning of the program. When commit is triggered, this FM will be called first this will export data to memory of the update session. So the next update calls within the session can import data from that memory.