상세 컨텐츠

본문 제목

ABAP - 다른 SAP끼리 데이터 송수신하는 RFC 펑션 프로그램

ABAP

by Kiroc 2023. 8. 23. 16:10

본문

이러한 목적으로 만들어진 프로그램!

회사A와 회사B 간 데이터 주고받는 RFC INF 프로그램 생성했다

회사A의 DATA를 회사B로 넘겨주는 프로그램으로 아래 RFC호출 프로그램은 회사B에 생성했다.

REPORT zinfprog MESSAGE-ID zm. "회사B의 SAP에 생성

INCLUDE zinfprog_top.
INCLUDE zinfprog_sel.
INCLUDE zinfprog_o01.
INCLUDE zinfprog_i01.
INCLUDE zinfprog_f01.

AT SELECTION-SCREEN.
PERFORM chk_date.

START-OF-SELECTION.
PERFORM get_data.

CASE 'X'.
  WHEN p_disp. "조회모드
    CALL SCREEN '100'.

  WHEN p_export. "inf 모드
     IF p_rfcd IS INITIAL.
       MESSAGE s000 WITH 'RFC Destination 입력해주세요! '.
       EXIT.
     ELSE.
       PERFORM export_data.
     ENDIF.
ENDCASE.
**INCLUDE zinfprog_sel.

TABLES : zt0010.

SELECTION-SCREEN BEGIN OF BLOCK b01 WITH FRAME TITLE text-001.
SELECT-OPTIONS : s_budat FOR zt0010-budat.
PARAMETERS : p_rfcd TYPE rfcdest DEFAULT 'BC문의100'.
SELECTION-SCREEN END OF BLOCK b01.

PARAMETERS : p_export RADIOBUTTON GROUP rad1.
PARAMETERS : p_disp RADIOBUTTON GROUP rad1 DEFAULT 'X'.
** INCLUDE zinfprog_f01.

*&---------------------------------------------------------------------*
*&      FORM  EXPORT_DATA
*&---------------------------------------------------------------------*
FORM export_data .

  DATA : lr_date TYPE RANGE OF budat, lv_date TYPE dats.
  DATA : gt_ret  TYPE TABLE OF bapiret2 WITH HEADER LINE.

  lr_date = VALUE #( ( sign = 'I' option = 'BT' low = s_budat-low high = s_budat-high ) ).

  CALL FUNCTION 'Z_SAP_DATA_IF' DESTINATION p_rfcd "해당 FM은 회사A의 SAP에 생성.
    EXPORTING
      iv_date     = lr_date
    TABLES
      return      = gt_ret
      et_zt0010 = gt_zt0010
      et_zt0020 = gt_zt0020
      et_zt0030 = gt_zt0030
      et_zt0040 = gt_zt0040
      et_zt0110 = gt_zt0110
      et_zt0120 = gt_zt0120
      et_zt0130 = gt_zt0130
      et_zt0140 = gt_zt0140.

  IF gt_ret[ 1 ]-type = 'S'.

    DELETE zt0010 FROM TABLE gt_zt0010.
    IF sy-subrc IS INITIAL.
      COMMIT WORK.
    ELSE.
      ROLLBACK WORK.
    ENDIF.

    DELETE zt0020 FROM TABLE gt_zt0020.
    IF sy-subrc IS INITIAL.
      COMMIT WORK.
    ELSE.
      ROLLBACK WORK.
    ENDIF.

    DELETE zt0030 FROM TABLE gt_zt0030.
    IF sy-subrc IS INITIAL.
      COMMIT WORK.
    ELSE.
      ROLLBACK WORK.
    ENDIF.

    DELETE zt0040 FROM TABLE gt_zt0040.
    IF sy-subrc IS INITIAL.
      COMMIT WORK.
    ELSE.
      ROLLBACK WORK.
    ENDIF.

    DELETE zt0110 FROM TABLE gt_zt0110.
    IF sy-subrc IS INITIAL.
      COMMIT WORK.
    ELSE.
      ROLLBACK WORK.
    ENDIF.

    DELETE zt0120 FROM TABLE gt_zt0120.
    IF sy-subrc IS INITIAL.
      COMMIT WORK.
    ELSE.
      ROLLBACK WORK.
    ENDIF.

    DELETE zt0130 FROM TABLE gt_zt0130.
    IF sy-subrc IS INITIAL.
      COMMIT WORK.
    ELSE.
      ROLLBACK WORK.
    ENDIF.

    DELETE zt0140 FROM TABLE gt_zt0140.
    IF sy-subrc IS INITIAL.
      COMMIT WORK.
    ELSE.
      ROLLBACK WORK.
    ENDIF.

  ELSE.
    MESSAGE s001 WITH '회사B로 INSERT 실패입니다. '.
    EXIT.
  ENDIF.

ENDFORM.

회사A의 RFC FM은 아래 처럼 생성했음.

FUNCTION Z_SAP_DATA_IF. "해당 FM은 회사A의 SAP에 생성.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     VALUE(IS_DATE) LIKE  ZT0010-BUDAT OPTIONAL
*"  TABLES
*"      ET_ZT0010 STRUCTURE  ZT0010
*"      RETURN STRUCTURE  BAPIRET2
*"      ET_ZT0020 STRUCTURE  ZT0020
*"      ET_ZT0030 STRUCTURE  ZT0030
*"      ET_ZT0040 STRUCTURE  ZT0040
*"      ET_ZT0110 STRUCTURE  ZT0110
*"      ET_ZT0120 STRUCTURE  ZT0120
*"      ET_ZT0130 STRUCTURE  ZT0130
*"      ET_ZT0140 STRUCTURE  ZT0140
*"----------------------------------------------------------------------
* 회사B에서 2023.0X월 내역이 있으면 회사A로 보내고 회사B 내역은 삭제하는 RFC

  DATA : lt_zt0010 TYPE TABLE OF zt0010,
         lt_zt0020 TYPE TABLE OF zt0020,
         lt_zt0030 TYPE TABLE OF zt0030,
         lt_zt0040 TYPE TABLE OF zt0040,

         lt_zt0110 TYPE TABLE OF zt0110,
         lt_zt0120 TYPE TABLE OF zt0120,
         lt_zt0130 TYPE TABLE OF zt0130,
         lt_zt0140 TYPE TABLE OF zt0140.

  DATA : lv_return TYPE string,
         lv_s      TYPE string,
         lv_f      TYPE string.
  DATA : ir_date TYPE RANGE OF zt0010-budat.

* TABLE INF
  IF et_zt0010[] IS NOT INITIAL
     OR et_zt0020[] IS NOT INITIAL
     OR et_zt0030[] IS NOT INITIAL
     OR et_zt0040[] IS NOT INITIAL

     OR et_zt0110[] IS NOT INITIAL
     OR et_zt0120[] IS NOT INITIAL
     OR et_zt0130[] IS NOT INITIAL
     OR et_zt0140[] IS NOT INITIAL.

    CLEAR : lv_s, lv_f, lv_return.

    IF et_zt0010[] IS NOT INITIAL.
      INSERT zt0010 FROM TABLE et_zt0010 ACCEPTING DUPLICATE KEYS.
                                                                        "키값 중복 시 덤프 방지. SY-SUBRC = 4로 INSERT는 안됨
      IF sy-subrc = 0.
        COMMIT WORK.
        lv_s = 'ZT0010'.
      ELSE.
        ROLLBACK WORK.
        lv_f = 'ZT0010'.
      ENDIF.
    ENDIF.

    IF et_zt0020[] IS NOT INITIAL.
      INSERT zt0020 FROM TABLE et_zt0020 ACCEPTING DUPLICATE KEYS.
      IF sy-subrc = 0.
        COMMIT WORK.
        lv_s = lv_s && ' ZT0020'.
      ELSE.
        ROLLBACK WORK.
        lv_f = lv_f && ' ZT0020'.
      ENDIF.
    ENDIF.

    IF et_zt0030[] IS NOT INITIAL.
      INSERT zt0030 FROM TABLE et_zt0030 ACCEPTING DUPLICATE KEYS.
      IF sy-subrc = 0.
        COMMIT WORK.
        lv_s = lv_s && ' ZT0030'.
      ELSE.
        ROLLBACK WORK.
        lv_f = lv_f && ' ZT0030'.
      ENDIF.
    ENDIF.

    IF et_zt0040[] IS NOT INITIAL.
      INSERT zt0040 FROM TABLE et_zt0040 ACCEPTING DUPLICATE KEYS.
      IF sy-subrc = 0.
        COMMIT WORK.
        lv_s = lv_s && ' ZT0040'.
      ELSE.
        ROLLBACK WORK.
        lv_f = lv_f && ' ZT0040'.
      ENDIF.
    ENDIF.

    IF et_zt0110[] IS NOT INITIAL.
      INSERT zt0110 FROM TABLE et_zt0110 ACCEPTING DUPLICATE KEYS.
      IF sy-subrc = 0.
        COMMIT WORK.
        lv_s = lv_s && ' ZT0110'.
      ELSE.
        ROLLBACK WORK.
        lv_f = lv_f && ' ZT0110'.
      ENDIF.
    ENDIF.

    IF et_zt0120[] IS NOT INITIAL.
      INSERT zt0120 FROM TABLE et_zt0120 ACCEPTING DUPLICATE KEYS.
      IF sy-subrc = 0.
        COMMIT WORK.
        lv_s = lv_s && ' ZT0120'.
      ELSE.
        ROLLBACK WORK.
        lv_f = lv_f && ' ZT0120'.
      ENDIF.
    ENDIF.

    IF et_zt0130[] IS NOT INITIAL.
      INSERT zt0130 FROM TABLE et_zt0130 ACCEPTING DUPLICATE KEYS.
      IF sy-subrc = 0.
        COMMIT WORK.
        lv_s = lv_s && ' ZT0130'.
      ELSE.
        ROLLBACK WORK.
        lv_f = lv_f && ' ZT0130'.
      ENDIF.
    ENDIF.

    IF et_zt0140[] IS NOT INITIAL.
      INSERT zt0140 FROM TABLE et_zt0140 ACCEPTING DUPLICATE KEYS.
      IF sy-subrc = 0.
        COMMIT WORK.
        lv_s = lv_s && ' ZT0140'.
      ELSE.
        ROLLBACK WORK.
        lv_f = lv_f && ' ZT0140'.
      ENDIF.
    ENDIF.

    "RESULT
    return-type    = 'S'.
    lv_s           = '성공:' && lv_s.
    lv_f           = '실패:' && lv_f.
    lv_return      = lv_s && '/' && lv_f.
    return-message = lv_return.
    APPEND return.

  ELSE.
    return-type     = 'E'.
    return-message  = 'NO DATA'.
    APPEND return.
  ENDIF.

ENDFUNCTION.

 

'ABAP' 카테고리의 다른 글

CLASS TEST #2 : Scrabble Score  (0) 2024.01.12
CLASS TEST #1 : Hello, World!  (0) 2024.01.09
ABAP - BDC 실습(자재 마스터 변경)  (0) 2023.03.17
SAP ABAP - BATCH JOB 삭제하기  (0) 2023.03.10
ABAP - 이벤트 정리 및 예시  (0) 2023.02.22

관련글 더보기