Save before next screen - SchwarzIT/sap-usi-logging-api GitHub Wiki

In backend applications, you should save the log before displaying the next screen. Otherwise, the user could exit the application while the screen is displayed and the call to the save method might never be reached. This might result in losing the entire log!

Anti-Pattern

METHOD bad_method.
  " Open log
  DATA(logger) = /usi/cl_bal_factory=>get_instance( )->create_new_logger( i_log_object  = 'ZMY_REPORT'
                                                                          i_sub_object  = 'DELETE_USER'
                                                                          i_external_id = i_username ).
  DATA(token) = logger->claim_ownership( ).

  " Business logic, that creates log messages
  [...]

  " DON'T DO THIS!
  "   The subsequent lines will be processed after the user left the screen.
  "   If the user would terminate the application or just not do anything, 
  "   until a timeout occurs, method logger->save would never be called and 
  "   the log would be lost!
  CALL SCREEN 2000.

  " Save & destroy log
  logger->save( token ).
  logger->free( token ).
ENDMETHOD.

The issue does not only occur for CALL SCREEN but for every statement, that would display another screen. Displaying an internal table using CL_SALV_TABLE would have the same effect.

Better solution

METHOD better_method.
  " Open log
  DATA(logger) = /usi/cl_bal_factory=>get_instance( )->create_new_logger( i_log_object  = 'ZMY_REPORT'
                                                                          i_sub_object  = 'DELETE_USER'
                                                                          i_external_id = i_username ).
  DATA(token) = logger->claim_ownership( ).

  " Business logic, that creates log messages
  [...]

  " Save & destroy log
  logger->save( token ).
  logger->free( token ).

  " Displaying the next screen AFTER saving the log avoids the issue.
  CALL SCREEN 2000.
ENDMETHOD.