‎2007 Oct 11 6:10 AM
Hi,
I want to learn how to upload data using BDC. If I have a excel data file how will upload it using BDC. I don't know the full process of doing it, if someone help me in this. I want it using BDC session process and processing that session. Plz help.
Thanks.
‎2007 Oct 11 6:17 AM
for writing any sort of BDC you'l first need Recording of perticular TCODE...
first create one recording of desired TCODE via SHDB Tcode....
then use following template to complete your BDC....
REPORT ZBDC_CHANGE_VENDOR_DETAILS .
<b>1. CREATE INTERNAL TABLE CONTAINING BDC FIELDS
(Pick this data from recording)</b>
data: begin of it_upload occurs 0 with header line,
"Define all the fields used in the recording
end of it_upload.
Batchinputdata of single transaction
data: bdcdata like bdcdata occurs 0 with header line.
*----
*Variables
*----
data: return(30).
*----
*Selection Screens
*----
selection-screen begin of block b1 with frame title text-000.
<b>*Give filename here.. file to be uploaded</b>
parameters p_fname like rlgrap-filename.
selection-screen end of block b1.
selection-screen begin of block b2 with frame title text-001.
parameters : p_sname(12),
p_keep default 'X' as checkbox.
selection-screen end of block b2.
*----
*At Selection-Screen
*----
at selection-screen on value-request for p_fname.
call function 'WS_FILENAME_GET'
exporting
DEF_FILENAME = ' '
DEF_PATH = ' '
mask = ',*.txt.' <b>".txt for notepad file and .xls for excel file</b>
mode = 'O'
TITLE = ' '
importing
filename = p_fname
RC =
exceptions
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
others = 5
.
if sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
endif.
at selection-screen.
call function 'WS_QUERY'
exporting
ENVIRONMENT =
filename = p_fname
query = 'FE'
WINID =
importing
return = return
exceptions
inv_query = 1
no_batch = 2
frontend_error = 3
others = 4
.
if sy-subrc <> 0.
elseif return = 0.
message e005 with 'File does not exist'.
endif.
*----
*Start-of-selection
*----
start-of-selection.
*Uploading the flat file containig the infotype records.
call function 'WS_UPLOAD'
exporting
CODEPAGE = ' '
filename = p_fname
filetype = 'DAT'
HEADLEN = ' '
LINE_EXIT = ' '
TRUNCLEN = ' '
USER_FORM = ' '
USER_PROG = ' '
DAT_D_FORMAT = ' '
IMPORTING
FILELENGTH =
tables
data_tab = it_upload
exceptions
conversion_error = 1
file_open_error = 2
file_read_error = 3
invalid_type = 4
no_batch = 5
unknown_error = 6
invalid_table_width = 7
gui_refuse_filetransfer = 8
customer_error = 9
others = 10
.
if sy-subrc <> 0.
message e005 with 'File upload failed'.
endif.
*Opening the session
call function 'BDC_OPEN_GROUP'
exporting
client = sy-mandt
group = p_sname
keep = p_keep
user = sy-uname
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 e005 with 'Unable to open a BDC group'.
endif.
loop at it_upload.
<b>2. Perform BDC_DYNPRO & BDC_FIELD for each screen & its fields...Eliminate those fields which are not necessary.OKCODE is mandatory
(Pick this data from recording)</b>*perform insert session details
"This code is example .. and the values mentioned in '' are from recording...
perform bdc_dynpro using 'SAPMF02K' '0106'.
perform bdc_field using:
'BDC_CURSOR' 'RF02K-D0130',
'BDC_OKCODE' '/00',
'RF02K-LIFNR' it_upload-vendor_code,
'RF02K-BUKRS' it_upload-company_code,
'RF02K-D0110' 'X',
'RF02K-D0120' 'X',
'RF02K-D0130' 'X'.
<b>3. Perform BDC_INSERT here b4 endloop</b>
CALL FUNCTION 'BDC_INSERT'
EXPORTING
TCODE = 'FBD1' put ur TCODE 4 whch u r doin BDC
POST_LOCAL = NOVBLOCAL
PRINTING = NOPRINT
SIMUBATCH = ' '
CTUPARAMS = ' '
TABLES
dynprotab = 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 bdcdata.
endloop.
<b>*Closing the session</b>
call function 'BDC_CLOSE_GROUP'
exceptions
not_open = 1
queue_error = 2
others = 3.
if sy-subrc <> 0.
message e005 with 'Unable to close the BDC group'.
endif.
----
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.
clear bdcdata.
bdcdata-fnam = fnam.
bdcdata-fval = fval.
append bdcdata.
endform.
‎2007 Oct 11 6:13 AM
Hi,
Generally how the people do is
if they want to upload the data from excelfile. They will save the data in Tab Delimited option while saving the file u can see type of file.
U also have a function module
ALSM_EXCEL_TO_INTERNAL_TABLE
Assign points if useful.
‎2007 Oct 11 6:13 AM
Hi
<b>the BDC program should be in this format</b>
Transaction Recorder (SHDB)
How to Upload Presentation Server Flat file to SAP R/3 system???
How to upload application server file to R/3 system?
Definition
Example - Call Transaction Method
<b>Transaction Recorder (SHDB)</b>
Before you work with the Batch Input methods, you should know the purpose of the tool
Transaction Recorder.
<b>Use:</b>
You can use the transaction recorder to record a series of transactions and their screens.
<b>Features:</b>
You can use the recording to create
Data transfer programs that use batch input or CALL TRANSACTION
Batch input sessions
Test data
Function modules.
Note: It doesnt record F1, F4 and Scrollbar movements
<b>Upload Flat file from Presentation Server to SAP R/3</b>
CALL FUNCTION GUI_UPLOAD'
EXPORTING
CODEPAGE = IBM'
FILENAME = P_UFILE
FILETYPE = 'DAT'
TABLES
DATA_TAB = INT_TAB
EXCEPTIONS
CONVERSION_ERROR = 1
FILE_OPEN_ERROR = 2
FILE_READ_ERROR = 3
INVALID_TYPE = 4
NO_BATCH = 5
UNKNOWN_ERROR = 6
INVALID_TABLE_WIDTH = 7
GUI_REFUSE_FILETRANSFER = 8
CUSTOMER_ERROR = 9
OTHERS = 10 .
IF SY-SUBRC NE 0.
MESSAGE E999(FR) WITH 'ERROR IN FILE UPLOAD'.
ENDIF.
<b>Upload file from application server to SAP R/3</b>
Open the the application server file
OPEN DATASET <dsn> FOR INPUT <mode>
Read the data from application server file
READ DATASET <dsn> INTO <wa>
And then close the application server file
CLOSE DATASET <dsn>
<b>Definition- Declaring BDC Table</b>
DATA: BDC_TAB LIKE STANDARD TABLE OF
BDCDATA INITIAL SIZE 6
WITH HEADER LINE .
The internal table used to collect the transactions information must be declared LIKE BDCDATA.
Filling BDC Table Method #1
FORM FILL_BDC_TAB.
REFRESH BDC_TAB.
CLEAR BDC_TAB.
BDC_TAB-PROGRAM = SAPMF02K.
BDC_TAB-DYNPRO = 01016.
BDC_TAB-DYNBEGIN = X.
APPEND BDC_TAB.
CLEAR BDC_TAB.
BDC_TAB-FNAM = RF02K-LIFNR.
BDC_TAB-FVAL = TEST1.
APPEND BDC_TAB.
CLEAR BDC_TAB.
BDC_TAB-FNAM = RF02K-D0010.
BDC_TAB-FVAL = X.
APPEND BDC_TAB.
CLEAR BDC_TAB.
BDC_TAB-PROGRAM = SAPMF02K.
BDC_TAB-DYNPRO = 0110.
BDC_TAB-DYNBEGIN = X.
APPEND BDC_TAB.
CLEAR BDC_TAB.
BDC_TAB-FNAM = LFA1-STRAS.
BDC_TAB-FVAL = 123 Main St..
APPEND BDC_TAB.
CLEAR BDC_TAB.
BDC_TAB-FNAM = BDC_OKCODE.
BDC_TAB-FVAL = /11.
APPEND BDC_TAB.
ENDFORM.
Filling BDC Table Method #2
FORM FILL_BDC_TAB.
REFRESH BDC_TAB.
PERFORM POPULATE_BDC_TAB
USING:
1 SAPMF02K 0106,
RF02K-LIFNR TEST1,
RF02K-D0010 X,
1 SAPMF02K 0110,
LFA1-STRAS, 123 Main St.,
BDC_OKCODE, /11.
ENDFORM.
FORM POPULATE_BDC_TAB USING FLAG VAR1 VAR2.
CLEAR BDC_TAB.
IF FLAG = 1.
BDC_TAB-PROGRAM = VAR1.
BDC_TAB-DYNPRO = VAR2..
BDC_TAB-DYNBEGIN = X.
ELSE.
BDC_TAB-FNAM = VAR1.
BDC_TAB-FVAL = VAR2.
ENDIF.
APPEND BDC_TAB.
ENDFORM.
This two subroutine method to fill the BDC table is preferable because the POPULATE_BDC_TABLE subroutine is reusable throughout all batch input programs.
<b>Example #1 - Change Vendor (Call Transaction Method)</b>
<b>Example #1- Declaration Section</b>
REPORT Y180DM10.
DATA: BDC_TAB LIKE STANDARD TABLE OF
BDCDATA INITIAL SIZE 6 WITH HEADER LINE.
INFILE(20) VALUE /tmp/bc180_file4.
DATA: BEGIN OF INREC.
VENDNUM LIKE LFA1-LIFNR.
STREET LIKE LFA1-STRAS.
END OF INREC.
PARAMETERS: DISPMODE DEFAULT A,
UPDAMODE DEFAULT S.
START-OF-SELECTION.
OPEN DATASET INFILE
FOR INPUT IN TEXT MODE.
DO.
READ DATASET INFILE INTO INREC.
IF SY-SUBRC < > 0. EXIT. ENDIF.
PERFORM FILL_BDC_TAB.
CALL TRANSACTION FK02
USING BDC_TAB
MODE DISPMODE
UPDATE UPDAMODE.
IF SY-SUBRC < > 0.
WRITE: /ERROR.
ENDIF.
ENDDO.
CLOSE DATASET INFILE.
<b>synchronous updating</b>
DO.
PERFORM FILL_BDC_TAB.
CALL TRANSACTION FK02
USING BDC_TAB
MODE N
UPDATE S.
IF SY-SUBRC < > 0.
WRITE: /ERROR.
ENDIF.
ENDDO.
With synchronous updating, we can check SY-SUBRC to determine the success of the transaction and the actual update to the database.
<b>asynchronous updating</b>
DO.
PERFORM FILL_BDC_TAB.
CALL TRANSACTION FK02
USING BDC_TAB
MODE N
UPDATE A.
IF SY-SUBRC < > 0.
WRITE: /ERROR.
ENDIF.
ENDDO.
With asynchronous updating, we can check SY-SUBRC to determine the success of the transaction only, not the actual update to the database.
<b>Error Handling</b>
Write an error report.
Send the record(s) in error to an error file.
<b>Create a batch input session with the record(s) in error.</b>
To store error messages ( CALL TRANSACTION )
data: begin of Tab_Mess occurs 0.
include structure bdcmsgcoll.
data : end of Tab_Mess,
CALL TRANSACTION FK02 USING BDC_TAB MODE N UPDATE S
MESSAGES INTO TAB_MESS.
IF SY-SUBRC NE 0.
WRITE: / Tab_MESS-TCODE, Tab_MESS-DYNUMB, Tab_MESS-MSGTYP ,
Tab_MESS-MSGID.
ENDIF.
<b>i am giving you example for Change Vendor you practice for ur tcode</b>
For our example, we will use the Change Vendor transaction (FK02) to add a street address to an already existing vendor.
<b>Step #1</b>
Use SystemStatus menu path to determine online program name (SAPMF02K), screen number (0110)
<b>Step #2</b>
Use F1 key and Technical Info pushbutton in each screen field to be filled to determine the field name.
<b>Step #3</b>
Determine how to proceed in the transaction
(save the record by clicking on the Save pushbutton or pressing the F11 key).
<b>BDC Table Contents</b>
After researching the transaction we can determine the contents of the BDC table.
PROGRAM DYNPRO DYNBEGIN FNAM FVAL
SAMPF02K 0106 X
RF02K-LIFNR TEST1
RF02K-D0110 X
SAMPF02K 0110 X
LFA1-STRAS 123 Main St.
BDC_OKCODE /11
<b>Batch Input Methods</b>
CALL TRANSACTION USING
STATEMENT
<b>Call transaction - for data transfer</b>
Processing batch input data with CALL TRANSACTION USING is the faster of the two recommended data transfer methods. In this method, legacy data is processed inline in your data transfer program.
Syntax:
CALL TRANSACTION <tcode>
USING <bdc_tab>
MODE <mode>
UPDATE <update>
A Display all
E Display errors only
N No display
S Synchronous
A Asynchronous
L Local update
<b>The process flow of CALL TRANSACTION</b>
A program that uses CALL TRANSACTION USING to process legacy data should execute thefollowing steps:
Prepare a BDCDATA structure for the transaction that you wish to run.
Prepare a internal table to store error messages Tab_Mess like structure of BDCMSGCOLL.
With a CALL TRANSACTION USING statement, call the transaction and prepare the BDCDATA structure. For example:
CALL TRANSACTION MM01' USING BDCDATA MODE 'A' UPDATE 'S'. MESSAGES INTO TAB_MESS.
IF SY-SUBRC <> 0.
<Error_handling>.
ENDIF.
<b>Overview of Batch Input Session</b>
The first batch input method is to create a batch input session. It is the processing of this batch input session that updates the database, not the execution of the batch input program.
<b>Reward if usefull</b>
‎2007 Oct 11 6:15 AM
Hi,
Please find the code. i have created for call transcation. Just convert it into BDC session. you can search forum for help in creating BDC sessions.
*****************************************************************************
MAIN PROGRAM
******************************************************************************
&----
*& AT SELECTION-SCREEN *
&----
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
Dialog box for selecting File Name.
PERFORM display_dialog.
AT SELECTION-SCREEN.
Validate Input File
PERFORM validate_input_file.
Validate Plant.
PERFORM validate_plant.
&----
*& START-OF-SELECTION *
&----
START-OF-SELECTION.
Upload Data from File.
PERFORM upload_data.
Build the BDC data.
PERFORM build_bdc.
&----
*& END-OF-SELECTION *
&----
END-OF-SELECTION.
Display the Log details
PERFORM display_report.
**************************************************************************************
Include where i have written the code
***************************************************************************************
----
INCLUDE ZULV_BOM_UPLOAD_F01 *
----
&----
*& Form display_dialog
&----
To display the pop up dialog box
----
*
----
FORM display_dialog.
Calling FM to display File Select dialog box
CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = syst-cprog
dynpro_number = syst-dynnr
FIELD_NAME = ' '
IMPORTING
file_name = p_file .
ENDFORM. " display_dialog
&----
*& Form upload_data
&----
To Upload the data into Internal Table.
----
*
----
FORM upload_data.
DATA : lt_itab TYPE STANDARD TABLE OF alsmex_tabline,
ls_itab TYPE alsmex_tabline,
ls_upload TYPE ty_upload_data.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = p_file
i_begin_col = 1
i_begin_row = 2
i_end_col = 5
i_end_row = 50000
TABLES
intern = lt_itab[]
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE i000 WITH text-t04.
LEAVE LIST-PROCESSING.
ENDIF.
CLEAR ls_itab.
LOOP AT lt_itab INTO ls_itab.
CASE ls_itab-col.
WHEN 1.
ls_upload-matnr = ls_itab-value.
WHEN 2.
PERFORM convert_to_decimal USING ls_itab-value.
ls_upload-bmeng = ls_itab-value.
WHEN 3.
ls_upload-idnrk = ls_itab-value.
WHEN 4.
PERFORM convert_to_decimal USING ls_itab-value.
ls_upload-menge = ls_itab-value.
WHEN 5.
ls_upload-meins = ls_itab-value.
ENDCASE.
AT END OF row.
APPEND ls_upload TO gt_upload.
CLEAR ls_upload.
ENDAT.
CLEAR ls_itab.
ENDLOOP.
Deleting the contents of internal table
FREE lt_itab.
ENDFORM. " upload_data
&----
*& Form build_bdc
&----
Build the BDC
----
*
----
FORM build_bdc.
DATA: l_str_count TYPE string,
l_str TYPE string,
l_count(2) TYPE n VALUE 1,
l_posnr(4) TYPE n VALUE 10,
l_break TYPE i VALUE 0,
ls_upload TYPE ty_upload_data,
ls_matnr TYPE ty_matnr,
ls_log TYPE ty_log,
ls_bdcmsgcol TYPE bdcmsgcoll,
l_matnr TYPE matnr .
Collecting the Matnr into a separate IT
LOOP AT gt_upload INTO ls_upload.
ls_matnr-matnr = ls_upload-matnr.
APPEND ls_matnr TO gt_matnr.
CLEAR: ls_matnr,
ls_upload.
ENDLOOP.
Sort the Table by Material number
SORT gt_matnr BY matnr.
SORT gt_upload BY matnr.
CLEAR g_grp_index .
delete the duplicate records
DELETE ADJACENT DUPLICATES FROM gt_matnr COMPARING matnr.
CLEAR: ls_matnr,
ls_upload.
LOOP AT gt_matnr INTO ls_matnr.
Copying the matnr into Log Internal table
ls_log-matnr = ls_matnr-matnr.
APPEND ls_log TO gt_log.
CLEAR ls_log.
REFRESH: gt_bdcdata[],
gt_bdcmsgcol.
CLEAR l_matnr.
SELECT SINGLE matnr
FROM mast
INTO l_matnr
WHERE matnr EQ ls_matnr-matnr
AND werks EQ p_plant
AND stlan EQ '5' .
IF sy-subrc EQ 0 .
CLEAR ls_log.
ls_log-light = '1'.
ls_log-matnr = ls_matnr-matnr.
ls_log-desc = text-t11.
APPEND ls_log TO gt_log .
CONTINUE.
ENDIF.
PERFORM bdc_dynpro USING 'SAPLCSDI' '0100'.
PERFORM bdc_field USING 'BDC_OKCODE' '/00'.
PERFORM bdc_field USING 'RC29N-MATNR' ls_matnr-matnr.
PERFORM bdc_field USING 'RC29N-WERKS' p_plant.
PERFORM bdc_field USING 'RC29N-STLAN' '5'.
PERFORM bdc_field USING 'RC29N-DATUV' p_date.
READ TABLE gt_upload INTO ls_upload
WITH KEY matnr = ls_matnr-matnr.
CHECK sy-subrc EQ 0.
PERFORM bdc_dynpro USING 'SAPLCSDI' '0110'.
PERFORM bdc_field USING 'BDC_OKCODE' '/00'.
PERFORM bdc_field USING 'RC29K-BMENG' ls_upload-bmeng.
CLEAR ls_upload.
PERFORM bdc_field USING 'RC29K-STLST' '1'.
PERFORM bdc_dynpro USING 'SAPLCSDI' '0111'.
PERFORM bdc_field USING 'BDC_OKCODE' '/00'.
LOOP AT gt_upload INTO ls_upload
WHERE matnr = ls_matnr-matnr.
l_break = l_break + 1.
CLEAR: l_str,
l_str_count.
l_str_count = l_count.
CONDENSE l_str_count.
PERFORM bdc_dynpro USING 'SAPLCSDI' '0140'.
PERFORM bdc_field USING 'BDC_OKCODE' '/00'.
BDC for Item level
CONCATENATE 'RC29P-IDNRK(' l_str_count ')' INTO l_str.
PERFORM bdc_field USING l_str ls_upload-idnrk.
CONCATENATE 'RC29P-MENGE(' l_str_count ')' INTO l_str.
PERFORM bdc_field USING l_str ls_upload-menge.
CONCATENATE 'RC29P-MEINS(' l_str_count ')' INTO l_str.
PERFORM bdc_field USING l_str ls_upload-meins.
CONCATENATE 'RC29P-POSTP(' l_str_count ')' INTO l_str.
PERFORM bdc_field USING l_str 'L'.
PERFORM bdc_dynpro USING 'SAPLCSDI' '0130'.
PERFORM bdc_field USING 'BDC_OKCODE' '/00'.
PERFORM bdc_field USING 'RC29P-POSNR' l_posnr.
PERFORM bdc_field USING 'RC29P-IDNRK' ls_upload-idnrk.
PERFORM bdc_field USING 'RC29P-MENGE' ls_upload-menge.
PERFORM bdc_field USING 'RC29P-MEINS' ls_upload-meins.
PERFORM bdc_dynpro USING 'SAPLCSDI' '0131'.
PERFORM bdc_field USING 'BDC_OKCODE' '/00'.
PERFORM bdc_field USING 'RC29P-RVREL' 'X'. " Change
if the item is more than 14 records.
l_break = l_break MOD 14.
IF l_break EQ 0.
PERFORM bdc_dynpro USING 'SAPLCSDI' '0140'.
PERFORM bdc_field USING : 'BDC_OKCODE' '=FCNP'.
l_count = 1.
l_break = 1.
ENDIF.
l_count = l_count + 1.
l_posnr = l_posnr + 10.
CLEAR ls_upload.
ENDLOOP.
PERFORM bdc_dynpro USING 'SAPLCSDI' '0140'.
PERFORM bdc_field USING 'BDC_OKCODE' '=FCBU'.
l_count = 1.
PERFORM call_transcation USING ls_matnr-matnr.
CLEAR ls_matnr.
ENDLOOP.
ENDFORM. " build_bdc
&----
*& Form bdc-dynpro
&----
The program name and the screen number are passed
----
-->pa_program program name
-->pa_dynpro screen number
----
FORM bdc_dynpro USING pa_program
pa_dynpro.
DATA: c_x(1) TYPE c VALUE 'X'.
gs_bdcdata-program = pa_program.
gs_bdcdata-dynpro = pa_dynpro.
gs_bdcdata-dynbegin = c_x.
APPEND gs_bdcdata TO gt_bdcdata.
CLEAR gs_bdcdata.
ENDFORM. " bdc-dynpro
&----
*& Form bdc_field
&----
To populate field details in BDC
----
-->pa_fnam field name
-->pa_fval field value
----
FORM bdc_field USING pa_fnam
pa_fval.
gs_bdcdata-fnam = pa_fnam.
gs_bdcdata-fval = pa_fval.
APPEND gs_bdcdata TO gt_bdcdata.
CLEAR gs_bdcdata.
ENDFORM. " bdc_field
&----
*& Form validate_input_file
&----
To validate Input File
----
*
----
FORM validate_input_file.
check whether the uploaded file is in excel format
IF p_file NP '*.xls'.
MESSAGE e000 WITH text-t07.
ENDIF.
ENDFORM.
&----
*& Form call_transcation
&----
Call Transcation CS01
----
*
----
FORM call_transcation USING pa_matnr TYPE matnr.
DATA : c_mode TYPE c VALUE 'N' .
DATA: ls_bdcmsgcol TYPE bdcmsgcoll,
l_message TYPE string,
ls_log TYPE ty_log,
f_success TYPE c,
f_error TYPE c,
l_index(4) TYPE c .
CLEAR ls_log.
ls_log-matnr = pa_matnr.
CALL TRANSACTION 'CS01' USING gt_bdcdata
MODE c_mode
UPDATE 'S'
MESSAGES INTO gt_bdcmsgcol.
CLEAR ls_bdcmsgcol.
LOOP AT gt_bdcmsgcol INTO ls_bdcmsgcol.
IF ls_bdcmsgcol-msgtyp EQ 'S'.
Call transcation Success.set the flag
f_success = 'X'.
ELSEIF ls_bdcmsgcol-msgtyp EQ 'E' OR
ls_bdcmsgcol-msgtyp EQ 'A'.
Call transcation failed
f_error = 'X'.
ENDIF.
CLEAR ls_bdcmsgcol.
ENDLOOP.
IF f_success = 'X'.
Transcation executed successfully.
Check for success message
CLEAR: f_success,
ls_bdcmsgcol.
READ TABLE gt_bdcmsgcol INTO ls_bdcmsgcol
WITH KEY msgtyp = 'S'.
CHECK sy-subrc EQ 0.
ls_log-light = '3'. " Green color light
PERFORM build_message USING ls_bdcmsgcol
CHANGING l_message.
ls_log-desc = l_message. "Success message from BDC
ELSEIF f_error = 'X'.
Transcation failed
ls_log-light = '1'. "Red color light
Open BDC Session
Creating Group name for Open BDC
g_grp_index = g_grp_index + 1 .
CLEAR l_index.
MOVE g_grp_index TO l_index .
CONDENSE l_index NO-GAPS.
CONCATENATE g_group l_index sy-uzeit INTO g_group .
CONCATENATE g_group pa_matnr INTO g_group.
PERFORM open_group.
PERFORM bdc_insert.
PERFORM bdc_close.
ls_log-session = g_group.
CLEAR: f_error,
g_group,
ls_bdcmsgcol.
initializing group name.
g_group = 'ZB'.
READ TABLE gt_bdcmsgcol INTO ls_bdcmsgcol
WITH KEY msgtyp = 'E'.
IF sy-subrc EQ 0.
Message from BDC
PERFORM build_message USING ls_bdcmsgcol
CHANGING l_message.
ls_log-desc = l_message. "Error / Success message from BDC
ELSE.
Message Not Present in BDCMSGCOL.
ls_log-desc = text-t02. "Error message
ENDIF.
ENDIF.
APPEND ls_log TO gt_log.
CLEAR ls_log.
ENDFORM. " call_transcation
&----
*& Form open_group
&----
Open Group for BDC
----
*
----
FORM open_group.
DATA: l_client TYPE mandt,
l_user TYPE sy-uname,
l_holddate TYPE apqi-startdate.
CONSTANTS c_x TYPE c VALUE 'X'.
l_client = sy-mandt.
l_user = sy-uname.
CALL FUNCTION 'BDC_OPEN_GROUP'
EXPORTING
client = l_client
group = g_group
user = l_user
keep = c_x
holddate = l_holddate.
ENDFORM. " open_group
&----
*& Form bdc_insert
&----
Insert BDC data
----
*
----
FORM bdc_insert.
CALL FUNCTION 'BDC_INSERT'
EXPORTING
tcode = 'CS01'
TABLES
dynprotab = gt_bdcdata.
IF sy-subrc NE 0.
MESSAGE e000 WITH
'Problem with BDC session, contact system admin'.
ENDIF.
ENDFORM. " bdc_insert
&----
*& Form bdc_close
&----
Close The BDC
----
*
----
FORM bdc_close.
CALL FUNCTION 'BDC_CLOSE_GROUP'.
ENDFORM. " bdc_close
‎2007 Oct 11 6:17 AM
for writing any sort of BDC you'l first need Recording of perticular TCODE...
first create one recording of desired TCODE via SHDB Tcode....
then use following template to complete your BDC....
REPORT ZBDC_CHANGE_VENDOR_DETAILS .
<b>1. CREATE INTERNAL TABLE CONTAINING BDC FIELDS
(Pick this data from recording)</b>
data: begin of it_upload occurs 0 with header line,
"Define all the fields used in the recording
end of it_upload.
Batchinputdata of single transaction
data: bdcdata like bdcdata occurs 0 with header line.
*----
*Variables
*----
data: return(30).
*----
*Selection Screens
*----
selection-screen begin of block b1 with frame title text-000.
<b>*Give filename here.. file to be uploaded</b>
parameters p_fname like rlgrap-filename.
selection-screen end of block b1.
selection-screen begin of block b2 with frame title text-001.
parameters : p_sname(12),
p_keep default 'X' as checkbox.
selection-screen end of block b2.
*----
*At Selection-Screen
*----
at selection-screen on value-request for p_fname.
call function 'WS_FILENAME_GET'
exporting
DEF_FILENAME = ' '
DEF_PATH = ' '
mask = ',*.txt.' <b>".txt for notepad file and .xls for excel file</b>
mode = 'O'
TITLE = ' '
importing
filename = p_fname
RC =
exceptions
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
others = 5
.
if sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
endif.
at selection-screen.
call function 'WS_QUERY'
exporting
ENVIRONMENT =
filename = p_fname
query = 'FE'
WINID =
importing
return = return
exceptions
inv_query = 1
no_batch = 2
frontend_error = 3
others = 4
.
if sy-subrc <> 0.
elseif return = 0.
message e005 with 'File does not exist'.
endif.
*----
*Start-of-selection
*----
start-of-selection.
*Uploading the flat file containig the infotype records.
call function 'WS_UPLOAD'
exporting
CODEPAGE = ' '
filename = p_fname
filetype = 'DAT'
HEADLEN = ' '
LINE_EXIT = ' '
TRUNCLEN = ' '
USER_FORM = ' '
USER_PROG = ' '
DAT_D_FORMAT = ' '
IMPORTING
FILELENGTH =
tables
data_tab = it_upload
exceptions
conversion_error = 1
file_open_error = 2
file_read_error = 3
invalid_type = 4
no_batch = 5
unknown_error = 6
invalid_table_width = 7
gui_refuse_filetransfer = 8
customer_error = 9
others = 10
.
if sy-subrc <> 0.
message e005 with 'File upload failed'.
endif.
*Opening the session
call function 'BDC_OPEN_GROUP'
exporting
client = sy-mandt
group = p_sname
keep = p_keep
user = sy-uname
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 e005 with 'Unable to open a BDC group'.
endif.
loop at it_upload.
<b>2. Perform BDC_DYNPRO & BDC_FIELD for each screen & its fields...Eliminate those fields which are not necessary.OKCODE is mandatory
(Pick this data from recording)</b>*perform insert session details
"This code is example .. and the values mentioned in '' are from recording...
perform bdc_dynpro using 'SAPMF02K' '0106'.
perform bdc_field using:
'BDC_CURSOR' 'RF02K-D0130',
'BDC_OKCODE' '/00',
'RF02K-LIFNR' it_upload-vendor_code,
'RF02K-BUKRS' it_upload-company_code,
'RF02K-D0110' 'X',
'RF02K-D0120' 'X',
'RF02K-D0130' 'X'.
<b>3. Perform BDC_INSERT here b4 endloop</b>
CALL FUNCTION 'BDC_INSERT'
EXPORTING
TCODE = 'FBD1' put ur TCODE 4 whch u r doin BDC
POST_LOCAL = NOVBLOCAL
PRINTING = NOPRINT
SIMUBATCH = ' '
CTUPARAMS = ' '
TABLES
dynprotab = 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 bdcdata.
endloop.
<b>*Closing the session</b>
call function 'BDC_CLOSE_GROUP'
exceptions
not_open = 1
queue_error = 2
others = 3.
if sy-subrc <> 0.
message e005 with 'Unable to close the BDC group'.
endif.
----
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.
clear bdcdata.
bdcdata-fnam = fnam.
bdcdata-fval = fval.
append bdcdata.
endform.
‎2007 Oct 11 6:18 AM
hi,
include bdcrecx1.
parameters: filename like rlgrap-filename.
data: begin of record,
ASNUM(18),
ASKTX(40),
ASTYP(4),
MEINS(3),
MATKL(6),
BKLAS(4),
end of record.
data: itab_program like record occurs 0 with header line.
***
****************************************************
At Selection Screen
****************************************************
at selection-screen on value-request for filename.
perform query_filename changing filename.
****************************************************
*START-OF-SELECTION
*****************************************************
start-of-selection.
*-- Upload flat data to ITAB.
perform upload_to_itab.
*{ chg001 -- modified as per requirement
perform open_group.
loop at itab_program.
perform upload_programs.
endloop.
perform close_group.
----
FORM UPLOAD_TO_ITAB *
----
........ *
----
form upload_to_itab.
call function 'WS_UPLOAD'
exporting
CODEPAGE = ' '
filename = filename
filetype = 'DAT'
HEADLEN = ' '
LINE_EXIT = ' '
TRUNCLEN = ' '
USER_FORM = ' '
USER_PROG = ' '
DAT_D_FORMAT = ' '
IMPORTING
FILELENGTH =
tables
data_tab = itab_program
EXCEPTIONS
CONVERSION_ERROR = 1
FILE_OPEN_ERROR = 2
FILE_READ_ERROR = 3
INVALID_TYPE = 4
NO_BATCH = 5
UNKNOWN_ERROR = 6
INVALID_TABLE_WIDTH = 7
GUI_REFUSE_FILETRANSFER = 8
CUSTOMER_ERROR = 9
OTHERS = 10
.
if sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
write : 'File opening error.'.
endif.
endform.
----
FORM QUERY_FILENAME *
----
........ *
----
--> P_FILENAME *
----
form query_filename changing p_filename.
data : tmp_filename like filename.
call function 'WS_FILENAME_GET'
exporting
def_filename = filename
mask = ',.txt,.txt.'
mode = 'O'
title = 'Select the file to Upload'
importing
filename = tmp_filename
exceptions
inv_winsys = 01
no_batch = 02
selection_cancel = 03
selection_error = 04.
if sy-subrc = 0.
filename = tmp_filename.
endif.
endform.
*******************************************
*perform open_group.
&----
*& Form UPLOAD_programs
&----
text
----
--> p1 text
<-- p2 text
----
form upload_programs.
perform bdc_dynpro using 'SAPLBAS0' '0300'.
perform bdc_field using 'BDC_CURSOR'
'ASMD-ASNUM'.
perform bdc_field using 'BDC_OKCODE'
'=NEW'.
perform bdc_field using 'RM63T-SPRAS'
'EN'.
perform bdc_dynpro using 'SAPLBAS0' '0300'.
perform bdc_field using 'BDC_CURSOR'
'ASMD-MATKL'.
perform bdc_field using 'BDC_OKCODE'
'=POST'.
perform bdc_field using 'ASMD-ASNUM'
itab_program-ASNUM.
perform bdc_field using 'ASMDT-ASKTX'
itab_program-ASKTX.
perform bdc_field using 'ASMD-ASTYP'
itab_program-ASTYP.
perform bdc_field using 'ASMD-MEINS'
itab_program-MEINS.
perform bdc_field using 'ASMD-MATKL'
itab_program-MATKL.
perform bdc_field using 'ASMD-BKLAS'
itab_program-BKLAS.
perform bdc_dynpro using 'SAPLBAS0' '0300'.
perform bdc_field using 'BDC_OKCODE'
'/EBACK'.
perform bdc_field using 'BDC_CURSOR'
'ASMD-ASNUM'.
perform bdc_dynpro using 'SAPLSPO1' '0100'.
perform bdc_field using 'BDC_OKCODE'
'=YES'.
perform bdc_transaction using 'AC03'.
endform. " UPLOAD_programs
check this to upload dat from excel to internal table.
TABLES:zmatnr.
TYPE-POOLS truxs.
DATA : itab LIKE alsmex_tabline OCCURS 0 WITH HEADER LINE.
DATA row LIKE alsmex_tabline-row.
data : gi_final like zmatnr occurs 0 with header line.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETER : pfname LIKE rlgrap-filename OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR pfname.
PERFORM search.
START-OF-SELECTION.
perform process.
form process.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = pfname
i_begin_col = 1
i_begin_row = 2
i_end_col = 12
i_end_row = 65000
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.
describe table itab lines itab_count.
row = 1.
loop at itab.
if itab-row <> row.
append gi_final.
clear gi_final.
endif.
case itab-col.
when '1'.
gi_final-MATNR = itab-value.
when '2'.
gi_final-Maktx = itab-value.
endcase.
row = itab-row.
endloop.
append gi_final.
clear gi_final.
endform.
FORM search .
CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
EXPORTING
static = 'X'
CHANGING
file_name = pfname.
ENDFORM.
regards
siva
‎2007 Oct 11 6:29 AM
Please have a look at the following code this will give u a clear understanding for BDC session creation . This code will create a BDC session that can be processed thru SM35 . Just execute the code by creating a Z program in SE38 and then check the created session in SM35 . Reward points if info useful....
&----
*& Report Z_TEST_BDC_SESSIONS
*&
&----
*&
*&
&----
REPORT Z_TEST_BDC_SESSIONS.
data : gt_bdc TYPE STANDARD TABLE OF bdcdata,
gs_bdc TYPE bdcdata,
user LIKE apqi-userid ,
"user for start session in batch
keep(1) TYPE c VALUE 'X',
, "' ' = delete session if finished
"'X' = keep session if finished
holddate LIKE apqi-startdate ,
w_group(12) TYPE c,
p_group(6) VALUE 'SLUPLD'.
CONSTANTS : c_comma TYPE c VALUE ',',
c_x TYPE c VALUE 'X',
c_e TYPE c VALUE 'N'.
Creating BDC data for a sales order
PERFORM fill_bdc_screen USING 'SAPMV45A' '0102' .
PERFORM fill_bdc_val USING: 'BDC_OKCODE' '/00'.
PERFORM fill_bdc_val USING: 'VBAK-VBELN' '1006'." Enter a valid sales order
Every BDC session is uniquely identified by its group
CONCATENATE p_group sy-uzeit INTO w_group.
user = sy-uname.
open , insert and close BDC group to create a session
CALL FUNCTION 'BDC_OPEN_GROUP'
EXPORTING
client = sy-mandt
group = w_group
user = user
keep = keep
holddate = holddate.
CALL FUNCTION 'BDC_INSERT'
EXPORTING
tcode = 'VA02'
TABLES
dynprotab = gt_bdc.
CALL FUNCTION 'BDC_CLOSE_GROUP'.
FORM fill_bdc_screen USING p_prog
p_scrn.
DATA : ls_bdc TYPE bdcdata.
MOVE : p_prog TO ls_bdc-program,
p_scrn TO ls_bdc-dynpro,
c_x TO ls_bdc-dynbegin.
APPEND ls_bdc TO gt_bdc.
ENDFORM. " fill_bdc_screen
FORM fill_bdc_val USING p_fnam
p_fval.
DATA : ls_bdc TYPE bdcdata,
l_str TYPE string.
MOVE p_fval TO l_str.
MOVE : p_fnam TO ls_bdc-fnam,
l_str TO ls_bdc-fval.
APPEND ls_bdc TO gt_bdc.
ENDFORM. " fill_bdc_val
‎2007 Oct 11 6:50 AM
Hi Amardeep,
For a BDC upload you need to write a program which creates BDC sessions.
Steps for BDC Uplaod:
1. Work out the transaction you would use to create the data manually.
2. Use transaction "SHDB" to record the creation of one material master data.
Click the New recording button or the Menu - Recording - Create
3. Save the recording, and then go back a screen to the overview.
4. Select the recording and click on Edit - Create Program.
Give the program a Z* name, and select transfer from recording.
5. Edit the program. You will see that all the data you entered is hard-coded into the program. Make the following changes:
a. After the start-of-selection, Call ws_upload to upload the file (the excel file needs to be saved as TAB separated).
b. After the open-group, Loop on the uploaded data. For each line, perform validation checks on the data, then modify the perform bdc_field commands to use the file data.
c. After perform bdc_transaction, add endloop.
Execute the program. It will have options to create a batch session or to process directly.
Please reward points if useful.