You are on page 1of 11

reating an ABAP program from a BDC recording

Added by Vaibhav Tiwari, last edited by Vaibhav Tiwari on Aug 03, 2007
Creating an ABAP program from a BDC recording
Step 1
Execute transaction recorder, transaction SHDB or recording button via transaction SM35.
Step 2
Enter name for recording (can be anything)
Step 3
Enter transaction to be executed
Step 4
The transaction will now be executed, simply perform the actions you want to record. This example
changes the Net price of item 1 on a purchase order (sets it to 20).
Step 5
Once you have finished the recording and selected the save button or exited the transaction you
Will be presented with the following information. This details the screens and fields that have been
Processed during the recording and will be used as a basis to create BDC ABAP program.
Source Code for BDC using Call Transaction
*Code used to create BDC
&---------------------------------------------------------------------
*& Report ZBDC_EXAMPLE *
*& *
&---------------------------------------------------------------------
*& Example BDC program, which updates net price of item 00010 of a *
*& particular Purchase order(EBELN). *
*& *
&---------------------------------------------------------------------
REPORT ZBDC_EXAMPLE NO STANDARD PAGE HEADING
LINE-SIZE 132.
*-----------------------------------------------------------------------
Data declaration
TABLES: ekko, ekpo.
TYPES: BEGIN OF t_ekko,
ebeln TYPE ekko-ebeln,
waers TYPE ekko-waers,
netpr TYPE ekpo-netpr,
err_msg(73) TYPE c,
END OF t_ekko.
DATA: it_ekko TYPE STANDARD TABLE OF t_ekko INITIAL SIZE 0,
wa_ekko TYPE t_ekko,
it_error TYPE STANDARD TABLE OF t_ekko INITIAL SIZE 0,
wa_error TYPE t_ekko,
it_success TYPE STANDARD TABLE OF t_ekko INITIAL SIZE 0,
wa_success TYPE t_ekko.
DATA: w_textout LIKE t100-text.
DATA: gd_update TYPE i,
gd_lines TYPE i.
*Used to store BDC data
DATA: BEGIN OF bdc_tab OCCURS 0.
INCLUDE STRUCTURE bdcdata.
DATA: END OF bdc_tab.
*Used to stores error information from CALL TRANSACTION Function Module
DATA: BEGIN OF messtab OCCURS 0.
INCLUDE STRUCTURE bdcmsgcoll.
DATA: END OF messtab.
*-----------------------------------------------------------------------
*Screen declaration
SELECTION-SCREEN BEGIN OF BLOCK block1 WITH FRAME
TITLE text-001. "Purchase order Num
SELECT-OPTIONS: so_ebeln FOR ekko-ebeln OBLIGATORY.
SELECTION-SCREEN END OF BLOCK block1.
SELECTION-SCREEN BEGIN OF BLOCK block2 WITH FRAME
TITLE text-002. "New NETPR value
PARAMETERS: p_newpr(14) TYPE c obligatory. "LIKE ekpo-netpr.
SELECTION-SCREEN END OF BLOCK block2.
************************************************************************
*START-OF-SELECTION
START-OF-SELECTION.
Retrieve data from Purchase order table(EKKO)
SELECT ekko~ebeln ekko~waers ekpo~netpr
INTO TABLE it_ekko
FROM ekko AS ekko INNER JOIN ekpo AS ekpo
ON ekpo~ebeln EQ ekko~ebeln
WHERE ekko~ebeln IN so_ebeln AND
ekpo~ebelp EQ '10'.
************************************************************************
*END-OF-SELECTION
END-OF-SELECTION.
Check data has been retrieved ready for processing
DESCRIBE TABLE it_ekko LINES gd_lines.
IF gd_lines LE 0.
Display message if no data has been retrieved
MESSAGE i003(zp) WITH 'No Records Found'(001).
LEAVE TO SCREEN 0.
ELSE.
Update Customer master data (instalment text)
LOOP AT it_ekko INTO wa_ekko.
PERFORM bdc_update.
ENDLOOP.
Display message confirming number of records updated
IF gd_update GT 1.
MESSAGE i003(zp) WITH gd_update 'Records updated'(002).
ELSE.
MESSAGE i003(zp) WITH gd_update 'Record updated'(003).
ENDIF.
Display Success Report
**********************
Check Success table
DESCRIBE TABLE it_success LINES gd_lines.
IF gd_lines GT 0.
Display result report column headings
PERFORM display_column_headings.
Display result report
PERFORM display_report.
ENDIF.
Display Error Report
********************
Check errors table
DESCRIBE TABLE it_error LINES gd_lines.
If errors exist then display errors report
IF gd_lines GT 0.
Display errors report
PERFORM display_error_headings.
PERFORM display_error_report.
ENDIF.
ENDIF.
&---------------------------------------------------------------------
*& Form DISPLAY_COLUMN_HEADINGS
&---------------------------------------------------------------------
Display column headings
----------------------------------------------------------------------
FORM display_column_headings.
WRITE:2 ' Success Report '(014) COLOR COL_POSITIVE.
SKIP.
WRITE:2 'The following records updated successfully:'(013).
WRITE:/ sy-uline(42).
FORMAT COLOR COL_HEADING.
WRITE:/ sy-vline,
(10) 'Purchase Order'(004), sy-vline,
(11) 'Old Netpr'(005), sy-vline,
(11) 'New Netpr'(006), sy-vline.
WRITE:/ sy-uline(42).
ENDFORM. " DISPLAY_COLUMN_HEADINGS
&---------------------------------------------------------------------
*& Form BDC_UPDATE
&---------------------------------------------------------------------
Populate BDC table and call transaction ME22
----------------------------------------------------------------------
FORM bdc_update.
PERFORM dynpro USING:
'X' 'SAPMM06E' '0105',
' ' 'BDC_CURSOR' 'RM06E-BSTNR',
' ' 'RM06E-BSTNR' wa_ekko-ebeln,
' ' 'BDC_OKCODE' '/00', "OK code
'X' 'SAPMM06E' '0120',
' ' 'BDC_CURSOR' 'EKPO-NETPR(01)',
' ' 'EKPO-NETPR(01)' p_newpr,
' ' 'BDC_OKCODE' '=BU'. "OK code
Call transaction to update customer instalment text
CALL TRANSACTION 'ME22' USING bdc_tab MODE 'N' UPDATE 'S'
MESSAGES INTO messtab.
Check if update was succesful
IF sy-subrc EQ 0.
ADD 1 TO gd_update.
APPEND wa_ekko TO it_success.
ELSE.
Retrieve error messages displayed during BDC update
LOOP AT messtab WHERE msgtyp = 'E'.
Builds actual message based on info returned from Call transaction
CALL FUNCTION 'MESSAGE_TEXT_BUILD'
EXPORTING
msgid = messtab-msgid
msgnr = messtab-msgnr
msgv1 = messtab-msgv1
msgv2 = messtab-msgv2
msgv3 = messtab-msgv3
msgv4 = messtab-msgv4
IMPORTING
message_text_output = w_textout.
ENDLOOP.
Build error table ready for output
wa_error = wa_ekko.
wa_error-err_msg = w_textout.
APPEND wa_error TO it_error.
CLEAR: wa_error.
ENDIF.
Clear bdc date table
CLEAR: bdc_tab.
REFRESH: bdc_tab.
ENDFORM. " BDC_UPDATE
---------------------------------------------------------------------
FORM DYNPRO *
---------------------------------------------------------------------
stores values to bdc table *
---------------------------------------------------------------------
--> DYNBEGIN *
--> NAME *
--> VALUE *
---------------------------------------------------------------------
FORM dynpro USING dynbegin name value.
IF dynbegin = 'X'.
CLEAR bdc_tab.
MOVE: name TO bdc_tab-program,
value TO bdc_tab-dynpro,
'X' TO bdc_tab-dynbegin.
APPEND bdc_tab.
ELSE.
CLEAR bdc_tab.
MOVE: name TO bdc_tab-fnam,
value TO bdc_tab-fval.
APPEND bdc_tab.
ENDIF.
ENDFORM. " DYNPRO
&---------------------------------------------------------------------
*& Form DISPLAY_REPORT
&---------------------------------------------------------------------
Display Report
----------------------------------------------------------------------
FORM display_report.
FORMAT COLOR COL_NORMAL.
Loop at data table
LOOP AT it_success INTO wa_success.
WRITE:/ sy-vline,
(10) wa_success-ebeln, sy-vline,
(11) wa_success-netpr CURRENCY wa_success-waers, sy-vline,
(11) p_newpr, sy-vline.
CLEAR: wa_success.
ENDLOOP.
WRITE:/ sy-uline(42).
REFRESH: it_success.
FORMAT COLOR COL_BACKGROUND.
ENDFORM. " DISPLAY_REPORT
&---------------------------------------------------------------------
*& Form DISPLAY_ERROR_REPORT
&---------------------------------------------------------------------
Display error report data
----------------------------------------------------------------------
FORM display_error_report.
LOOP AT it_error INTO wa_error.
WRITE:/ sy-vline,
(10) wa_error-ebeln, sy-vline,
(11) wa_error-netpr CURRENCY wa_error-waers, sy-vline,
(73) wa_error-err_msg, sy-vline.
ENDLOOP.
WRITE:/ sy-uline(104).
REFRESH: it_error.
ENDFORM. " DISPLAY_ERROR_REPORT
&---------------------------------------------------------------------
*& Form DISPLAY_ERROR_HEADINGS
&---------------------------------------------------------------------
Display error report headings
----------------------------------------------------------------------
FORM display_error_headings.
SKIP.
WRITE:2 ' Error Report '(007) COLOR COL_NEGATIVE.
SKIP.
WRITE:2 'The following records failed during update:'(008).
WRITE:/ sy-uline(104).
FORMAT COLOR COL_HEADING.
WRITE:/ sy-vline,
(10) 'Purchase Order'(009), sy-vline,
(11) 'Netpr'(010), sy-vline,
(73) 'Error Message'(012), sy-vline.
WRITE:/ sy-uline(104).
FORMAT COLOR COL_NORMAL.
ENDFORM. " DISPLAY_ERROR_HEADINGS
Date format for entry into BDC
Because a date field is stored and displayed in different formats within SAP the following code is
required to convert the date into a format which can be input onto a screen field.
Date field
data: ld_date(8).
ld_date(2) = sy-datum+6(2).
ld_date+2(2) = sy-datum+4(2).
ld_date+4(4) = sy-datum(4).


General Flow of a BDC Program
General flow of a BDC program is like this....
1) First create recording for the T code which you want to make BDC for... Use T
code SHDB for recording.
2 ) Now save that recording and create pogram from that recording using Create
Program button. give the BDC driver program name and create.
3 ) Now the general logic of BDC program goes like this....
- Upload Flat file into and internal table using function module "UPLOAD"
- OPEN BDC GROUP.
- Now loop at that internal table which contains the data from flat file.
- move data from internal table to fields of BDCDATA using automatically
gebnerated code from BDC.
- CALL TRANSACTION <T CODE> using BDCDATA...
- CLOSE BDC GROUP
A sample program for the same is attatched here for your referance... just go through
it..
report ztej_test_new no standard page heading line-size 255.

data bdcdata like bdcdata occurs 0 with header line.
tables: zipcldesigcat.

data : begin of itab occurs 0,
mandt like zipcldesigcat-mandt,
zdesigncd like zipcldesigcat-zdesigncd,
zdesignation like zipcldesigcat-zdesignation,
zdesigcat like zipcldesigcat-zdesigcat,
end of itab.

*INCLUDE bdcrecx1.

start-of-selection.
perform upload.
perform open.

loop at itab.
perform move.
call transaction 'SE16' using bdcdata mode 'E'.
refresh bdcdata.
endloop.
perform close.

*&---------------------------------------------------------------------*
*& Form UPLOAD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form upload.
call function 'UPLOAD'
exporting
* CODEPAGE = ''
filename = ''
filetype = 'DAT'
item = 'Your File'
* FILEMASK_MASK = ' '
* FILEMASK_TEXT = ' '
* FILETYPE_NO_CHANGE = ' '
* FILEMASK_ALL = ' '
* FILETYPE_NO_SHOW = ' '
* LINE_EXIT = ' '
* USER_FORM = ' '
* USER_PROG = ' '
* SILENT = 'S'
* IMPORTING
* FILESIZE =
* CANCEL =
* ACT_FILENAME =
* ACT_FILETYPE =
tables
data_tab = itab
exceptions
conversion_error = 1
invalid_table_width = 2
invalid_type = 3
no_batch = 4
unknown_error = 5
gui_refuse_filetransfer = 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.

*CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
* EXPORTING
* filename = 'C:\TEST.XLS'
* i_begin_col = 1
* i_begin_row = 1
* i_end_col = 3
* i_end_row = 5
* tables
* intern = ITAB
* EXCEPTIONS
* INCONSISTENT_PARAMETERS = 1
* UPLOAD_OLE = 2
* OTHERS = 3
* .
*IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
*ENDIF.
*
endform. " UPLOAD
*----------------------------------------------------------------------*
* Start new screen *
*----------------------------------------------------------------------*
form bdc_dynpro using program dynpro.
clear bdcdata.
bdcdata-program = program.
bdcdata-dynpro = dynpro.
bdcdata-dynbegin = 'X'.
append bdcdata.
endform.

*----------------------------------------------------------------------*
* Insert field *
*----------------------------------------------------------------------*
form bdc_field using fnam fval.
if fval <> space.
clear bdcdata.
bdcdata-fnam = fnam.
bdcdata-fval = fval.
append bdcdata.
endif.
endform.
*&---------------------------------------------------------------------*
*& Form OPEN
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form open.
call function 'BDC_OPEN_GROUP'
exporting
client = sy-mandt
* DEST = FILLER8
group = 'ZDESIGCAT_R'
* HOLDDATE = FILLER8
* KEEP = FILLER1
user = sy-uname
* RECORD = FILLER1
* 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.

endform. " OPEN

*&---------------------------------------------------------------------*
*& Form MOVE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form move.

perform bdc_dynpro using 'SAPLSETB' '0230'.
perform bdc_field using 'BDC_CURSOR'
'DATABROWSE-TABLENAME'.
perform bdc_field using 'BDC_OKCODE'
'=ANLE'.
perform bdc_field using 'DATABROWSE-TABLENAME'
'ZIPCLDESIGCAT'.
perform bdc_dynpro using 'SAPLZIPCLDESIGCAT' '0001'.
perform bdc_field using 'BDC_CURSOR'
'VIM_POSITION_INFO'.
perform bdc_field using 'BDC_OKCODE'
'=NEWL'.
perform bdc_dynpro using 'SAPLZIPCLDESIGCAT' '0001'.
perform bdc_field using 'BDC_CURSOR'
'ZIPCLDESIGCAT-ZDESIGCAT(01)'.
perform bdc_field using 'BDC_OKCODE'
'=SAVE'.
perform bdc_field using 'ZIPCLDESIGCAT-ZDESIGNCD(01)'
'2101'.
perform bdc_field using 'ZIPCLDESIGCAT-ZDESIGNATION(01)'
'new'.
perform bdc_field using 'ZIPCLDESIGCAT-ZDESIGCAT(01)'
'n'.
perform bdc_dynpro using 'SAPLZIPCLDESIGCAT' '0001'.
perform bdc_field using 'BDC_CURSOR'
'ZIPCLDESIGCAT-ZDESIGNCD(02)'.
perform bdc_field using 'BDC_OKCODE'
'=ENDE'.
perform bdc_dynpro using 'SAPLSETB' '0230'.
perform bdc_field using 'BDC_OKCODE'
'/EEND'.
perform bdc_field using 'BDC_CURSOR'
'DATABROWSE-TABLENAME'.


endform. " MOVE

*&---------------------------------------------------------------------*
*& Form CLOSE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form close.
call function 'BDC_CLOSE_GROUP'
exceptions
not_open = 1
queue_error = 2
others = 3.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

endform. " CLOS

You might also like