Skip to Content
Technical Articles
Author's profile photo Muhammad Muzammil Khan

BDC – Batch Data Communication in SAP ABAP

BDC – Batch Data Communication in SAP ABAP

BDC means Batch Data Communication, not Batch Data Conversion. Moreover, it was renamed Batch Input at least 20 years ago (the term BDC is still widely used though).

In this blog, I am gonna discussed the SAP DATA Migration tools mainly BDC- Batch Data Communication to export data from non-SAP environment to SAP ERP.
I will create BDC (Upload) Program for cheques with Payment doc.i-e; the BDC Program for transaction code FCH5 – Create Check.

I will discussed and write ABAP Code for BDC creation using two methods.

1- Online/Real time Data Upload.

2- Session/Batch Data Upload  (using t-code SM35)

Pros and Cons.

1- Online/Real time Data Upload.

The Error Handling is Explicit: means you have to write ABAP code to collect errors generated for each records at the time of Program Execute for analysis using Message table T001.

2- Session Method Data Upload  (using t-code SM35)

The Error Handling is Implicit: means you don’t have to collect the error messages for records to analyze, as it was already get created when Batch (created ) is execute from SM35. And you can analyze the errors for Batch over there.

1- Online/Real time Data Upload.

Below are the ABAP coding steps for BDC creation using Online/Real time Data Upload.,

     1- Declare Structure/Internal Table/Work Area for File Format.

     2- Call Function module ‘GUI_Upload’
     3- Assign Internal Table ‘IT_FILE‘ in Function Module, to collect file data in it.

     4- Declare Internal table / Work Area for BDCDATA (from Std Structure in SAP)
     5- Clear WA_BDCDATA and assign SCREEN PROGRAM, SCREEN NUMBER and DYNBEGIN            which you can extract using recording from t-code: SHDB.
     6- Clear WA_BDCDATA Again and Assign FIELDNAME and FIELDVALUE in it from Work Area              field ‘WA_FILE- Field’ and APPEND to ‘IT_BDCDATA’ for all fields.
     7- Then call transaction from syntax
                        Use Syntax <CALL TRANSACTION ‘FCH5’ USING IT_BDCDATA MODE V_MODE
                                                                                               MESSAGES INTO IT_BDCMSGCOLL.> 

                                            – REFRESH IT_BDCDATA.

The possible values for V_MODE will be,

    A – DISPLAY ALL SCREENS

    E – DISPLAY ERRORS

    N – NO SCREENS

Below is the Screen shot explaining the above logic for BDC to collect data from external file and insert in to SAP Table using pertaining T- Code.

 

Error Message Handling

As discussed above, in BDC creation using Online/Real time  Processing the error handling is explicit. Below is the logical Steps to create ABAP Code for error Messages in SAP

1-Error Message Handling

       < LOOP AT IT_BDCMSGCOLL INTO WA_BDCMSGCOLL.
           ENDLOOP. >

      a- Type Manual Code inside Loop for Error Using Message table T001 for Message Type and                 Message  Number from IT_BDCMSGCOLL.

      b- Call Functional Module <CALL FUNCTION ‘FORMAT_MESSAGE‘.>

OR
      c- Using Method (Class) <Cl_demo_output=>display(it_bdcmsgcoll)>.

2- Session Method Data Upload (using t-code SM35)

Session Method and Call Transaction Method both works on same Principle i-e; Via Screens & Screens Fields Repeatedly.

Session is like Folder in which we collect data from file then execute/ upload it to SAP data base via Batch Input Method using t-Code SM35.

There are three steps to upload data through BDC Session Method, those are,

  • Create Session – calling Function Module ‘BDC_OPEN_GROUP’ you open session.
  • Insert Data – in Session from IT_BDCDATA using Function Module ‘BDC_INSERT to insert Screens, Fields and Values.
  • Close Session – using Function Module ‘BDC_CLOSE_GROUP’.

At Last, you execute the SESSION from t-code SM35, as discussed before all the error log are saved by the session, no need to write code.


ABAP Code for  the Reference.

Below is the ABAP BDC Code for reference using  example for t-code : FCH5 -Cheque Creation

*******************************************************
* DATA DECLARATIONS
*******************************************************

TypesBEGIN OF TY_FILE,
VBLNR TYPE PAYRVBLNR“PAYMENT DOC NO
ZBUKR TYPE PAYRZBUKR“COMPANY CODE
GJAHR TYPE PAYRGJAHR“FISCAL YEAR
HBKID TYPE PAYRHBKID“HOUSE BANK
HKTID TYPE PAYRHKTID“ACCOUNT ID
CHECT TYPE PAYRCHECT“CHECK NUMBER
END OF TY_FILE.

DATA IT_FILE TYPE TABLE OF TY_FILE,
WA_FILE TYPE TY_FILE.

DATA IT_BDCDATA TYPE TABLE OF BDCDATA,
WA_BDCDATA TYPE BDCDATA.

DATA IT_BDCMSGCOLL TYPE TABLE OF BDCMSGCOLL,
WA_BDCMSGCOLL TYPE BDCMSGCOLL.

DATA V_RWBTR TYPE WRBTR.

DATA V_FILE TYPE STRING.

DATA V_MODE(1TYPE C.  ” A – DISPLAY ALL SCREENS
” E – DISPLAY ERRORS
” N – NO SCREENS
DATA V_MSG_TXT(100TYPE C.

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

SELECTION-SCREENBEGIN OF BLOCK B1 WITH FRAME TITLE TEXT000.

PARAMETERSP_FILE  TYPE IBIPPARMSPATH.

SELECTION-SCREENEND OF BLOCK B1.

SELECTION-SCREENBEGIN OF BLOCK B4 WITH FRAME TITLE TEXT004.

PARAMETERSV_BOX1 AS CHECKBOX.

SELECTION-SCREENEND OF BLOCK B4.

SELECTION-SCREENBEGIN OF BLOCK B2 WITH FRAME TITLE TEXT001.

PARAMETERSRB_A RADIOBUTTON GROUP G2
RB_E RADIOBUTTON GROUP G2
RB_N RADIOBUTTON GROUP G2
.
SELECTION-SCREENEND OF BLOCK B2.

SELECTION-SCREENBEGIN OF BLOCK B3 WITH FRAME TITLE TEXT002.

PARAMETERSP_SNAME(12TYPE C.

SELECTION-SCREENEND OF BLOCK B3.

*INITIALIZATION.
* LOOP AT SCREEN.
*    IF SCREEN-NAME =’P_SNAME’.
*       SCREEN-INPUT = 0.
*       MODIFY SCREEN.
*    ENDIF.
*  ENDLOOP.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.

PERFORM F4_FILENAME.

*******************************************************
*      AT SELECTION-SCREEN OUTPUT
*******************************************************

AT SELECTION-SCREEN OUTPUT.

IF V_BOX1 ‘X’.
LOOP AT SCREEN.
IF SCREENNAME =‘RB_A’ OR
SCREENNAME =‘RB_E’ OR
SCREENNAME =‘RB_N’  .
SCREENINPUT 0.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
ELSE.
LOOP AT SCREEN.
IF SCREENNAME =‘P_SNAME’.
SCREENINPUT 0.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
ENDIF.

*********************************************************
*RAIO BUTTONS PROGRAMING FOR V_MODE ” A – DISPLAY ALL SCREENS
” E – DISPLAY ERRORS
” N – NO SCREENS

*******************************************************
*  START-OF-SELECTION
*******************************************************

START-OF-SELECTION.
IF RB_A ‘X’.
V_MODE ‘A’.
ELSEIF RB_E ‘X’ .
V_MODE ‘E’.
ELSEIF RB_N ‘X’.
V_MODE ‘N’.
ENDIF.

*******************************************************
*  UPLOAD FILE
*******************************************************

V_FILE P_FILE.
IF V_FILE IS NOT INITIAL.

CALL FUNCTION ‘GUI_UPLOAD’
EXPORTING
FILENAME                      V_FILE
*   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_FILE
* CHANGING
*   ISSCANPERFORMED               = ‘ ‘
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 SYSUBRC 0.
*****
*****CASE SY-SUBRC.
*****  WHEN 5.
*****    MESSAGE ‘INVALID_TYPE ‘ TYPE ‘E’.
*****  WHEN OTHERS.
*****    MESSAGE ‘ERROR’ TYPE ‘E’.
*****ENDCASE.
*****
*****ELSE.
MESSAGE ‘FILE SUCCESSFUL UPLOADED’ TYPE ‘I’.

ENDIF.

********************************************************
*  BDC PROGRAM CALL – SESSION METHOD
********************************************************

IF V_BOX1 ‘X’.

CALL FUNCTION ‘BDC_OPEN_GROUP’
EXPORTING
*         CLIENT                    = SY-MANDT
*         DEST                      = FILLER8
GROUP                     P_SNAME
*         HOLDDATE                  = FILLER8
KEEP                      ‘X’      “TO KEEP SUCCESSFULLY PROCESSED SESSION.
USER                      SYUNAME
*         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 SYSUBRC <> 0.
*       Implement suitable error handling here
ENDIF.

LOOP AT IT_FILE INTO WA_FILE.

SELECT SINGLE WRBTR INTO V_RWBTR FROM BSEG
WHERE BELNR WA_FILEVBLNR.

PERFORM BDC_DYNPRO USING ‘SAPMFCHK’ ‘0500’.
PERFORM BDC_FIELD USING ‘PAYR-VBLNR’ WA_FILEVBLNR.
PERFORM BDC_FIELD USING ‘PAYR-ZBUKR’ WA_FILEZBUKR.
PERFORM BDC_FIELD USING ‘PAYR-GJAHR’ WA_FILEGJAHR.
PERFORM BDC_FIELD USING ‘PAYR-HBKID’ WA_FILEHKTID.
PERFORM BDC_FIELD USING ‘PAYR-CHECT’ WA_FILECHECT.
PERFORM BDC_FIELD USING ‘PAYR-CHECT’ WA_FILECHECT.

*      *************************************************
PERFORM BDC_DYNPRO USING ‘SAPMFCHK’ ‘0501’.
PERFORM BDC_FIELD USING ‘PAYR-RWBTR’ V_RWBTR.
PERFORM BDC_FIELD USING ‘BDC_OKCODE’ ‘=UPDA’.

*      *************************************************
*      REFRESH IT_BDCDATA.

ENDLOOP.
*      *********************************************************
*       FUNCTIONAL MODULE TO INSERT DATA
*      *********************************************************

CALL FUNCTION ‘BDC_INSERT’
EXPORTING
TCODE                  ‘FCH5’
*         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 SYSUBRC 0.
MESSAGE ‘SESSION SUCCESSFUL CREATED’ TYPE ‘I’.
ENDIF.

CALL FUNCTION ‘BDC_CLOSE_GROUP’
*       EXCEPTIONS
*         NOT_OPEN          = 1
*         QUEUE_ERROR       = 2
*         OTHERS            = 3
.
IF SYSUBRC <> 0.
*        MESSAGE ‘SESSION & CREATED SUCCESSFULLY’ TYPE ‘I’.
ENDIF.

ELSE.

*******************************************************
*  BDC PROGRAM CALL – NORMAL METHOD
*******************************************************

LOOP AT IT_FILE INTO WA_FILE.

SELECT SINGLE WRBTR INTO V_RWBTR FROM BSEG
WHERE BELNR WA_FILEVBLNR.

*          SAPMFCHK,0500,X,
*             PAYR-VBLNR = WA_FILE-VBLNR,
*             PAYR-ZBUKR = WA_FILE-ZBUKR,
*             PAYR-GJAHR = WA_FILE-GJAHR,
*             PAYR-HBKID = WA_FILE-HBKID,
*             PAYR-HKTID = WA_FILE-HKTID,
*             PAYR-CHECT = WA_FILE-CHECT.
*
*          SAPMFCHK,0501,X
*             PAYR-RWBTR = V_RWBTR.
*      *************************************************
PERFORM BDC_DYNPRO USING ‘SAPMFCHK’ ‘0500’.
PERFORM BDC_FIELD USING ‘PAYR-VBLNR’ WA_FILEVBLNR.
PERFORM BDC_FIELD USING ‘PAYR-ZBUKR’ WA_FILEZBUKR.
PERFORM BDC_FIELD USING ‘PAYR-GJAHR’ WA_FILEGJAHR.
PERFORM BDC_FIELD USING ‘PAYR-HBKID’ WA_FILEHKTID.
PERFORM BDC_FIELD USING ‘PAYR-CHECT’ WA_FILECHECT.
PERFORM BDC_FIELD USING ‘PAYR-CHECT’ WA_FILECHECT.

*      *************************************************
PERFORM BDC_DYNPRO USING ‘SAPMFCHK’ ‘0501’.
PERFORM BDC_FIELD USING ‘PAYR-RWBTR’ V_RWBTR.
PERFORM BDC_FIELD USING ‘BDC_OKCODE’ ‘=UPDA’.

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

CALL TRANSACTION ‘FCH5’ USING IT_BDCDATA MODE V_MODE
MESSAGES INTO IT_BDCMSGCOLL.  ” A – DISPLAY ALL SCREENS
REFRESH IT_BDCDATA.                                ” E – DISPLAY ERRORS
” N – NO SCREENS
*      *ERROR MESSAGE HANDLING*****IF_’N’**********************************************************

*       IF SY-SUBRC = 0.
*         WRITE : / ‘CHEQUE’,WA_FILE-CHECT,’FOR PAYT DOC’,WA_FILE-VBLNR,’IS ASSIGNED.’ COLOR 1.
*       ELSE.
*         WRITE : / ‘CHEQUE’,WA_FILE-CHECT,’TO PAYT DOC’,WA_FILE-VBLNR,’IS NOT ASSIGNED.’ COLOR 3.
*       ENDIF.

ENDLOOP.

*      **ERROR HANDLING USING FM ‘FORMAT_MESSAGE’***************************************************

LOOP AT IT_BDCMSGCOLL INTO WA_BDCMSGCOLL.
CALL FUNCTION ‘FORMAT_MESSAGE’
EXPORTING
ID              SYMSGID
LANG            ‘-D’
NO              SYMSGNO
V1              SYMSGV1
V2              SYMSGV2
V3              SYMSGV3
V4              SYMSGV4
IMPORTING
MSG             V_MSG_TXT
EXCEPTIONS
NOT_FOUND       1
OTHERS          2
.
IF SYSUBRC 0.
WRITE/ V_MSG_TXT COLOR 2.
ENDIF.

ENDLOOP.
ENDIF.
ELSE.
MESSAGE ‘FILE PATH NOT FOUND’ TYPE ‘E’.
ENDIF.

***ERROR HANDLING USING METHOD/CLASS ‘FORMAT_MESSAGE’***

**LOOP AT IT_BDCMSGCOLL INTO WA_BDCMSGCOLL.
**Cl_demo_output+>display(it_bdcmsgcoll).
**
**ENDLOOP.

INCLUDE ZFCH5_CHECK_DYNPRO.

*&———————————————————————*
*&  Include  ZFCH5_CHECK_DYNPRO
*&———————————————————————*
FORM BDC_FIELD USING FP_FNAM FP_FVAL.
CLEAR  WA_BDCDATA.
WA_BDCDATAFNAM FP_FNAM.
WA_BDCDATAFVAL FP_FVAL.
APPEND WA_BDCDATA TO IT_BDCDATA.
ENDFORM.

INCLUDE ZFCH5_CHECK_FIELDS.

*&———————————————————————*
*&  Include  ZFCH5_CHECK_FIELDS
*&———————————————————————*
FORM BDC_DYNPRO USING FP_PROG FP_DYNPRO.
CLEAR  WA_BDCDATA.
WA_BDCDATAPROGRAM FP_PROG.
WA_BDCDATADYNPRO FP_DYNPRO.
WA_BDCDATADYNBEGIN ‘X’“NEW SCREEN
APPEND WA_BDCDATA TO IT_BDCDATA.
ENDFORM.

INCLUDE Z_F4_FILENAME.

*&———————————————————————*
*&  Include  Z_F4_FILENAME
*&———————————————————————*
FORM F4_FILENAME.
CALL FUNCTION ‘F4_FILENAME’
EXPORTING
PROGRAM_NAME        SYSTCPROG
DYNPRO_NUMBER       SYSTDYNNR
FIELD_NAME          ‘P_FILE’
IMPORTING
FILE_NAME           P_FILE
.
ENDFORM.

Assigned Tags

      11 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sandra Rossi
      Sandra Rossi

      BDC means Batch Data Communication, not Batch Data Conversion. Moreover, it was renamed Batch Input at least 20 years ago (the term BDC is still widely used though).

      Batch Input is a very old technology so many blog posts exist. Can you explain what yours add to others?

      You don't explain the intent of your blog post. Because of the title, I thought you would be talking about what is Batch Input, how to record a transaction and how to execute it, but you only talk about few differences between CALL TRANSACTION and Batch Input Session (which has been discussed a lot many years ago), what is SM35, and provide a code which works only for FCH5 (without any explanation by the way).

      You don't even talk about SHDB...

      So, I think you should name your blog post "Batch Input for FCH5", instead of misleading people with a generic attractive title.

      Other minor remarks: 1) batch Input is not only for data conversion, it can be used to automate any daily task. 2) The function module FORMAT_MESSAGE was superseded by ABAP statement MESSAGE (a long time ago). 3) Concerning the code you have posted, you should use the beautify function.

      Author's profile photo Muhammad Muzammil Khan
      Muhammad Muzammil Khan
      Blog Post Author

      Thanks for Correction

      Author's profile photo Muhammad Muzammil Khan
      Muhammad Muzammil Khan
      Blog Post Author

      At first, I appericate the time you took to read this article and provide you feedback.

      Mentioned about the SHDB recording.

      Also correct the Conversion to communication.

      The code for FCH5 is for reference, but the logic is generic you can use it for and Batch Input.

      The SM35 is to execute the The Batch session. And the code works with both Methods, Batch Session Method and Real-time batch Input (without creating Session).

      I have made few correction. And also will keep on updating the blog with new advancement as and when I explored them.

       

      Author's profile photo Jelena Perfiljeva
      Jelena Perfiljeva

      If you are quoting someone else in your blog text, make sure to use the quotation marks and note the source of the quote. It seems that Sandra's first sentence was just copy-pasted into the blog above without an attribution. It looks very confusing. If you made some changes in the blog based on the comments then just note that fact and thank the respective contributor. (You'll find some examples of such corrections in other SCN blogs.)

      Author's profile photo Matthew Billingham
      Matthew Billingham

      Obsolete function modules, acres of unannotated code using obsolete language components, significant portions of the code is commented out. SELECT within a loop. And global variables used with forms. Not exactly a showcase of best practice ABAP programming. And of a pretty much obsolete technique that really isn't necessary to know nowadays, and even if you should have to deal with it, there's already plenty of blogs and examples about it.

       

       

      Author's profile photo Mike Senikoglou
      Mike Senikoglou

      Wow someone is not in a good mood today!!! 🙂

      Author's profile photo Michelle Crapo
      Michelle Crapo

      In my mind - BDC is the last thing I always use.   You might want to mention LSMW, LTMOM, LTMC, BAPIs, and finally I believe there are still some SAP direct input programs for data loads.  And yes, in LSMW you can add your BDC code there.  That might be something you could add to this blog.  Or maybe point people to a second blog that talks about it.  Again there are a lot of blogs out there about the subject(s).  I sometimes have trouble searching when too many blogs come up.  If you are adding to what has already been written see above comment, it makes sense.

      In some versions of SAP, BDCs / Call transactions can't be used on certain transactions.

      Author's profile photo Muhammad Muzammil Khan
      Muhammad Muzammil Khan
      Blog Post Author

      Yes its Classic approach, I know there are many new approaches like LTMC, LSMW etc. but still one must know about it.

      In future,I will discuss about the new approaches too.

      Till then, OLD IS GOLD.

      Author's profile photo Jelena Perfiljeva
      Jelena Perfiljeva

      No, it's not "GOLD". It's an awful, error-prone tool of last resort that should've been dead by now.

      Author's profile photo Sandra Rossi
      Sandra Rossi

      I'd love it if SAP GUI Scripting could run on a server (right now, it can run on frontend, only). It's so much powerful. And then, bye bye Batch Input 🙂

      Author's profile photo Atiul Ehsan
      Atiul Ehsan

      A lot of thanks for sharing such a helpful document, there always some negative approach you will find in your career path to pull back from your desired goal, so just ignore those. Knowing Old things is not problematic it is a rather more efficient thing to face different real-life problems in different ways. So keep sharing good things with the community and good luck.