Tuesday, 4 December 2012

BDC - Session Method

BDC (Batch Data Communication):
       It Transfers or upload the data’s(records) from Legacy(Non SAP) system to SAP system.
 Batch input means bulk amount of data. It is a programming method. 

Types: It has two types
 1. CALL TRANSACTION METHOD.
 2. SESSION METHOD (CLASSICAL).

Session Method:
1. Asynchronous Processing.
2. Synchronous database updates.
3. Transfer of data for multiple transactions. Using FM bdc_insert.
4. As compared to call transaction, this is slow.
5. Best suited for bulk transfer.
6. Automatic error handling.
7. Error logs are created.
8. The value of sy-subrc is never returned. Because records are stored  temporary in session and after the records are updated   to the database.  
9. You can store the sessions and it can be used for later processing.                                                         10. it can handle small amount of data as well as large amount of data.  
11. Function modules :
üF4_FILENAME ( Open the legacy file [.txt or .xls]) .
üTEXT_CONVERT_XSL_TO_SAP ( Convert the .xls file to sap format) : Using for only .xls files.
üGUI_UPLOAD : Using for only .txt files.
üBDC_OPEN_GROUP : To create a new session.(No session 1).
üBDC_INSERT :  To add a transaction to a batch input session.
üBDC_CLOSE_GROUP : To close the session.

Example 1: Session method ( .txt file) Customer(xd01) master creation:
report ZRBDC_XD01_RAJA no standard page heading line-size 255.

*include bdcrecx1.

data: begin of record OCCURS 0,
* data element: BUKRS
        BUKRS_001
(004),
* data element: KTOKD
        KTOKD_002
(004),
* data element: ANRED
        ANRED_003
(015),
* data element: NAME1_GP
        NAME1_004
(035),
* data element: SORTL
        SORTL_005
(010),
* data element: ORT01_GP
        ORT01_006
(035),
* data element: PSTLZ
        PSTLZ_007
(010),
* data element: LAND1_GP
        LAND1_008
(003),
* data element: REGIO
        REGIO_009
(003),
* data element: SPRAS
        SPRAS_010
(002),
* data element: CIVVE
        CIVVE_011
(001),
* data element: AKONT
        AKONT_012
(010),
      
end of record.

***************GLOBAL STR BDCDATA***********************

DATA: IT_BDCDATA TYPE STANDARD TABLE OF BDCDATA,
      WA_BDCDATA 
TYPE BDCDATA.

DATA: FILE1 TYPE STRING.

********************************************************

******************UPLOAD FLAT FILE**********************

******************SELECTION SCREEN**********************

SELECTION-SCREEN: BEGIN OF BLOCK B1 WITH FRAME.

PARAMETERS: FILE TYPE LOCALFILE OBLIGATORY.

SELECTION-SCREEN: END OF BLOCK B1.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR FILE.

**********************************************************

******************FUNCTION MODULE************************

CALL FUNCTION 'F4_FILENAME'
 
EXPORTING
   PROGRAM_NAME        
SYST-CPROG
   DYNPRO_NUMBER       
SYST-DYNNR
*   FIELD_NAME          = ' '
 
IMPORTING
   FILE_NAME           
FILE.


FILE1 
FILE.

CALL FUNCTION 'GUI_UPLOAD'
  
EXPORTING
    filename                      
FILE1
   FILETYPE                      
'ASC'
   HAS_FIELD_SEPARATOR           
'X'
*   HEADER_LENGTH                 = 0
*   READ_BY_LINE                  = 'X'
*   DAT_MODE                      = ' '
* IMPORTING
*   FILELENGTH                    =
*   HEADER                        =
  
tables
    data_tab                      
RECORD
 
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.

***************************************************
**************OPEN SESSION*************************

CALL FUNCTION 'BDC_OPEN_GROUP'
 
EXPORTING
   
CLIENT                    SY-MANDT
*   DEST                      = FILLER8
   
GROUP                     'demo'           "LOGON USERNAME
*   HOLDDATE                  = FILLER8
   KEEP                      
'X'
   USER                      
SY-UNAME
*   RECORD                    = FILLER1
*   PROG                      = SY-CPROG
* 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.

*** End generated data section ***

start-of-selection.

*perform open_group.

LOOP AT RECORD.

perform bdc_dynpro      using 'SAPMF02D' '0100'.
perform bdc_field       using 'BDC_CURSOR'
                              
'RF02D-KTOKD'.
perform bdc_field       using 'BDC_OKCODE'
                              
'/00'.
perform bdc_field       using 'RF02D-BUKRS'
                              record
-BUKRS_001.
perform bdc_field       using 'RF02D-KTOKD'
                              record
-KTOKD_002.
perform bdc_dynpro      using 'SAPMF02D' '0110'.
perform bdc_field       using 'BDC_CURSOR'
                              
'KNA1-SPRAS'.
perform bdc_field       using 'BDC_OKCODE'
                              
'/00'.
perform bdc_field       using 'KNA1-ANRED'
                              record
-ANRED_003.
perform bdc_field       using 'KNA1-NAME1'
                              record
-NAME1_004.
perform bdc_field       using 'KNA1-SORTL'
                              record
-SORTL_005.
perform bdc_field       using 'KNA1-ORT01'
                              record
-ORT01_006.
perform bdc_field       using 'KNA1-PSTLZ'
                              record
-PSTLZ_007.
perform bdc_field       using 'KNA1-LAND1'
                              record
-LAND1_008.
perform bdc_field       using 'KNA1-REGIO'
                              record
-REGIO_009.
perform bdc_field       using 'KNA1-SPRAS'
                              record
-SPRAS_010.
perform bdc_dynpro      using 'SAPMF02D' '0120'.
perform bdc_field       using 'BDC_CURSOR'
                              
'KNA1-LIFNR'.
perform bdc_field       using 'BDC_OKCODE'
                              
'/00'.
perform bdc_dynpro      using 'SAPMF02D' '0125'.
perform bdc_field       using 'BDC_CURSOR'
                              
'KNA1-NIELS'.
perform bdc_field       using 'BDC_OKCODE'
                              
'/00'.
perform bdc_dynpro      using 'SAPMF02D' '0130'.
perform bdc_field       using 'BDC_CURSOR'
                              
'KNBK-BANKS(01)'.
perform bdc_field       using 'BDC_OKCODE'
                              
'=ENTR'.
perform bdc_dynpro      using 'SAPMF02D' '0340'.
perform bdc_field       using 'BDC_CURSOR'
                              
'RF02D-KUNNR'.
perform bdc_field       using 'BDC_OKCODE'
                              
'=ENTR'.
perform bdc_dynpro      using 'SAPMF02D' '0370'.
perform bdc_field       using 'BDC_CURSOR'
                              
'RF02D-KUNNR'.
perform bdc_field       using 'BDC_OKCODE'
                              
'=ENTR'.
perform bdc_field       using 'KNA1-CIVVE'
                              record
-CIVVE_011.
perform bdc_dynpro      using 'SAPMF02D' '0360'.
perform bdc_field       using 'BDC_CURSOR'
                              
'KNVK-NAMEV(01)'.
perform bdc_field       using 'BDC_OKCODE'
                              
'=ENTR'.
perform bdc_dynpro      using 'SAPMF02D' '0210'.
perform bdc_field       using 'BDC_CURSOR'
                              
'KNB1-AKONT'.
perform bdc_field       using 'BDC_OKCODE'
                              
'/00'.
perform bdc_field       using 'KNB1-AKONT'
                              record
-AKONT_012.
perform bdc_dynpro      using 'SAPMF02D' '0215'.
perform bdc_field       using 'BDC_CURSOR'
                              
'KNB1-ZTERM'.
perform bdc_field       using 'BDC_OKCODE'
                              
'/00'.
perform bdc_dynpro      using 'SAPMF02D' '0220'.
perform bdc_field       using 'BDC_CURSOR'
                              
'KNB5-MAHNA'.
perform bdc_field       using 'BDC_OKCODE'
                              
'/00'.
perform bdc_dynpro      using 'SAPMF02D' '0230'.
perform bdc_field       using 'BDC_CURSOR'
                              
'KNB1-VRSNR'.
perform bdc_field       using 'BDC_OKCODE'
                              
'/00'.
*perform bdc_transaction using 'XD01'.



*****************************************************
****************USE BDC INSERT FM***********************

CALL FUNCTION 'BDC_INSERT'
 
EXPORTING
   TCODE                  
'XD01'
*   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.
refresh it_bdcdata.

ENDLOOP.
************************************************************

*perform close_group.

******************CALL SESSION CLOSE FM*********************

CALL FUNCTION 'BDC_CLOSE_GROUP'
 
EXCEPTIONS
   NOT_OPEN          
1
   QUEUE_ERROR       
2
   
OTHERS            3
          .
IF sy-subrc <> 0.
WRITE:'SESSION CREATED go to sm35'.

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

************************************************************

************FORM BDC_DYPRO**********************************

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.

************************************************************

*****************FORM BDC_FIELDS**************************

FORM BDC_FIELD USING FNAM FVAL.

CLEAR WA_BDCDATA.

WA_BDCDATA
-FNAM FNAM.
WA_BDCDATA
-FVAL FVAL.

APPEND WA_BDCDATA TO IT_BDCDATA.

ENDFORM.

************************************************************
record: (.txt file):
1000   YB01   mr      RAJA50 IN     NAGERCOIL    629401          IN       18      EN      X        19103103
1000   YB01   mr      RAJA51 IN     NAGERCOIL    629401          IN       18      EN      X        19103103



Example 2: Session method ( .xls file) Customer(xd01) master creation:
report ZRBDC_XD01_RAJA no standard page heading line-size 255.

*include bdcrecx1.
TYPE-POOLS TRUXS. “This is use to data conversion in TEXT_CONVERT_XLS_TO_SAP.

data: begin of record OCCURS 0,
* data element: BUKRS
        BUKRS_001
(004),
* data element: KTOKD
        KTOKD_002
(004),
* data element: ANRED
        ANRED_003
(015),
* data element: NAME1_GP
        NAME1_004
(035),
* data element: SORTL
        SORTL_005
(010),
* data element: ORT01_GP
        ORT01_006
(035),
* data element: PSTLZ
        PSTLZ_007
(010),
* data element: LAND1_GP
        LAND1_008
(003),
* data element: REGIO
        REGIO_009
(003),
* data element: SPRAS
        SPRAS_010
(002),
* data element: CIVVE
        CIVVE_011
(001),
* data element: AKONT
        AKONT_012
(010),
      
end of record.

***************GLOBAL STR BDCDATA***********************

DATA: IT_BDCDATA TYPE STANDARD TABLE OF BDCDATA,
      WA_BDCDATA 
TYPE BDCDATA.
********************************************************

******************UPLOAD FLAT FILE**********************

******************SELECTION SCREEN**********************

DATA IT_RAW TYPE TRUXS_T_TEXT_DATA.
PARAMETERS PATH TYPE RLGRAP-FILENAME OBLIGATORY.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR PATH.

**********************************************************

******************FUNCTION MODULE************************

CALL FUNCTION 'F4_FILENAME'
 
EXPORTING
   PROGRAM_NAME        
SYST-CPROG
   DYNPRO_NUMBER       
SYST-DYNNR
   FIELD_NAME          = 'path'
 
IMPORTING
   FILE_NAME           
path.

CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
  
EXPORTING
*   I_FIELD_SEPERATOR          =
*   I_LINE_HEADER              =
    I_TAB_RAW_DATA             
IT_RAW
    I_FILENAME                 
PATH
  
TABLES
    I_TAB_CONVERTED_DATA       
RECORD
* EXCEPTIONS
*   CONVERSION_FAILED          = 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.

***************************************************
**************OPEN SESSION*************************

CALL FUNCTION 'BDC_OPEN_GROUP'
 
EXPORTING
   
CLIENT                    SY-MANDT
*   DEST                      = FILLER8
   
GROUP                     'demo'           "LOGON USERNAME
*   HOLDDATE                  = FILLER8
   KEEP                      
'X'
   USER                      
SY-UNAME
*   RECORD                    = FILLER1
*   PROG                      = SY-CPROG
* 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.

*** End generated data section ***

start-of-selection.

*perform open_group.

LOOP AT RECORD.

perform bdc_dynpro      using 'SAPMF02D' '0100'.
perform bdc_field       using 'BDC_CURSOR'
                              
'RF02D-KTOKD'.
perform bdc_field       using 'BDC_OKCODE'
                              
'/00'.
perform bdc_field       using 'RF02D-BUKRS'
                              record
-BUKRS_001.
perform bdc_field       using 'RF02D-KTOKD'
                              record
-KTOKD_002.
perform bdc_dynpro      using 'SAPMF02D' '0110'.
perform bdc_field       using 'BDC_CURSOR'
                              
'KNA1-SPRAS'.
perform bdc_field       using 'BDC_OKCODE'
                              
'/00'.
perform bdc_field       using 'KNA1-ANRED'
                              record
-ANRED_003.
perform bdc_field       using 'KNA1-NAME1'
                              record
-NAME1_004.
perform bdc_field       using 'KNA1-SORTL'
                              record
-SORTL_005.
perform bdc_field       using 'KNA1-ORT01'
                              record
-ORT01_006.
perform bdc_field       using 'KNA1-PSTLZ'
                              record
-PSTLZ_007.
perform bdc_field       using 'KNA1-LAND1'
                              record
-LAND1_008.
perform bdc_field       using 'KNA1-REGIO'
                              record
-REGIO_009.
perform bdc_field       using 'KNA1-SPRAS'
                              record
-SPRAS_010.
perform bdc_dynpro      using 'SAPMF02D' '0120'.
perform bdc_field       using 'BDC_CURSOR'
                              
'KNA1-LIFNR'.
perform bdc_field       using 'BDC_OKCODE'
                              
'/00'.
perform bdc_dynpro      using 'SAPMF02D' '0125'.
perform bdc_field       using 'BDC_CURSOR'
                              
'KNA1-NIELS'.
perform bdc_field       using 'BDC_OKCODE'
                              
'/00'.
perform bdc_dynpro      using 'SAPMF02D' '0130'.
perform bdc_field       using 'BDC_CURSOR'
                              
'KNBK-BANKS(01)'.
perform bdc_field       using 'BDC_OKCODE'
                              
'=ENTR'.
perform bdc_dynpro      using 'SAPMF02D' '0340'.
perform bdc_field       using 'BDC_CURSOR'
                              
'RF02D-KUNNR'.
perform bdc_field       using 'BDC_OKCODE'
                              
'=ENTR'.
perform bdc_dynpro      using 'SAPMF02D' '0370'.
perform bdc_field       using 'BDC_CURSOR'
                              
'RF02D-KUNNR'.
perform bdc_field       using 'BDC_OKCODE'
                              
'=ENTR'.
perform bdc_field       using 'KNA1-CIVVE'
                              record
-CIVVE_011.
perform bdc_dynpro      using 'SAPMF02D' '0360'.
perform bdc_field       using 'BDC_CURSOR'
                              
'KNVK-NAMEV(01)'.
perform bdc_field       using 'BDC_OKCODE'
                              
'=ENTR'.
perform bdc_dynpro      using 'SAPMF02D' '0210'.
perform bdc_field       using 'BDC_CURSOR'
                              
'KNB1-AKONT'.
perform bdc_field       using 'BDC_OKCODE'
                              
'/00'.
perform bdc_field       using 'KNB1-AKONT'
                              record
-AKONT_012.
perform bdc_dynpro      using 'SAPMF02D' '0215'.
perform bdc_field       using 'BDC_CURSOR'
                              
'KNB1-ZTERM'.
perform bdc_field       using 'BDC_OKCODE'
                              
'/00'.
perform bdc_dynpro      using 'SAPMF02D' '0220'.
perform bdc_field       using 'BDC_CURSOR'
                              
'KNB5-MAHNA'.
perform bdc_field       using 'BDC_OKCODE'
                              
'/00'.
perform bdc_dynpro      using 'SAPMF02D' '0230'.
perform bdc_field       using 'BDC_CURSOR'
                              
'KNB1-VRSNR'.
perform bdc_field       using 'BDC_OKCODE'
                              
'/00'.
*perform bdc_transaction using 'XD01'.



*****************************************************
****************USE BDC INSERT FM***********************

CALL FUNCTION 'BDC_INSERT'
 
EXPORTING
   TCODE                  
'XD01'
*   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.
refresh it_bdcdata.

ENDLOOP.
************************************************************

*perform close_group.

******************CALL SESSION CLOSE FM*********************

CALL FUNCTION 'BDC_CLOSE_GROUP'
 
EXCEPTIONS
   NOT_OPEN          
1
   QUEUE_ERROR       
2
   
OTHERS            3
          .
IF sy-subrc <> 0.
WRITE:'SESSION CREATED go to sm35'.

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

************************************************************

************FORM BDC_DYPRO**********************************

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.

************************************************************

*****************FORM BDC_FIELDS**************************

FORM BDC_FIELD USING FNAM FVAL.

CLEAR WA_BDCDATA.

WA_BDCDATA
-FNAM FNAM.
WA_BDCDATA
-FVAL FVAL.

APPEND WA_BDCDATA TO IT_BDCDATA.

ENDFORM.

************************************************************
record: (.txt file):
1000   YB01   mr      RAJA50 IN     NAGERCOIL    629401          IN       18      EN      X        19103103
1000   YB01   mr      RAJA51 IN     NAGERCOIL    629401          IN       18      EN      X        19103103































2 comments: