Thursday, November 17, 2011

Display return messages from BAPI and BI calls

You have the requirements, you plan the algorithm, you search for the correct BAPI that fulfills the requirements, you plan the UI and start writing some code.

Everything seems to be fine and as you proceed you have to face the boring part of displaying the return messages from the BAPI call.

How you should display the return messages? There is always the solution of writing the output as a list, but I don't think that anyone likes the old-school-fashioned list display. Moreover there is always the need to display messages from 2 or more recursive BAPI calls or from a combination between a BAPI and Batch Input call.

One good solution is to create a custom function module which will display the return messages in a popup using the application log functionality.























FUNCTION z_display_return_messages.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  TABLES
*"      BAPIRET2 TYPE  BAPIRET2_T OPTIONAL
*"      BI_MESSAGES TYPE  ETTCD_MSG_TABTYPE OPTIONAL
*"----------------------------------------------------------------------




* Message log structures
  DATA: ls_s_log  TYPE bal_s_log,
        ls_s_msg  TYPE bal_s_msg,
        ls_s_prof TYPE bal_s_prof,
        lv_handle  TYPE balloghndl.




  DATA ls_return2  TYPE bapiret2.
  DATA ls_messages TYPE bdcmsgcoll.




  CLEAR: ls_s_log, ls_s_prof, lv_handle.


  ls_s_log-aldate = sy-datum.
  ls_s_log-altime = sy-uzeit.
  ls_s_log-aluser = sy-uname.
  ls_s_log-alprog = sy-repid.


  CALL FUNCTION 'BAL_LOG_CREATE'
    EXPORTING
      i_s_log                 = ls_s_log
    IMPORTING
      e_log_handle            = lv_handle
    EXCEPTIONS
      log_header_inconsistent = 1
      OTHERS                  = 2.




  IF LINES( bapiret2 ) > 0.
    LOOP AT bapiret2 INTO ls_return2 .


      CLEAR ls_s_msg.


      ls_s_msg-msgid = ls_return2-id.
      ls_s_msg-msgty = ls_return2-type.
      ls_s_msg-msgno = ls_return2-number.
      ls_s_msg-msgv1 = ls_return2-message_v1.
      ls_s_msg-msgv2 = ls_return2-message_v2.
      ls_s_msg-msgv3 = ls_return2-message_v3.
      ls_s_msg-msgv4 = ls_return2-message_v4.




      CASE ls_s_msg-msgty.
        WHEN 'E'.
          ls_s_msg-probclass = 1.
        WHEN 'W'.
          ls_s_msg-probclass = 2.
        WHEN 'I'.
          ls_s_msg-probclass = 3.
        WHEN 'S'.
          ls_s_msg-probclass = 4.
      ENDCASE.


      CALL FUNCTION 'BAL_LOG_MSG_ADD'
        EXPORTING
          i_log_handle     = lv_handle
          i_s_msg          = ls_s_msg
        EXCEPTIONS
          log_not_found    = 1
          msg_inconsistent = 2
          log_is_full      = 3
          OTHERS           = 4.
    ENDLOOP.


  ENDIF.


  IF LINES( bi_messages ) > 0.
    LOOP AT bi_messages INTO ls_messages .


      CLEAR ls_s_msg.


      ls_s_msg-msgid = ls_messages-msgid.
      ls_s_msg-msgty = ls_messages-msgtyp.
      ls_s_msg-msgno = ls_messages-msgnr.
      ls_s_msg-msgv1 = ls_messages-msgv1.
      ls_s_msg-msgv2 = ls_messages-msgv2.
      ls_s_msg-msgv3 = ls_messages-msgv3.
      ls_s_msg-msgv4 = ls_messages-msgv4.




      CASE ls_s_msg-msgty.
        WHEN 'E'.
          ls_s_msg-probclass = 1.
        WHEN 'W'.
          ls_s_msg-probclass = 2.
        WHEN 'I'.
          ls_s_msg-probclass = 3.
        WHEN 'S'.
          ls_s_msg-probclass = 4.
      ENDCASE.


      CALL FUNCTION 'BAL_LOG_MSG_ADD'
        EXPORTING
          i_log_handle     = lv_handle
          i_s_msg          = ls_s_msg
        EXCEPTIONS
          log_not_found    = 1
          msg_inconsistent = 2
          log_is_full      = 3
          OTHERS           = 4.
    ENDLOOP.
  ENDIF.




  IF sy-subrc = 0.
    CALL FUNCTION 'BAL_DSP_PROFILE_POPUP_GET'
      IMPORTING
        e_s_display_profile = ls_s_prof.


    CALL FUNCTION 'BAL_DSP_LOG_DISPLAY'
      EXPORTING
        i_s_display_profile  = ls_s_prof
      EXCEPTIONS
        profile_inconsistent = 1
        internal_error       = 2
        no_data_available    = 3
        no_authority         = 4
        OTHERS               = 5.
  ENDIF.


  CALL FUNCTION 'BAL_LOG_REFRESH'
    EXPORTING
      i_log_handle  = lv_handle
    EXCEPTIONS
      log_not_found = 1
      OTHERS        = 2.






ENDFUNCTION.