BDC/BAPI报错消息转换(函数)

导语:在用BDC或者BAPI的时候,RETURN的消息是个内表,每次都要拼接成消息号,比较麻烦,所以写一个通用函数来拼接一下,这样很多地方都可以用了。

函数名称:

字段 描述 属性
函数名称 ZFM_YH_SE91 BDC/BAPI报错消息转换
函数入参 IS_MESSAGE_BDC 邮件的主题 选填
IS_MESSAGE_BAPI 附件名称 选填
函数出参 MESSAGE 返回消息

函数代码

FUNCTION zfm_yh_se91.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(IS_MESSAGE_BDC) TYPE  BDCMSGCOLL OPTIONAL
*"     VALUE(IS_MESSAGE_BAPI) TYPE  BAPIRET2 OPTIONAL
*"  EXPORTING
*"     VALUE(MESSAGE)
*"----------------------------------------------------------------------

  DATA : lv_tabix TYPE i.
  DATA : lv_field TYPE char10.
  DATA : lv_message TYPE char20.
  DATA : BEGIN OF lw_text,
           text TYPE t100-text,
         END OF lw_text.
  DATA : lt_text LIKE TABLE OF lw_text.

  IF is_message_bdc IS NOT INITIAL.

    SELECT SINGLE * FROM t100 INTO @DATA(lw_t100) WHERE arbgb = @is_message_bdc-msgid AND msgnr = @is_message_bdc-msgnr AND sprsl = '1'.
    SPLIT lw_t100-text AT '&' INTO TABLE lt_text.
    CLEAR lv_tabix.
    LOOP AT lt_text INTO lw_text.
      lv_tabix = lv_tabix + 1.
      lv_field = 'MSGV' && lv_tabix.
      ASSIGN COMPONENT lv_field OF STRUCTURE is_message_bdc TO FIELD-SYMBOL(<fs>).
      IF sy-subrc = 0.
        CONCATENATE lw_text <fs> INTO lv_message.
      ENDIF.
      IF message IS INITIAL.
        message = lv_message.
      ELSE.
        CONCATENATE message lv_message INTO message.
      ENDIF.
    ENDLOOP.

  ELSEIF is_message_bapi IS NOT INITIAL.

    SELECT SINGLE * FROM t100 INTO @lw_t100 WHERE arbgb = @is_message_bapi-id AND msgnr = @is_message_bapi-NUMBER AND sprsl = '1'.
    SPLIT lw_t100-text AT '&' INTO TABLE lt_text.
    CLEAR lv_tabix.
    LOOP AT lt_text INTO lw_text.
      lv_tabix = lv_tabix + 1.
      lv_field = 'MESSAGE_V' && lv_tabix.
      ASSIGN COMPONENT lv_field OF STRUCTURE is_message_bapi TO <fs>.
      IF sy-subrc = 0.
        CONCATENATE lw_text <fs> INTO lv_message.
      ENDIF.
      IF message IS INITIAL.
        message = lv_message.
      ELSE.
        CONCATENATE message lv_message INTO message.
      ENDIF.
    ENDLOOP.

  ENDIF.

ENDFUNCTION.

如果是BDC的消息,就用【IS_MESSAGE_BDC】入参。
如果是BAPI的消息,就用【IS_MESSAGE_BAPI】入参。

作者:小飞猪猪猪猪猪猪猪–CSDN

你可能感兴趣的:(常用自建函数,sap,abap)