Handling Table Control in BDC

An example abap program of handling Table Control during bdc programming.
REPORT zmm_bdcp_purchaseorderkb02
       NO STANDARD PAGE HEADING LINE-SIZE 255.

*----------------------------------------------------------------------*
*                 Declaring internal tables                            *
*----------------------------------------------------------------------*

*-----Declaring line structure

DATA : BEGIN OF it_dummy OCCURS 0,

         dummy(255) TYPE c,

       END OF it_dummy.

*-----Internal table for line items


DATA :  BEGIN OF it_idata OCCURS 0,
          ematn(18),      "Material Number.
          menge(13),      "Qyantity.
          netpr(11),      "Net Price.
          werks(4),       "Plant.
          ebelp(5),       "Item Number.
        END OF it_idata.

*-----Deep structure for header data and line items

DATA  :  BEGIN OF it_me21 OCCURS 0,
           lifnr(10),      "Vendor A/c No.
           bsart(4),       "A/c Type.
           bedat(8),       "Date of creation of PO.
           ekorg(4),       "Purchasing Organisation.
           ekgrp(3),       "Purchasing Group.
           x_data LIKE TABLE OF it_idata,
         END OF it_me21.

DATA  :  x_idata LIKE LINE OF it_idata.
DATA  :  v_delimit VALUE ','.
DATA  :  v_indx(3) TYPE n.
DATA  :  v_fnam(30) TYPE c.
DATA  :  v_count TYPE n.
DATA  :  v_ne TYPE i.
DATA  :  v_ns TYPE i.

*include bdcrecx1.

INCLUDE zmm_incl_purchaseorderkb01.

*----------------------------------------------------------------------*
*                 Search help for file                                 *
*----------------------------------------------------------------------*

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      program_name  = syst-cprog
      dynpro_number = syst-dynnr
    IMPORTING
      file_name     = p_file.

START-OF-SELECTION.

*----------------------------------------------------------------------*
*        To upload the data into line structure                        *
*----------------------------------------------------------------------*

  CALL FUNCTION 'WS_UPLOAD'
    EXPORTING
      filename = p_file
      filetype = 'DAT'
    TABLES
      data_tab = it_dummy.

*----------------------------------------------------------------------*
*     Processing the data from line structure to internal tables       *
*----------------------------------------------------------------------*

  REFRESH:it_me21.
  CLEAR  :it_me21.

  LOOP AT it_dummy.
    IF it_dummy-dummy+0(01) = 'H'.
      v_indx = v_indx + 1.
      CLEAR   it_idata.
      REFRESH it_idata.
      CLEAR   it_me21-x_data.
      REFRESH it_me21-x_data.
      SHIFT it_dummy.
      SPLIT it_dummy AT v_delimit INTO it_me21-lifnr
                                       it_me21-bsart
                                       it_me21-bedat
                                       it_me21-ekorg
                                       it_me21-ekgrp.
      APPEND it_me21.
    ELSEIF it_dummy-dummy+0(01) = 'L'.
      SHIFT it_dummy.
      SPLIT it_dummy AT v_delimit INTO it_idata-ematn
                                       it_idata-menge
                                       it_idata-netpr
                                       it_idata-werks
                                       it_idata-ebelp.

      APPEND it_idata TO it_me21-x_data.
      MODIFY it_me21 INDEX v_indx.
    ENDIF.
  ENDLOOP.

*----------------------------------------------------------------------*
*                 To open the group                                    *
*----------------------------------------------------------------------*

  PERFORM open_group.

*----------------------------------------------------------------------*
*         To populate the bdcdata table for header data                *
*----------------------------------------------------------------------*

  LOOP AT it_me21.
    v_count = v_count + 1.
    REFRESH it_bdcdata.
    PERFORM subr_bdc_table USING:   'X' 'SAPMM06E'    '0100',
                                    ' ' 'BDC_CURSOR'  'EKKO-LIFNR',
                                    ' ' 'BDC_OKCODE'  '/00',
                                    ' ' 'EKKO-LIFNR'  it_me21-lifnr,
                                    ' ' 'RM06E-BSART' it_me21-bsart,
                                    ' ' 'RM06E-BEDAT' it_me21-bedat,
                                    ' ' 'EKKO-EKORG'  it_me21-ekorg,
                                    ' ' 'EKKO-EKGRP'  it_me21-ekgrp,
                                    ' ' 'RM06E-LPEIN' 'T'.


    PERFORM subr_bdc_table USING:   'X' 'SAPMM06E'    '0120',
                                    ' ' 'BDC_CURSOR'  'RM06E-EBELP',
                                    ' ' 'BDC_OKCODE'  '/00'.


    MOVE 1 TO v_indx.

*-----To populate the bdcdata table for line item data

    LOOP AT it_me21-x_data INTO x_idata.
      CONCATENATE 'EKPO-EMATN(' v_indx ')'  INTO v_fnam.
      PERFORM  subr_bdc_table USING ' ' v_fnam x_idata-ematn.
      CONCATENATE 'EKPO-MENGE(' v_indx ')'  INTO v_fnam.
      PERFORM  subr_bdc_table USING ' ' v_fnam x_idata-menge.
      CONCATENATE 'EKPO-NETPR(' v_indx ')'  INTO v_fnam.
      PERFORM  subr_bdc_table USING ' ' v_fnam x_idata-netpr.
      CONCATENATE 'EKPO-WERKS(' v_indx ')'  INTO v_fnam.
      PERFORM  subr_bdc_table USING ' ' v_fnam x_idata-werks.
      v_indx = v_indx + 1.

      PERFORM subr_bdc_table USING:  'X' 'SAPMM06E'    '0120',
                                     ' ' 'BDC_CURSOR'  'RM06E-EBELP',
                                     ' ' 'BDC_OKCODE'  '/00'.
    ENDLOOP.

    PERFORM subr_bdc_table USING:    'X' 'SAPMM06E'    '0120',
                                     ' ' 'BDC_CURSOR'  'RM06E-EBELP',
                                     ' ' 'BDC_OKCODE'  '=BU'.

    PERFORM bdc_transaction USING 'ME21'.
  ENDLOOP.
  PERFORM close_group.

*----------------------------------------------------------------------*
*                   End of selection event                             *
*----------------------------------------------------------------------*

END-OF-SELECTION.
  IF session NE 'X'.

*-----To display the successful records
    WRITE :/10  text-001.          "Sucess records
    WRITE :/10  SY-ULINE(20).
    SKIP.
    IF it_sucess IS INITIAL.
      WRITE :/  text-002.
    ELSE.
      WRITE :/   text-008,          "Total number of Succesful records
              35 v_ns.
      SKIP.
      WRITE:/   text-003,          "Vendor Number
             17 text-004,          "Record number
             30 text-005.          "Message
    ENDIF.

    LOOP AT it_sucess.
      WRITE:/4  it_sucess-lifnr,
             17 it_sucess-tabix CENTERED,
             30 it_sucess-sucess_rec.
    ENDLOOP.

    SKIP.

*-----To display the erroneous records

    WRITE:/10   text-006.          "Error Records
    WRITE:/10   SY-ULINE(17).
    SKIP.

    IF it_error IS INITIAL.
      WRITE:/   text-007.          "No error records
    ELSE.
      WRITE:/   text-009,          "Total number of erroneous records
             35 v_ne.
      SKIP.
      WRITE:/   text-003,          "Vendor Number
             17 text-004,          "Record number
             30 text-005.          "Message
    ENDIF.

    LOOP AT it_error.
      WRITE:/4  it_error-lifnr,
             17 it_error-tabix CENTERED,
             30 it_error-error_rec.
    ENDLOOP.

    REFRESH it_sucess.
    REFRESH it_error.

  ENDIF.

CODE IN INCLUDE.

*----------------------------------------------------------------------*
*  Include           ZMM_INCL_PURCHASEORDERKB01
*----------------------------------------------------------------------*

DATA:   it_BDCDATA LIKE BDCDATA    OCCURS 0 WITH HEADER LINE.
DATA:   it_MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.
DATA:   E_GROUP_OPENED.

*-----Internal table to store sucess records

DATA:BEGIN OF it_sucess OCCURS 0,
       msgtyp(1)   TYPE c,
       lifnr  LIKE  ekko-lifnr,
       tabix  LIKE  sy-tabix,
       sucess_rec(125),
     END OF it_sucess.

DATA: g_mess(125) type c.

*-----Internal table to store error records
DATA:BEGIN OF it_error OCCURS 0,
       msgtyp(1)   TYPE c,
       lifnr  LIKE  ekko-lifnr,
       tabix  LIKE  sy-tabix,
       error_rec(125),
     END OF it_error.

*----------------------------------------------------------------------*
*        Selection screen
*----------------------------------------------------------------------*

SELECTION-SCREEN BEGIN OF LINE.

PARAMETERS session RADIOBUTTON GROUP ctu.  "create session
SELECTION-SCREEN COMMENT 3(20) text-s07 FOR FIELD session.
SELECTION-SCREEN POSITION 45.
PARAMETERS ctu RADIOBUTTON GROUP ctu.     "call transaction
SELECTION-SCREEN COMMENT 48(20) text-s08 FOR FIELD ctu.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 3(20) text-s01 FOR FIELD group.
SELECTION-SCREEN POSITION 25.
PARAMETERS group(12).                      "group name of session
SELECTION-SCREEN COMMENT 48(20) text-s05 FOR FIELD ctumode.
SELECTION-SCREEN POSITION 70.
PARAMETERS ctumode LIKE ctu_params-dismode DEFAULT 'N'.

"A: show all dynpros
"E: show dynpro on error only
"N: do not display dynpro

SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 48(20) text-s06 FOR FIELD cupdate.
SELECTION-SCREEN POSITION 70.
PARAMETERS cupdate LIKE ctu_params-updmode DEFAULT 'L'.
SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 3(20) text-s03 FOR FIELD keep.
SELECTION-SCREEN POSITION 25.

PARAMETERS: keep AS CHECKBOX.       "' ' = delete session if finished
"'X' = keep   session if finished
SELECTION-SCREEN COMMENT 48(20) text-s09 FOR FIELD e_group.
SELECTION-SCREEN POSITION 70.
PARAMETERS e_group(12).             "group name of error-session
SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 51(17) text-s03 FOR FIELD e_keep.
SELECTION-SCREEN POSITION 70.
PARAMETERS: e_keep AS CHECKBOX.     "' ' = delete session if finished
"'X' = keep   session if finished

SELECTION-SCREEN END OF LINE.
PARAMETERS:p_file LIKE rlgrap-filename.

*----------------------------------------------------------------------*
*   at selection screen                                                *
*----------------------------------------------------------------------*

AT SELECTION-SCREEN.
* group and user must be filled for create session
  IF SESSION = 'X' AND
     GROUP = SPACE. "OR USER = SPACE.
    MESSAGE E613(MS).
  ENDIF.

*----------------------------------------------------------------------*
*   create batchinput session                                          *
*----------------------------------------------------------------------*

FORM OPEN_GROUP.
  IF SESSION = 'X'.
    SKIP.
    WRITE: /(20) 'Create group'(I01), GROUP.
    SKIP.

*----open batchinput group
    CALL FUNCTION 'BDC_OPEN_GROUP'
      EXPORTING
        CLIENT = SY-MANDT
        GROUP  = GROUP
        USER   = sy-uname.
    WRITE:/(30) 'BDC_OPEN_GROUP'(I02),
            (12) 'returncode:'(I05),
                 SY-SUBRC.
  ENDIF.
ENDFORM.                    "OPEN_GROUP

*----------------------------------------------------------------------*
*   end batchinput session                                             *
*----------------------------------------------------------------------*

FORM CLOSE_GROUP.
  IF SESSION = 'X'.

*------close batchinput group
    CALL FUNCTION 'BDC_CLOSE_GROUP'.
    WRITE: /(30) 'BDC_CLOSE_GROUP'(I04),
            (12) 'returncode:'(I05),
                 SY-SUBRC.

  ELSE.
    IF E_GROUP_OPENED = 'X'.
      CALL FUNCTION 'BDC_CLOSE_GROUP'.
      WRITE: /.
      WRITE: /(30) 'Fehlermappe wurde erzeugt'(I06).
    ENDIF.
  ENDIF.
ENDFORM.                    "CLOSE_GROUP


*----------------------------------------------------------------------*
*        Start new transaction according to parameters                 *
*----------------------------------------------------------------------*

FORM BDC_TRANSACTION USING TCODE TYPE ANY.
  DATA: L_SUBRC LIKE SY-SUBRC.
*------batch input session
  IF SESSION = 'X'.
    CALL FUNCTION 'BDC_INSERT'
      EXPORTING
        TCODE     = TCODE
      TABLES
        DYNPROTAB = it_BDCDATA.

    WRITE: / 'BDC_INSERT'(I03),
             TCODE,
             'returncode:'(I05),
             SY-SUBRC,
             'RECORD:',
             SY-INDEX.
  ELSE.
    REFRESH it_MESSTAB.
    CALL TRANSACTION TCODE USING it_BDCDATA
                     MODE   CTUMODE
                     UPDATE CUPDATE
                     MESSAGES INTO it_MESSTAB.
    L_SUBRC = SY-SUBRC.
    WRITE: / 'CALL_TRANSACTION',
             TCODE,
             'returncode:'(I05),
             L_SUBRC,
             'RECORD:',
             SY-INDEX.
  ENDIF.

*----------------------------------------------------------------------*
*   Message handling for Call Transaction                              *
*----------------------------------------------------------------------*
  perform subr_mess_hand using g_mess.

*-----Erzeugen fehlermappe
  IF L_SUBRC <> 0 AND E_GROUP <> SPACE.
    IF E_GROUP_OPENED = ' '.
      CALL FUNCTION 'BDC_OPEN_GROUP'
        EXPORTING
          CLIENT = SY-MANDT
          GROUP  = E_GROUP
          USER   = sy-uname
          KEEP   = E_KEEP.
      E_GROUP_OPENED = 'X'.
    ENDIF.

    CALL FUNCTION 'BDC_INSERT'
      EXPORTING
        TCODE     = TCODE
      TABLES
        DYNPROTAB = it_BDCDATA.
  ENDIF.

  REFRESH it_BDCDATA.

ENDFORM.                    "BDC_TRANSACTION

*----------------------------------------------------------------------*
*      Form  subr_bdc_table                                            *
*----------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_0220   text                                                *
*      -->P_0221   text                                                *
*      -->P_0222   text                                                *
*----------------------------------------------------------------------*

FORM subr_bdc_table  USING      VALUE(P_0220) TYPE ANY
                                VALUE(P_0221) TYPE ANY
                                VALUE(P_0222) TYPE ANY.

  CLEAR it_bdcdata.
  IF P_0220 = ' '.
    CLEAR it_bdcdata.
    it_bdcdata-fnam     = P_0221.
    it_bdcdata-fval     = P_0222.
    APPEND it_bdcdata.
  ELSE.
    it_bdcdata-dynbegin = P_0220.
    it_bdcdata-program  = P_0221.
    it_bdcdata-dynpro   = P_0222.
    APPEND it_bdcdata.
  ENDIF.

ENDFORM.                    " subr_bdc_table

*----------------------------------------------------------------------*
*      Form  subr_mess_hand                                            *
*----------------------------------------------------------------------*
*       text                                                           *
*----------------------------------------------------------------------*
*      -->P_G_MESS  text                                               *
*----------------------------------------------------------------------*

FORM subr_mess_hand USING  P_G_MESS TYPE ANY.
  LOOP AT IT_MESSTAB.
    CALL FUNCTION 'FORMAT_MESSAGE'
      EXPORTING
        ID     = it_messtab-msgid
        LANG   = it_messtab-msgspra
        NO     = it_messtab-msgnr
        v1     = it_messtab-msgv1
        v2     = it_messtab-msgv2
      IMPORTING
        MSG    = P_G_MESS
      EXCEPTIONS
        OTHERS = 0.

    CASE it_messtab-msgtyp.
      when 'E'.
        it_error-error_rec   =  P_G_MESS.
        it_error-lifnr       =  it_me21-lifnr.
        it_error-tabix       =  v_count.
        APPEND IT_ERROR.
      when 'S'.
        it_sucess-sucess_rec =  P_G_MESS.
        it_sucess-lifnr      =  it_me21-lifnr.
        it_sucess-tabix      =  v_count.
        APPEND IT_SUCESS.

    endcase.

  ENDLOOP.
  Describe table it_sucess lines v_ns.
  Describe table it_error  lines v_ne.

ENDFORM.                    " subr_mess_hand

ABAP

See Also:
How To Program the F4 Help

Tables
Database Table

ABAP Books List
ABAP/4 Certification, Programming, Smartforms, Sapscripts and Object Oriented Programming Books

Smart Forms
SAP Smartforms

ABAP Menu:
ABAP Example Hints and Tips

Return to Index:-
SAP ABAP/4 Programming, Basis Administration, Configuration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.  The site www.gotothings.com is in no way affiliated with SAP AG.
Any unauthorised copying or mirroring is prohibited.