Friday, March 22, 2013

BDC Example

**********************************************************************
*                         TRITH AGRO                                 *
**********************************************************************
* Object Name:     ZFI_BDCX_IA05                                     *
* Description:     Task List Creation using BDC                      *
* Tech. Consultant:  Binay Biswakarma                                *
* Func. Consultant:  Anish Ahya                                      *
* Date : 08.09.2011                                                  *
* Initial TR number: DEVK900584                                      *
**********************************************************************
* Change TR number          Change by                 Change date    *
*                                                                    *
*                                                                    *
**********************************************************************

REPORT  zfi_bdcx_ia05 MESSAGE-ID 8i.

*---Header File Structure
DATABEGIN OF ihead OCCURS 0,
      comno TYPE i,             "Reference / Common Key for Header File & Item File (Internal Use)
      plnnr LIKE plpo-plnnr,    "Group No
      ktext LIKE plkod-ktext,   "Task list description
      sttag LIKE rc271-sttag,   "Key Date
      arbpl LIKE rcr01-arbpl,   "Work Center
      werks LIKE t001w-werks,   "Plant
      verwe LIKE plkod-verwe,   "Usage
      statu LIKE plkod-statu,   "Status
      strat LIKE plkod-strat,   "Management Strategy
      vfrdt LIKE rc271-sttag,   "Valid from Date
      anlzu  LIKE plkod-anlzu,    "  Syst.Condition
      END OF ihead.

*---Item File Structure
DATABEGIN OF iitem OCCURS 0,
      comno TYPE i,             "Reference / Common Key for Header File & Item File (Internal Use)
      ltxa1(100TYPE  c,       "Opration Description (Maximum 100 Character)
      arbei LIKE plpod-arbei,   "Amount of work involved in performing the activity.
      arbeh LIKE plpod-arbeh,   "Unit for work.
      anzzl LIKE plpod-anzzl,   "Number of capacities required
      dauno LIKE plpod-dauno,   "Normal duration of the activity
      daune LIKE plpod-daune,   "Normal duration/unit
      kzyk1 LIKE riewp-kzyk1,   "Short text for the maintenance cycle (time/performance)
      larnt LIKE plpod-larnt,   "Activity Type
      END OF iitem.

DATA ifile LIKE iitem OCCURS WITH HEADER LINE.

DATABEGIN OF imess OCCURS 0,
      messa(100TYPE c,
      END OF imess.

*---BDC Structure
DATA bdcdata LIKE bdcdata    OCCURS WITH HEADER LINE,
       msg_tab LIKE bdcmsgcoll OCCURS WITH HEADER LINE,
       gv_flag" External group indicator

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERp_mode TYPE ctu_mode AS LISTBOX VISIBLE LENGTH 30 OBLIGATORY DEFAULT 'N'.  "BDC Upload Mode
SELECTION-SCREEN END OF BLOCK b1.

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.
PARAMETERp_head LIKE rlgrap-filename OBLIGATORY.     "Group Header File Name
SELECTION-SCREEN SKIP 1.
PARAMETERp_item LIKE rlgrap-filename OBLIGATORY.     "Group Item   File Name
SELECTION-SCREEN END OF BLOCK b2.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_head.
  PERFORM file_f4 USING p_head.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_item.
  PERFORM file_f4 USING p_item.

START-OF-SELECTION.
  PERFORM upload_header_data.
  PERFORM upload_item_data.
  PERFORM check_validation.
  LOOP AT ihead.
    PERFORM call_bdc.
  ENDLOOP.

  IF imess[] IS NOT INITIAL.
    PERFORM display_message.
  ENDIF.

*&---------------------------------------------------------------------*
*&      Form  FILE_F4
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM file_f4 USING p_input LIKE rlgrap-filename.

  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      program_name  syst-cprog
      dynpro_number syst-dynnr
      field_name    ' '
    IMPORTING
      file_name     p_input.

ENDFORM.                                                    " FILE_F4

*&---------------------------------------------------------------------*
*&      Form  UPLOAD_HEADER_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM upload_header_data.

  DATA p_file TYPE string.

  MOVE p_head TO p_file.

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                p_file
      filetype                'DAT'
    TABLES
      data_tab                ihead
    EXCEPTIONS
      file_open_error         1
      file_read_error         2
      no_batch                3
      gui_refuse_filetransfer 4
      invalid_type            5
      no_authority            6
      unknown_error           7
      bad_data_format         8
      header_not_allowed      9
      separator_not_allowed   10
      header_too_long         11
      unknown_dp_error        12
      access_denied           13
      dp_out_of_memory        14
      disk_full               15
      dp_timeout              16
      OTHERS                  17.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.                    " UPLOAD_HEADER_DATA
*&---------------------------------------------------------------------*
*&      Form  UPLOAD_ITEM_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM upload_item_data.

  CALL FUNCTION 'WS_UPLOAD'
    EXPORTING
      filename                p_item
      filetype                'DAT'
    TABLES
      data_tab                iitem
    EXCEPTIONS
      conversion_error        1
      file_open_error         2
      file_read_error         3
      invalid_type            4
      no_batch                5
      unknown_error           6
      invalid_table_width     7
      gui_refuse_filetransfer 8
      customer_error          9
      OTHERS                  10.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.                    " UPLOAD_ITEM_DATA
*&---------------------------------------------------------------------*
*&      Form  CHECK_VALIDATION
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM check_validation .

  TABLES plko.

  LOOP AT ihead.

    IF ihead-plnnr IS NOT INITIAL.

      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
        EXPORTING
          input  ihead-plnnr
        IMPORTING
          output ihead-plnnr.

      SELECT SINGLE FROM plko
      WHERE plnty EQ 'A'
        AND plnnr EQ ihead-plnnr.

      IF sy-subrc NE 0.
*        MESSAGE i000 WITH 'Task List Group ' ihead-plnnr ' Does not Exist'.
*        STOP.
        gv_flag 'X'.
      ELSE.
        SELECT SINGLE datuv FROM plko
        INTO ihead-vfrdt
        WHERE plnty EQ 'A'
          AND plnnr EQ ihead-plnnr
          AND datuv LE ihead-sttag.

        IF sy-subrc EQ 0.
          MODIFY ihead TRANSPORTING vfrdt.
        ENDIF.

      ENDIF.

    ELSEIF ihead-ktext IS INITIAL.
      MESSAGE e000 WITH 'Please enter Task list description'.
    ELSEIF ihead-sttag IS INITIAL.
      MESSAGE e000 WITH 'Please enter Key Date'.
    ELSEIF ihead-arbpl IS INITIAL.
      MESSAGE e000 WITH 'Please enter Work Center'.
    ELSEIF ihead-werks IS INITIAL.
      MESSAGE e000 WITH 'Please enter Plant'.
    ELSEIF ihead-verwe IS INITIAL.
      MESSAGE e000 WITH 'Please enter Usage'.
    ELSEIF ihead-statu IS INITIAL.
      MESSAGE e000 WITH 'Please enter Status'.
    ELSEIF ihead-strat IS INITIAL.
      MESSAGE e000 WITH 'Please enter Management Strategy'.
    ENDIF.

  ENDLOOP.

ENDFORM.                    " CHECK_VALIDATION
*&---------------------------------------------------------------------*
*&      Form  CALL_BDC
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM call_bdc .

  DATAp_date(10TYPE c,
        p_anzzl(3TYPE c,
        p_arbei(9TYPE c,
        p_dauno(7TYPE c,
        ltxa2(40)  TYPE c,
        ltxa3(20)  TYPE c.

  CLEAR  bdcdata,msg_tab,ifile.
  REFRESHbdcdata,msg_tab,ifile.

  LOOP AT iitem WHERE comno EQ ihead-comno.
    MOVE-CORRESPONDING iitem TO ifile.
    APPEND ifile.
  ENDLOOP.

  WRITE ihead-sttag TO p_date.

  PERFORM bdc_dynpro USING 'SAPLCPDI'      '3001'.
  PERFORM bdc_field  USING 'BDC_CURSOR'    'RC271-PLNNR'.
  PERFORM bdc_field  USING 'BDC_OKCODE'    '/00'.
  PERFORM bdc_field  USING 'RC271-PLNNR'   ihead-plnnr.
  PERFORM bdc_field  USING 'RC271-STTAG'   p_date.

  IF ihead-plnnr IS NOT INITIAL AND ihead-vfrdt IS NOT INITIAL AND gv_flag IS INITIAL.
    PERFORM bdc_dynpro USING 'SAPLCPDI'    '3200'.
    PERFORM bdc_field  USING 'BDC_CURSOR'  'RC270-HEAD2'.
    PERFORM bdc_field  USING 'BDC_OKCODE'  '=ANLG'.
  ENDIF.

  PERFORM bdc_dynpro USING 'SAPLCPDA'      '3010'.
  PERFORM bdc_field  USING 'BDC_CURSOR'    'PLKOD-STRAT'.
  PERFORM bdc_field  USING 'BDC_OKCODE'    '=VOUE'.
  PERFORM bdc_field  USING 'PLKOD-KTEXT'   ihead-ktext.
  PERFORM bdc_field  USING 'PLKOD-WERKS'   ihead-werks.
  PERFORM bdc_field  USING 'RCR01-ARBPL'   ihead-arbpl.
  PERFORM bdc_field  USING 'PLKOD-VERWE'   ihead-verwe.
  PERFORM bdc_field  USING 'PLKOD-STATU'   ihead-statu.
  PERFORM bdc_field  USING 'PLKOD-STRAT'   ihead-strat.
*  Field Addition
  PERFORM bdc_field  USING 'PLKOD-ANLZU'   ihead-anlzu.
* End Addition

  READ TABLE ifile INDEX 1.

  CLEARp_dauno,ltxa2,ltxa3,p_anzzl,p_arbei.

  WRITE ifile-anzzl TO p_anzzl LEFT-JUSTIFIED.
  WRITE ifile-arbei TO p_arbei LEFT-JUSTIFIED.
  WRITE ifile-dauno TO p_dauno LEFT-JUSTIFIED.

  PERFORM bdc_dynpro USING 'SAPLCPDI'    '3400'.
  PERFORM bdc_field  USING 'BDC_CURSOR'  'PLPOD-LTXA1(01)'.
  PERFORM bdc_field  USING 'BDC_OKCODE'  '=PICK'.

  MOVE ifile-ltxa1+40(40TO ltxa2.
  MOVE ifile-ltxa1+80(20TO ltxa3.

  PERFORM bdc_field  USING 'PLPOD-LTXA1(01)' ifile-ltxa1(40).
  PERFORM bdc_field  USING 'PLPOD-ARBEI(01)' p_arbei.
  PERFORM bdc_field  USING 'PLPOD-ARBEH(01)' ifile-arbeh.
  PERFORM bdc_field  USING 'PLPOD-ANZZL(01)' p_anzzl.
  PERFORM bdc_field  USING 'PLPOD-DAUNO(01)' p_dauno.
  PERFORM bdc_field  USING 'PLPOD-DAUNE(01)' ifile-daune.
* Field Addition
  PERFORM bdc_field  USING 'PLPOD-LARNT(01)' ifile-larnt.

  PERFORM bdc_dynpro USING 'SAPLCPDO'        '3370'.
  PERFORM bdc_field  USING 'BDC_OKCODE'      '=LTXT'.

  PERFORM bdc_dynpro USING 'SAPLSTXX'         '1100'.
  PERFORM bdc_field  USING 'BDC_OKCODE'       '=TXBA'.
  PERFORM bdc_field  USING 'RSTXT-TXLINE(03)' ltxa2.
  PERFORM bdc_field  USING 'RSTXT-TXLINE(04)' ltxa3.

  PERFORM bdc_dynpro USING 'SAPLCPDO'        '3370'.
  PERFORM bdc_field  USING 'BDC_OKCODE'      '=BACK'.

  PERFORM bdc_dynpro USING 'SAPLCPDI'        '3400'.
  PERFORM bdc_field  USING 'BDC_CURSOR'      'PLPOD-LTXA1(02)'.
  PERFORM bdc_field  USING 'BDC_OKCODE'      '=P+'.

  LOOP AT ifile.

    IF sy-tabix EQ 1.
      CONTINUE.
    ENDIF.

    CLEARp_dauno,ltxa2,ltxa3,p_anzzl,p_arbei.
    WRITE ifile-anzzl TO p_anzzl LEFT-JUSTIFIED.
    WRITE ifile-arbei TO p_arbei LEFT-JUSTIFIED.
    WRITE ifile-dauno TO p_dauno LEFT-JUSTIFIED.

    PERFORM bdc_dynpro USING 'SAPLCPDI'        '3400'.
    PERFORM bdc_field  USING 'BDC_CURSOR'      'PLPOD-LTXA1(02)'.
    PERFORM bdc_field  USING 'BDC_OKCODE'      '=PICK'.

    MOVE ifile-ltxa1+40(40TO ltxa2.
    MOVE ifile-ltxa1+80(20TO ltxa3.

    PERFORM bdc_field  USING 'PLPOD-LTXA1(02)' ifile-ltxa1(40).
    PERFORM bdc_field  USING 'PLPOD-ARBEI(02)' p_arbei.
    PERFORM bdc_field  USING 'PLPOD-ARBEH(02)' ifile-arbeh.
    PERFORM bdc_field  USING 'PLPOD-ANZZL(02)' p_anzzl.
    PERFORM bdc_field  USING 'PLPOD-DAUNO(02)' p_dauno.
    PERFORM bdc_field  USING 'PLPOD-DAUNE(02)' ifile-daune.
*   field Addition
    PERFORM bdc_field  USING 'PLPOD-LARNT(01)' ifile-larnt.

    PERFORM bdc_dynpro USING 'SAPLCPDO'        '3370'.
    PERFORM bdc_field  USING 'BDC_OKCODE'      '=LTXT'.

    PERFORM bdc_dynpro USING 'SAPLSTXX'        '1100'.
    PERFORM bdc_field  USING 'BDC_OKCODE'      '=TXBA'.
    PERFORM bdc_field  USING 'RSTXT-TXLINE(03)' ltxa2.
    PERFORM bdc_field  USING 'RSTXT-TXLINE(04)' ltxa3.

    PERFORM bdc_dynpro USING 'SAPLCPDO'      '3370'.
    PERFORM bdc_field  USING 'BDC_OKCODE'    '=BACK'.

    PERFORM bdc_dynpro USING 'SAPLCPDI'        '3400'.
    PERFORM bdc_field  USING 'BDC_CURSOR'      'PLPOD-LTXA1(02)'.
    PERFORM bdc_field  USING 'BDC_OKCODE'      '=P+'.

  ENDLOOP.

  PERFORM bdc_dynpro USING 'SAPLCPDI'      '3400'.
  PERFORM bdc_field  USING 'BDC_CURSOR'    'PLPOD-LTXA1(03)'.
  PERFORM bdc_field  USING 'BDC_OKCODE'    '=MAAL'.

  PERFORM bdc_dynpro USING 'SAPLCPDI'      '3400'.
  PERFORM bdc_field  USING 'BDC_CURSOR'    'PLPOD-LTXA1(03)'.
  PERFORM bdc_field  USING 'BDC_OKCODE'    '=WPLT'.

  PERFORM bdc_dynpro USING 'SAPLCPDI'      '3600'.
  PERFORM bdc_field  USING 'BDC_CURSOR'    'RC270-HEAD2'.
  PERFORM bdc_field  USING 'BDC_OKCODE'    '=IWPZ'.

  LOOP AT ifile.
    PERFORM bdc_dynpro USING 'SAPLCIDI'      '3000'.
    PERFORM bdc_field  USING 'BDC_CURSOR'    'RIEWP-KZYK1(01)'.
    PERFORM bdc_field  USING 'BDC_OKCODE'    '=NEXT'.
    PERFORM bdc_field  USING 'RIEWP-KZYK1(01)' ifile-kzyk1.
  ENDLOOP.

  PERFORM bdc_dynpro USING 'SAPLCIDI'      '3000'.
  PERFORM bdc_field  USING 'BDC_CURSOR'    'RIEWP-KZYK1(01)'.
  PERFORM bdc_field  USING 'BDC_OKCODE'    '=BU'.

  CALL TRANSACTION 'IA05'
       USING bdcdata
       UPDATE 'A'
       MODE   p_mode
       MESSAGES INTO msg_tab.

  IF p_mode NE 'A'.
    WAIT UP TO SECONDS.
  ENDIF.

  READ TABLE msg_tab WITH KEY msgtyp 'E'.
  IF sy-subrc EQ 0.
    PERFORM display_messages.
  ELSE.
    IF ihead-plnnr IS NOT INITIAL.
      CLEAR imess.
      CONCATENATE 'New Conunter is Created for Group ' ihead-plnnr INTO imess-messa SEPARATED BY space.
      APPEND imess.
    ELSE.
      READ TABLE msg_tab WITH KEY msgtyp 'S'
                                  msgid  'CI'
                                  msgnr  '317'.
      CLEAR imess.
      CONCATENATE 'New Group ' msg_tab-msgv1 ' Created' INTO imess-messa SEPARATED BY space.
      APPEND imess.
    ENDIF.
  ENDIF.

ENDFORM.                    " CALL_BDC

*&---------------------------------------------------------------------*
*&      Form  BDC_DYNPRO
*&---------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.

  CLEAR bdcdata.
  bdcdata-program  program.
  bdcdata-dynpro   dynpro.
  bdcdata-dynbegin 'X'.
  APPEND bdcdata.

ENDFORM.                    " BDC_DYNPRO

*&---------------------------------------------------------------------*
*&      Form  BDC_FIELD
*&---------------------------------------------------------------------*
FORM bdc_field USING fnam fval.

  CLEAR bdcdata.
  bdcdata-fnam fnam.
  bdcdata-fval fval.
  APPEND bdcdata.

ENDFORM.                    " BDC_FIELD

*&---------------------------------------------------------------------*
*&      Form  display_messages
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM display_messages .

  DATA msg_str(80).

  LOOP AT msg_tab.

    AT FIRST.
      LEAVE TO LIST-PROCESSING AND RETURN TO SCREEN 0.
      NEW-PAGE LINE-SIZE 100.
    ENDAT.

    PERFORM get_message_text CHANGING msg_str.

    CASE msg_tab-msgtyp.
      WHEN 'E' OR 'A'"Error or Abandend
        FORMAT COLOR 6"Red
      WHEN 'W'.  "Warning
        FORMAT  COLOR 3"Yellow
      WHEN 'I'.  "Information
        FORMAT COLOR 1"Blue
      WHEN 'S'.  "Information
        FORMAT COLOR 5"Green
    ENDCASE.
    WRITE/(5msg_tab-msgid ,msg_tab-msgnrmsg_str.

  ENDLOOP.

  CLEARimess,msg_tabREFRESHimess,msg_tab.

ENDFORM.                    " display_messages
*&---------------------------------------------------------------------*
*&      Form  GET_MESSAGE_TEXT
*&---------------------------------------------------------------------*
FORM get_message_text CHANGING p_msg_str.

  CALL FUNCTION 'MESSAGE_TEXT_BUILD'
    EXPORTING
      msgid               msg_tab-msgid
      msgnr               msg_tab-msgnr
      msgv1               msg_tab-msgv1
      msgv2               msg_tab-msgv2
      msgv3               msg_tab-msgv3
      msgv4               msg_tab-msgv4
    IMPORTING
      message_text_output p_msg_str.

ENDFORM.                    " GET_MESSAGE_TEXT
*&---------------------------------------------------------------------*
*&      Form  DISPLAY_MESSAGE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM display_message .

  LOOP AT imess.

    AT FIRST.
      LEAVE TO LIST-PROCESSING AND RETURN TO SCREEN 0.
      NEW-PAGE LINE-SIZE 100.
      FORMAT COLOR 5.
    ENDAT.

    WRITE:/ imess-messa.

  ENDLOOP.

ENDFORM.                    " DISPLAY_MESSAGE

*Text elements
*----------------------------------------------------------
* 001 BDC Processing Mode
* 002 File Name


*Selection texts
*----------------------------------------------------------
* P_HEAD         Group Header File Name
* P_ITEM         Group Item File Name
* P_MODE         BDC Processing Mode


*Messages
*----------------------------------------------------------
*
* Message class: ZW
*000   & & & & & &

1 comment: