Register Login

BDC Session method

Updated May 18, 2018

*&---------------------------------------------------------------------*
*& Report  ZBC2_DVG_SO_BDC
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZBC2_DVG_SO_BDC.
*********STRUCTURE FOR HEADER***********
TYPES: BEGIN OF STR_HEADER,
       VBELN TYPE ZBC2_DVG_SO1-VBELN,
       KUNNR TYPE ZBC2_DVG_SO1-KUNNR,
       ERDAT TYPE ZBC2_DVG_SO1-ERDAT,
       END OF STR_HEADER.
************STRUCTURE FOR ITEM*************
TYPES: BEGIN OF STR_ITEM,
       VBELN TYPE ZBC2_DVG_SO1-VBELN,
       MATNR TYPE ZBC2_DVG_SO2-MATNR,
       MENGE(13),
       END OF STR_ITEM.
*************STRUCTURE FOR FINAL***********
TYPES: BEGIN OF STR_FINAL,
       FIELD1 TYPE CHAR18,
       FIELD2 TYPE CHAR18,
       FIELD3 TYPE CHAR18,
       END OF STR_FINAL.
***********INTERNAL TABLE AND WORK AREA*************
DATA : IT_HEADER TYPE STANDARD TABLE OF STR_HEADER,
       IT_ITEM TYPE STANDARD TABLE OF STR_ITEM,
       IT_BDCDATA TYPE STANDARD TABLE OF BDCDATA,
       IT_MESSAGE TYPE STANDARD TABLE OF BDCMSGCOLL,
       IT_FINAL TYPE STANDARD TABLE OF STR_FINAL.
DATA : WA_HEADER TYPE STR_HEADER,
       WA_ITEM TYPE STR_ITEM,
       WA_BDCDATA TYPE BDCDATA,
       WA_MESSAGE TYPE BDCMSGCOLL,
       WA_FINAL TYPE STR_FINAL,
       WA_TEMP TYPE STR_FINAL.
**********PARAMETERS AND VARIABLE DECLARATION************
 SELECTION-SCREEN:BEGIN OF BLOCK B1 WITH FRAME.
 PARAMETERS: FILE TYPE LOCALFILE OBLIGATORY.
 SELECTION-SCREEN:END OF BLOCK B1.
 DATA: F_NAME TYPE STRING,
       MSG TYPE STRING,
       VAL TYPE N,
       V_MATNR TYPE STRING,
       V_MENGE TYPE STRING.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR FILE.
***********F4_FILENAME***************
  CALL FUNCTION 'F4_FILENAME'
   EXPORTING
     PROGRAM_NAME        = SYST-CPROG
     DYNPRO_NUMBER       = SYST-DYNNR
*     FIELD_NAME          = ' '
   IMPORTING
     FILE_NAME           = FILE

            .
**********GUI_UPLOAD************
  START-OF-SELECTION.
   F_NAME = FILE.
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
     FILENAME                      = F_NAME
     FILETYPE                      = 'ASC'
     HAS_FIELD_SEPARATOR           = 'X'
*     HEADER_LENGTH                 = 0
*     READ_BY_LINE                  = 'X'
*     DAT_MODE                      = ' '
*     CODEPAGE                      = ' '
*     IGNORE_CERR                   = ABAP_TRUE
*     REPLACEMENT                   = '#'
*     CHECK_BOM                     = ' '
*     VIRUS_SCAN_PROFILE            =
*     NO_AUTH_CHECK                 = ' '
*   IMPORTING
*     FILELENGTH                    =
*     HEADER                        =
    TABLES
      DATA_TAB                      = IT_FINAL
   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 SY-MSGTY NUMBER SY-MSGNO
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

*************SPLIT THE VALES INTO CORRESPONDING TABLE****************

LOOP AT IT_FINAL INTO WA_FINAL.
  MOVE-CORRESPONDING WA_FINAL TO WA_TEMP.
  AT NEW FIELD1.
    WA_HEADER-VBELN = WA_TEMP-FIELD1.
    WA_HEADER-KUNNR = WA_TEMP-FIELD2.
    WA_HEADER-ERDAT = WA_TEMP-FIELD3.
    APPEND WA_HEADER TO IT_HEADER.
    CONTINUE.
  ENDAT.
    WA_ITEM-VBELN = WA_TEMP-FIELD1.
    WA_ITEM-MATNR = WA_TEMP-FIELD2.
    WA_ITEM-MENGE = WA_TEMP-FIELD3.
    APPEND WA_ITEM TO IT_ITEM.
    CLEAR WA_ITEM.
    CLEAR WA_TEMP.
ENDLOOP.

***********BDC_OPEN_GROUP************
  CALL FUNCTION 'BDC_OPEN_GROUP'
   EXPORTING
     CLIENT                    = SY-MANDT
*     DEST                      = FILLER8
     GROUP                     = 'SO_BDC'
*     HOLDDATE                  = FILLER8
     KEEP                      = 'X'
     USER                      = SY-UNAME
*     RECORD                    = FILLER1
*     PROG                      = SY-CPROG
*     DCPFM                     = '%'
*     DATFM                     = '%'
*   IMPORTING
*     QID                       =
   EXCEPTIONS
     CLIENT_INVALID            = 1
     DESTINATION_INVALID       = 2
     GROUP_INVALID             = 3
     GROUP_IS_LOCKED           = 4
     HOLDDATE_INVALID          = 5
     INTERNAL_ERROR            = 6
     QUEUE_ERROR               = 7
     RUNNING                   = 8
     SYSTEM_LOCK_ERROR         = 9
     USER_INVALID              = 10
     OTHERS                    = 11
            .
  IF SY-SUBRC <> 0.
 MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.
***********START OF SELECTION*************
  START-OF-SELECTION.
LOOP AT IT_HEADER INTO WA_HEADER.

  CLEAR IT_BDCDATA.

  PERFORM GET.
***********BDC_INSERT*************
  CALL FUNCTION 'BDC_INSERT'
   EXPORTING
     TCODE                  = 'Z_SO'
*     POST_LOCAL             = NOVBLOCAL
*     PRINTING               = NOPRINT
*     SIMUBATCH              = ' '
*     CTUPARAMS              = ' '
    TABLES
      DYNPROTAB              = IT_BDCDATA[]
   EXCEPTIONS
     INTERNAL_ERROR         = 1
     NOT_OPEN               = 2
     QUEUE_ERROR            = 3
     TCODE_INVALID          = 4
     PRINTING_INVALID       = 5
     POSTING_INVALID        = 6
     OTHERS                 = 7
            .
  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.
***********BDC_CLOSE_GROUP*************
  CALL FUNCTION 'BDC_CLOSE_GROUP'
   EXCEPTIONS
     NOT_OPEN          = 1
     QUEUE_ERROR       = 2
     OTHERS            = 3
            .
  IF SY-SUBRC EQ 0.
    WRITE: 'SESSION CREATED'.
  ENDIF.
************RECORDING PROGRAM**************
FORM GET.

perform bdc_dynpro      using 'ZBC2_DVG_SO2' '9200'.
perform bdc_field       using 'BDC_CURSOR'
                              'WA_SO2-MENGE(01)'.
perform bdc_field       using 'BDC_OKCODE'
                              '/00'.
perform bdc_field       using 'WA_SO1-VBELN'
                              WA_HEADER-VBELN.
perform bdc_field       using 'WA_SO1-KUNNR'
                              WA_HEADER-KUNNR.
perform bdc_field       using 'WA_SO1-ERDAT'
                              WA_HEADER-ERDAT.
VAL = 0.
LOOP AT IT_ITEM INTO WA_ITEM WHERE VBELN = WA_HEADER-VBELN.
VAL = VAL + 1.

 CONCATENATE 'WA_SO2-MATNR('VAL')' INTO V_MATNR.
perform bdc_field       using V_MATNR
                              WA_ITEM-MATNR.
CONCATENATE 'WA_SO2-MENGE('VAL')' INTO V_MENGE.
perform bdc_field       using V_MENGE
                              WA_ITEM-MENGE.
perform bdc_dynpro      using 'ZBC2_DVG_SO2' '9200'.
perform bdc_field       using 'BDC_CURSOR'
                              'WA_SO2-MENGE(02)'.
perform bdc_field       using 'BDC_OKCODE'
                              '/00'.
ENDLOOP.

perform bdc_field       using 'BDC_OKCODE'
                              '=CREATE'.
perform bdc_dynpro      using 'ZBC2_DVG_SO2' '9200'.
perform bdc_field       using 'BDC_CURSOR'
                              'WA_SO1-VBELN'.
perform bdc_field       using 'BDC_OKCODE'
                              '=EXIT'.

ENDFORM.
FORM BDC_DYNPRO USING PROGRAM DYNPRO.
  CLEAR WA_BDCDATA.
  WA_BDCDATA-PROGRAM  = PROGRAM.
  WA_BDCDATA-DYNPRO   = DYNPRO.
  WA_BDCDATA-DYNBEGIN = 'X'.
  APPEND WA_BDCDATA TO IT_BDCDATA.
ENDFORM.

*----------------------------------------------------------------------*
*        Insert field                                                  *
*----------------------------------------------------------------------*
FORM BDC_FIELD USING FNAM FVAL.
    CLEAR WA_BDCDATA.
    WA_BDCDATA-FNAM = FNAM.
    WA_BDCDATA-FVAL = FVAL.
    APPEND WA_BDCDATA TO IT_BDCDATA.
ENDFORM.




**************Flat File*************
2001    1001    02052011
2001    5004    50
2001    5005    60
2001    5003    200
2002    1002    02052011
2002    5001    100
2002    5006    300
2002    5007    400


×