Call Transaction Method in SAP BDC

by | Dec 29, 2022 | ABAP, ABAP Beginner, SAP

Home » SAP » ABAP » ABAP Beginner » Call Transaction Method in SAP BDC

Introduction

A Call transaction is a very simple report program. Once you execute and come out of the program, there is no log to cross check.

Syntax:

CALL TRANSACTION <transaction code> USING <it_bdcdata>
MODE ‘A/E/N’
UPDATE ‘A/S/L’
MESSAGE INTO <it_msg_details>.

Here, it_bdcdata represents the internal table that has instructions for one time execution of the t-code. This is the same table that we discussed in the section “SAP BDC Technical Details”. So, this internal table at one moment will hold instructions for only one record.

Here, we have three different options for MODE. The modes determine the control over screen display.

  • A: It means, while execution of the program, all screens will be displayed. It is best suited for testing purpose as it is a foreground testing.
  • N: It means, while execution of the program, no screens will be shown even in case of success or error. This is best suited for production execution as it is a background job.
  • E: It means, the screens with message will be displayed every time there is an error. This is also used for testing purpose, in case SY-SUBRC fails for method N, and you are not able to identify the position of error.

Here, we have three different options for UPDATE. The update command signifies Create, Update and delete operation over table/database. And, this UPDATE command provided the control over the sequence of database operations. Like, if data will be saved for all external file one by one, or in parallel.

  • A: It means Asynchronous. It means irrespective of the face that first set of data was processed and data was being saved in db, the next round of data process will start and not wait for the completion of first. This also means for large amount of data, the speed of the program will be faster in this mode. But, also there is a very rare chance of getting errors.
  • S: It means Synchronous. It means, every time the command CALL TRANSACTION is executed, until the data is saved in table and a success SY-SUBRC comes, it will not execute the CALL TRANSACTION for the next set of data. It is preferred in Production mode, as it is error free (Although slower).
  • L: It means Local. This is the command which signifies that the update for entire data (whole excel or csv or txt) will be executed at once, in the end, and only after the “COMMIT WORK” statement. The use case for Local Update or Update Function Module is where you either want all data to updated or none. The best use case is updating header and detail, and if detail or header fails, none of these tables will be updated. You can read more about it here.

Here, it_msg_details saves all the messages that are produced over the screen (success, warning, information or error).

Call Transaction Method Flow

As you can see in the above design flow, the excel will be converted into internal table, and then BDC Codes (CALL TRANSACTION) will be called where <it_bdcdata> will be in form of BDCDATA format with various fields such as Program, DYNPRO, etc including field and value holding data of first row of the excel. Once the database process completes, the next data of the excel will be processed and so on.

Steps to upload data in SAP system using BDC

To understand the above process, we will show case a simple example where we will:

1. Create multiple tables for Employee Data that can be created using a report.

We have created these tables: zBarry_emp (for employee basic information), zBarry_sal (for employee Salary information) and zBarry_add (for employee address details). The fields are as shown below:

zBarry_emp

Field NameField Type
EMPID (Key)INT4
FNAMECHAR20
LNAMECHAR20
PHONECHAR20

zBarry_sal

Field NameField Type
EMPID (Key)INT4
SALARYCHAR20

zBarry_add

Field NameField Type
EMPID (Key)INT4
CITYCHAR20
PINCHAR20
STATECHAR20
COUNTRYCHAR20

2. The report will be able to take many fields as input.

Now, we will create a report ZBARRYEMP_REPORT to take multiple inputs via screen and save them in the above tables. The code of the same is shown below:

REPORT ZBARRYEMP_REPORT.
Data: ls_emp type ZBARRY_EMP,
      ls_sal type zbarry_sal,
      ls_add type zbarry_add,
      lv_MSG type char20.
PARAMETERS: p_empid TYPE int4,
p_fname type char20,
p_lname type char20,
p_phone type char20,
p_salary type char20,
p_city type char20,
p_pin type char20,
p_state type char20,
p_con type char20.
" mapping for Employee Info
ls_emp-empid = p_empid.
ls_emp-fname = p_fname.
ls_emp-lname = p_lname.
ls_emp-phone = p_phone.

" mapping for Employee Salary
ls_sal-empid = p_empid.
ls_sal-salary = p_salary.
" mapping for Employee Address
ls_add-empid = p_empid.
ls_add-city = p_city.
ls_add-pin = p_pin.
ls_add-state = p_state.
ls_add-country = p_con.
" SQL Operations.
INSERT ZBARRY_EMP FROM ls_emp.
If Sy-subrc = 0.
lv_MSG = 'Create is successful'.
else.
  lv_MSG = 'Create Failed'.
       Endif.
 INSERT ZBARRY_sal FROM ls_sal.
If Sy-subrc = 0.
lv_MSG = 'Create is successful'.
else.
  lv_MSG = 'Create Failed'.
       Endif.
  INSERT ZBARRY_add FROM ls_add.
If Sy-subrc = 0.
lv_MSG = 'Create is successful'.
else.
  lv_MSG = 'Create Failed'.
       Endif.
       if lv_MSG EQ 'Create Failed'.
         MESSAGE lv_MSG TYPE 'E' DISPLAY LIKE 'E'.
           ENDIF.

 

Output:

ABAP Report for BDC

3. Then, we will create a t-code for our report.

We will create a T-code “ZMYTCODE” using SE93 for our report “ZBARRYEMP_REPORT”.

Tcode for BDC

Steps to Record a transaction code using BDC

4. Thereafter, we will run a BDC recording (using T-Code SHDB) and get BDCDATA from that recorder.

Step 1. Go to t-code SHDB and click on “New Recording”

SHDB New Recording

Step 2: Provide T-Code, any name for recording and set update mode as “Synchronous”.

Create Recording SHDB

Step 3: It will open your Report. Now, provide all the data, and press execute.

BDC Recorder

Now click, back to see your BDCDATA, for our example it looks like this:

INDEXPROGRAM DYNPRODYNBEGINFNAM FVAL
1TZMYTCODE
2ZBARRYEMP_REPORT1000X
3BDC_CURSORP_CON
4BDC_OKCODE=ONLI
5P_EMPID12
6P_FNAMERudra
7P_LNAMEPandey
8P_PHONE99999999
9P_SALARYINR 9999999
10P_CITYMughalsarai
11P_PIN232101
12P_STATEUttar Pradesh
13P_CONIndia
14ZBARRYEMP_REPORT1000X
15BDC_OKCODE/EE
16BDC_CURSORP_EMPID

Now, here you can see that all our entered data is also recorded in the recording.

Here BDC_OKCODE “=ONLI” is for our “Execute” button, and BDC_OKCODE = “/EE” is showcasing that we pressed back. We will use the code until we press execute.

You can test your recording by clicking “Process” button:

Test BDC

While testing, do change data with a new set. Before exiting, you must save your recording.

Steps to Create a BDC Program

5. Now, we will create a program that accepts excel input and convert that excel into an internal table.

Step 1: Create a new ABAP report “ZBARRYEMP_BDC” using t-code SE38.

Step 2: Create the following:

  • local structure of type “bdcdata” where we will map our excel data with bdcdata that we recorded earlier
  • local internal table of type “bdcdata” where we will push our local structure that we have created above
  • local structure and internal table of type “bdcmsgcoll” where we will save the message for every call that we make to the recording
  • local internal table and local structure to hold excel data that we will upload

We have created the following:

REPORT ZBARRYEMP_BDC.
" local structure with combined fields of emp table, salary table and address table
TYPES: BEGIN OF ls_emp,
  empid type INT4,
  fname type char20,
  lname type char20,
  phone type char20,
  salary type char20,
  city type char20,
  pin type char20,
  state type char20,
  country type char20,
  END OF ls_emp.
DATA: ls_bdcdata TYPE bdcdata,
      ls_msg type bdcmsgcoll.
DATA: lt_bdcdata TYPE TABLE OF bdcdata,
      lt_bdcmsg TYPE TABLE OF bdcmsgcoll,
      lt_bankdata TYPE TABLE OF ls_emp.

 

Step 3: Now, we will import the excel data using Function Module ‘TEXT_CONVERT_XLS_TO_SAP’ and push it to our internal table it_empdata. You can read more about Excel upload in ABAP here.

" Uploading Excel and converting it into Interna Table
TYPE-POOLS truxs.
PARAMETERS p_file TYPE rlgrap-filename.
DATA : t_upload  TYPE STANDARD TABLE OF ls_emp,
       wa_upload TYPE ls_emp,
       it_type   TYPE truxs_t_text_data.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      field_name = 'p_file'
    IMPORTING
      file_name  = p_file.
START-OF-SELECTION.
  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
      i_tab_raw_data       = it_type
      i_filename           = p_file
    TABLES
      i_tab_converted_data = t_upload[]
    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 .
  ENDIF.
END-OF-SELECTION.

 

Step 4: Now in a loop we will keep pushing the bdcdata parameters that is

  • PROGRAM, DYNPRO, DYNBEGIN: These will remain constant for one screen (in our case, constant for whole program as we have only one screen). These will be taken from the BDCDATA that we have generated i.e.
ZBARRYEMP_REPORT1000X
  • FNAM, FVAL: These will hold the key and values (In our case, the keys are the parameters that we used for input in our ABAP report “ZBARRYEMP_REPORT” while the values will be taken from excel upload.
  • The first two fields for a screen are always BDC_CURSOR  and BDC_OKCODE.  The value can be retrieved from the generated BDCDATA. In this case, the values are ‘P_CON ‘ and ‘=ONLI’, respectively.

 

Step 5: Once the values are taken as per BDCDATA structure, we call the “CALL TRANSACTION” statement. These entire statements will be looped until the operation finishes for the entire excel data.

The syntax for CALL TRANSACTION is:

CALL TRANSACTION <transaction code> USING <it_bdcdata>
MODE ‘A/E/N’
UPDATE ‘A/S/L’
MESSAGES INTO <it_msg_details>.

 

For our example, the above value will become:

CALL TRANSACTION ‘ZMYTCODE’ USING lt_bdcdata
MODE ‘N’
UPDATE ‘S
MESSAGES INTO lt_bdcmsg.

 

Step 6: Now upload an excel file with the given format. In our example, we have an excel as shown below:

Excel format BDC

Note: Do not include the header. The excel should look like this:

Excel with Data for BDC

Once you are done with the ABAP Changes, then run the report, upload the excel file and once the ABAP Report executes, check the table to see if all the records are uploaded or not.

ABAP Code for BDC Program

REPORT ZBARRYEMP_BDC.
" local structure with combined fields of emp table, salary table and address table
TYPES: BEGIN OF ls_emp,
  empid type int2,
  fname type char20,
  lname type char20,
  phone type char20,
  salary type char20,
  city type char20,
  pin type char20,
  state type char20,
  country type char20,
  END OF ls_emp.
DATA: ls_bdcdata TYPE bdcdata,
      ls_msg type bdcmsgcoll.
DATA: lt_bdcdata TYPE TABLE OF bdcdata,
      lt_bdcmsg TYPE TABLE OF bdcmsgcoll,
      lt_empdata TYPE TABLE OF ls_emp.
" Uploading Excel and converting it into Interna Table
TYPE-POOLS truxs.
PARAMETERS p_file TYPE rlgrap-filename.
DATA : t_upload  TYPE STANDARD TABLE OF ls_emp,
       wa_upload TYPE ls_emp,
       it_type   TYPE truxs_t_text_data.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      field_name = 'p_file'
    IMPORTING
      file_name  = p_file.
START-OF-SELECTION.
  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
      i_tab_raw_data       = it_type
      i_filename           = p_file
    TABLES
      i_tab_converted_data = t_upload[]
    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 .
  ENDIF.
  " Loop the above table t_upload[] data until all data is uploaded via BDC
  LOOP AT t_upload into wa_upload.
* Screen 1 - Field 1 is always BDC_CURSOR
ls_bdcdata-PROGRAM = 'ZBARRYEMP_REPORT'.
ls_bdcdata-DYNPRO = '1000'.
ls_bdcdata-DYNBEGIN = 'X'.
ls_bdcdata-fnam = 'BDC_CURSOR'.
ls_bdcdata-fval = 'P_CON'.
APPEND ls_bdcdata to lt_bdcdata.
CLEAR ls_bdcdata.
* Screen 1 - Field 2 - Always BDC_OKCODE
ls_bdcdata-fnam = 'BDC_OKCODE'.
ls_bdcdata-fval = '=ONLI'.
APPEND ls_bdcdata to lt_bdcdata.
CLEAR ls_bdcdata.
* Screen 1 - Field 3
ls_bdcdata-fnam = 'P_EMPID'.
ls_bdcdata-fval = wa_upload-empid.
APPEND ls_bdcdata to lt_bdcdata.
CLEAR ls_bdcdata.
* Screen 1 - Field 4
ls_bdcdata-fnam = 'P_FNAME'.
ls_bdcdata-fval = wa_upload-fname.
APPEND ls_bdcdata to lt_bdcdata.
CLEAR ls_bdcdata.
* Screen 1 - Field 5
ls_bdcdata-fnam = 'P_LNAME'.
ls_bdcdata-fval = wa_upload-lname.
APPEND ls_bdcdata to lt_bdcdata.
CLEAR ls_bdcdata.
* Screen 1 - Field 6
ls_bdcdata-fnam = 'P_PHONE'.
ls_bdcdata-fval = wa_upload-phone.
APPEND ls_bdcdata to lt_bdcdata.
CLEAR ls_bdcdata.
* Screen 1 - Field 7
ls_bdcdata-fnam = 'P_SALARY'.
ls_bdcdata-fval = wa_upload-salary.
APPEND ls_bdcdata to lt_bdcdata.
CLEAR ls_bdcdata.
* Screen 1 - Field 8
ls_bdcdata-fnam = 'P_CITY'.
ls_bdcdata-fval = wa_upload-city.
APPEND ls_bdcdata to lt_bdcdata.
CLEAR ls_bdcdata.
* Screen 1 - Field 9
ls_bdcdata-fnam = 'P_PIN'.
ls_bdcdata-fval = wa_upload-pin.
APPEND ls_bdcdata to lt_bdcdata.
CLEAR ls_bdcdata.
* Screen 1 - Field 10
ls_bdcdata-fnam = 'P_STATE'.
ls_bdcdata-fval = '=ONLI'.
APPEND ls_bdcdata to lt_bdcdata.
CLEAR ls_bdcdata.
* Screen 1 - Field 10
ls_bdcdata-fnam = 'P_CON'.
ls_bdcdata-fval = wa_upload-country.
APPEND ls_bdcdata to lt_bdcdata.
CLEAR ls_bdcdata.
ENDLOOP.
"Call BDC Transaction
CALL TRANSACTION 'ZMYTCODE' USING lt_bdcdata MODE 'A' UPDATE 'S' MESSAGES INTO lt_bdcmsg.
CLEAR lt_bdcdata.
END-OF-SELECTION.

 

Output

BDC Program Output

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author