상세 컨텐츠

본문 제목

ABAP - BDC 실습(자재 마스터 변경)

ABAP

by Kiroc 2023. 3. 17. 16:12

본문

 

1. T-CD : SHDB에서 자재 변경 과정을 레코딩

위의 내용은 자재를 MM02에서 수정한 레코드인데,

내부생산기간을

MARC-DZEIT=12로 변경했다.

 

 

2. PAI에 BDC가 구동되는 버튼을 세팅한다.

 

* PAI 파트

*&---------------------------------------------------------------------*
*&      MODULE  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       TEXT
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.  
CASE OK_CODE.
    
  WHEN 'FC_ZERO'.      
    PERFORM DZEIT_ZERO.
  ENDCASE.
  
CLEAR : OK_CODE.
ENDMODULE.

 

* BDC 버튼 내부

*&---------------------------------------------------------------------*
*&      FORM  DZEIT_ZERO
*&---------------------------------------------------------------------*
*       TEXT
*----------------------------------------------------------------------*
FORM DZEIT_ZERO.
  
DATA : P_CONFIRM,
             LV_ERROR TYPE STRING.
  
DATA : LT_MESSAGE TYPE BAPIRET2_TAB,
             LV_MESSAGE TYPE STRING.
  
DATA : LV_S TYPE I,
              LV_F TYPE I.
  
CLEAR : LV_ERROR.
  
CLEAR : P_CONFIRM.
  
CALL FUNCTION 'POPUP_TO_CONFIRM'    
EXPORTING     
TITLEBAR                   = 'CAUTION'
      TEXT_QUESTION      = '일괄 처리하시겠습니까?'
     TEXT_BUTTON_1       = '네'(001)
     ICON_BUTTON_1        = 'ICON_CHECKED'
     TEXT_BUTTON_2               = '아니요'(002)
     ICON_BUTTON_2               = 'ICON_INCOMPLETE'
     DEFAULT_BUTTON              = '2'
     DISPLAY_CANCEL_BUTTON       = ''   
IMPORTING
     ANSWER                      = P_CONFIRM "1:CONTINEW / 2:CANCEL          
.
  
IF SY-SUBRC <> 0.  
  MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO            
  WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.  
ENDIF.
  
IF P_CONFIRM <> '1'.    
MESSAGE S170 DISPLAY LIKE 'E' WITH TEXT-E01. "취소했습니다  
ELSE.    
LOOP AT GT_0100_DATA ASSIGNING FIELD-SYMBOL(<FS_0100_DATA>).
      
"BDC 실습 시작      
CALL FUNCTION 'ZFM_CHANGE_PROD_PERIOD'        
EXPORTING
          IS_MM_DATA  = <FS_0100_DATA>        
TABLES
          ET_MSG         = LT_MESSAGE[]        
EXCEPTIONS
          ERROR_MESSAGE = 1.
      
IF SY-SUBRC EQ 1.
        
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO                            
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4                           
INTO LV_MESSAGE.
        
APPEND INITIAL LINE TO LT_MESSAGE[]                               
ASSIGNING FIELD-SYMBOL(<FS_MESSAGE>).
        <FS_MESSAGE>-TYPE     = 'E'.
        <FS_MESSAGE>-ID       = SY-MSGID.
        <FS_MESSAGE>-NUMBER   = SY-MSGNO.
        <FS_MESSAGE>-MESSAGE  = LV_MESSAGE.      
ENDIF.
      <FS_0100_DATA>-BAPI_MSG[] = LT_MESSAGE[].      
IF <FS_0100_DATA>-BAPI_MSG[] IS NOT INITIAL.
        <FS_0100_DATA>-STATUS  = ZCMCL_ICON=>GC_ERROR.
        <FS_0100_DATA>-MESSAGE = <FS_0100_DATA>-BAPI_MSG[ 1 ]-MESSAGE.        
ADD 1 TO LV_F. "실패 라인수      
ELSE.
        <FS_0100_DATA>-STATUS    = ZCMCL_ICON=>GC_LED_GREEN.        
ADD 1 TO LV_S. "성공 라인수      
ENDIF.    
ENDLOOP.
    
MESSAGE S332 WITH LV_S LV_F. "성공 : & | 실패 : &
      "BDC 실습 끝

*   GO_0100_ALV->SET_OPTIMIZE_WIDTH( ).
*   GO_0100_ALV->REFRESH( ).  
ENDIF.
ENDFORM.

 

3. 콜펑션 내부

3-1. 임포트 변수 선언

 

3-2. 테이블 선언

 

3-3. 내부 소스코드

FUNCTION ZFM_CHANGE_PROD_PERIOD.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     REFERENCE(IS_MM_DATA) TYPE  클래스=>TY_S_SCREEN_DATA
*"  TABLES
*"      ET_MSG TYPE  BAPIRET2_TAB
*"----------------------------------------------------------------------
  ET_MSG[] =  Z클래스=>CHANGE_MATERIAL_DZEIT( IS_MM_DATA ).
ENDFUNCTION.

 

3-4. 메소드 내부 코드

메소드 설명
메소드 매개변수

 

 METHOD CHANGE_MATERIAL_DZEIT.
    
"PARAMETERS    
DATA : LT_BDCDATA TYPE TABLE OF BDCDATA.
    
FIELD-SYMBOLS : <FS_BDCDATA> LIKE LINE OF LT_BDCDATA[].
    
"자재번호    
APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-PROGRAM    = 'SAPLMGMM'.
    <FS_BDCDATA>-DYNPRO     = '0060'.
    <FS_BDCDATA>-DYNBEGIN   = 'X'.

    APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-FNAM       = 'BDC_CURSOR'.
    <FS_BDCDATA>-FVAL       = 'RMMG1-MATNR'.
    
APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-FNAM       = 'BDC_OKCODE'.
    <FS_BDCDATA>-FVAL       = '=ENTR'.

    APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-FNAM       = 'RMMG1-MATNR'.
    <FS_BDCDATA>-FVAL       = IS_MM_DATA-MATNR.    
CONDENSE <FS_BDCDATA>-FVAL.
    
"전체뷰 선택
    APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-PROGRAM    = 'SAPLMGMM'.
    <FS_BDCDATA>-DYNPRO     = '0070'.
    <FS_BDCDATA>-DYNBEGIN   = 'X'.

    APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-FNAM       = 'BDC_CURSOR'.
    <FS_BDCDATA>-FVAL       = 'MSICHTAUSW-DYTXT(01)'.
    APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-FNAM       = 'BDC_OKCODE'.
    <FS_BDCDATA>-FVAL       = '=SELA'.

" 전체뷰 선택 후 확인
    APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-PROGRAM    = 'SAPLMGMM'.
    <FS_BDCDATA>-DYNPRO     = '0070'.
    <FS_BDCDATA>-DYNBEGIN   = 'X'.
    
APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-FNAM       = 'BDC_CURSOR'.
    <FS_BDCDATA>-FVAL       = 'MSICHTAUSW-DYTXT(01)'.
    
APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-FNAM       = 'BDC_OKCODE'.
    <FS_BDCDATA>-FVAL       = '=ENTR'.
    
"플랜트 선택    
APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-PROGRAM    = 'SAPLMGMM'.
    <FS_BDCDATA>-DYNPRO     = '0080'.
    <FS_BDCDATA>-DYNBEGIN   = 'X'.
    
APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-FNAM       = 'BDC_CURSOR'.
    <FS_BDCDATA>-FVAL       = 'RMMG1-WERKS'.

    APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-FNAM       = 'BDC_OKCODE'.
    <FS_BDCDATA>-FVAL       = '=ENTR'.
    
APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-FNAM       = 'RMMG1-WERKS'.
    <FS_BDCDATA>-FVAL       = IS_MM_DATA-PLWRK.
   CONDENSE <FS_BDCDATA>-FVAL.
    
"MRP2 뷰 선택    
APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-PROGRAM    = 'SAPLMGMM'.
    <FS_BDCDATA>-DYNPRO     = '4004'.
    <FS_BDCDATA>-DYNBEGIN   = 'X'.
    
APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-FNAM       = 'BDC_OKCODE'.
    <FS_BDCDATA>-FVAL       = '=SP13'.
    
APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-PROGRAM    = 'SAPLMGMM'.
    <FS_BDCDATA>-DYNPRO     = '4000'.
    <FS_BDCDATA>-DYNBEGIN   = 'X'.

    APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-FNAM       = 'BDC_OKCODE'.
    <FS_BDCDATA>-FVAL       = '/00'.
    
APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-FNAM       = 'BDC_CURSOR'.
    <FS_BDCDATA>-FVAL       = 'MARC-DZEIT'.
    
APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-FNAM       = 'MARC-DZEIT'.
    <FS_BDCDATA>-FVAL       = 12.    "원하는 값 넣기. 예시는 12일.
CONDENSE <FS_BDCDATA>-FVAL.
    
APPEND INITIAL LINE TO LT_BDCDATA[] ASSIGNING <FS_BDCDATA>.
    <FS_BDCDATA>-FNAM       = 'BDC_OKCODE'.
    <FS_BDCDATA>-FVAL       = '=BU'.

    
" 호출    
DATA :      LS_OPTION  TYPE CTU_PARAMS,
      LT_MESSAGE TYPE TAB_BDCMSGCOLL.

    LS_OPTION-DISMODE  = 'N'. 
    LS_OPTION-UPDMODE  = 'S'.

    CALL TRANSACTION 'MM02' USING LT_BDCDATA[]         
    MESSAGES INTO LT_MESSAGE[]          OPTIONS FROM LS_OPTION.
    
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'      
EXPORTING        
WAIT = 'X'.
    
DELETE LT_MESSAGE[] WHERE MSGTYP NE 'E'.
    
"메시지 처리
    RT_MESSAGE[] = ZCMCL_UTIL=>CONVERT_MESSAGE( LT_MESSAGE[] ).
  
ENDMETHOD.

 

3-5. 그 외

3-5-1. ZCMCL_UTIL=>CONVERT_MESSAGE

  METHOD convert_message.
    CHECK it_message[] IS NOT INITIAL.
    LOOP AT it_message[] ASSIGNING FIELD-SYMBOL(<fs_message>).
      APPEND INITIAL LINE TO rt_data[] ASSIGNING FIELD-SYMBOL(<fs_data>).
      MOVE <fs_message>-msgtyp  TO <fs_data>-type.
      MOVE <fs_message>-msgid   TO <fs_data>-id.
      MOVE <fs_message>-msgnr   TO <fs_data>-number.
      MOVE <fs_message>-msgspra TO <fs_data>-parameter.
      MOVE <fs_message>-msgv1   TO <fs_data>-message_v1.
      MOVE <fs_message>-msgv2   TO <fs_data>-message_v2.
      MOVE <fs_message>-msgv3   TO <fs_data>-message_v3.
      MOVE <fs_message>-msgv4   TO <fs_data>-message_v4.

      CALL FUNCTION 'FORMAT_MESSAGE'
        EXPORTING
          id        <fs_data>-id
          no        <fs_data>-number
          v1        <fs_data>-message_v1
          v2        <fs_data>-message_v2
          v3        <fs_data>-message_v3
          v4        <fs_data>-message_v4
        IMPORTING
          msg       <fs_data>-message
        EXCEPTIONS
          not_found 1
          OTHERS    2.
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDIF.
    ENDLOOP.
  ENDMETHOD.

 

 

3-5-2. ZCMCL_ICON=>GC_LED_GREEN.

 

4. BDC 파라미터 정리

* BDC 처리모드(DISMODE)

- A : 모든 화면 조회

- E : 오류 조회

- N : 백그라운드 처리

- P : 백그라운드 처리. 디버깅 가능.

 

* BDC 트랜잭션 모드(UPDMODE)

- L : 로컬

- S : 동기

- A : 비동기

 

프로그램을 생성 후 디버깅하면서 오류를 확인할 때는

    LS_OPTION-DISMODE  = 'A' 혹은 'E'
    LS_OPTION-UPDMODE  = 'L' 혹은 'A'.(S해도 무방하긴 함.)

위와 같이 처리.

 

우선 DISMODE를 A로 해서 모든 BDC단계를 눈으로 확인 후

에러가 잔존할 시 E로 처리해 에러가 없어질 때까지 수정.

 

프로그램을 완성하고 난 후에는 아래와 같이 처리.

    LS_OPTION-DISMODE  = 'N'. "백그라운드 처리 "N->E->A
    LS_OPTION-UPDMODE  = 'S'. "동기 'S'->A->L

관련글 더보기