Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

sample BDC code

Former Member
0 Likes
2,624

Hi All,

Any one can please send a sample <b>BDC code in direct input method and session method.</b>

you can send your views to 'muraly.g@gmail.com'.

Thanks in advance.

-Muraly

6 REPLIES 6
Read only

former_member189596
Active Participant
0 Likes
1,191

Hi muraly.

Please check the code below

...........................................................

*: Description :

*: -


:

*: This is a simple example program to get data from an excel :

*: file and store it in an internal table. :

*: :

*: :

*: SAP Version : 4.7 :

*:............................................................:

REPORT zupload_excel_to_itab.

TYPE-POOLS: truxs.

PARAMETERS: p_file TYPE rlgrap-filename.

TYPES: BEGIN OF t_datatab,

col1(30) TYPE c,

col2(30) TYPE c,

col3(30) TYPE c,

END OF t_datatab.

DATA: it_datatab type standard table of t_datatab,

wa_datatab type t_datatab.

DATA: it_raw TYPE truxs_t_text_data.

  • At selection screen

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.

START-OF-SELECTION.

CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'

EXPORTING

  • I_FIELD_SEPERATOR =

i_line_header = 'X'

i_tab_raw_data = it_raw " WORK TABLE

i_filename = p_file

TABLES

i_tab_converted_data = it_datatab[] "ACTUAL DATA

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.

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

  • END-OF-SELECTION.

END-OF-SELECTION.

LOOP AT it_datatab INTO wa_datatab.

WRITE:/ wa_datatab-col1,

wa_datatab-col2,

wa_datatab-col3.

ENDLOOP.

Regards,

bhaskar

Read only

Former Member
0 Likes
1,191

hi Muraly,

<b>BDC:</b>

Batch Data Communication (BDC) is the process of transferring data from one SAP System to another SAP system or from a non-SAP system to SAP System.

<b>Features :</b>

BDC is an automatic procedure.

This method is used to transfer large amount of data that is available in electronic medium.

BDC can be used primarily when installing the SAP system and when transferring data from a legacy system (external system).

BDC uses normal transaction codes to transfer data.

<b>Types of BDC :</b>

CLASSICAL BATCH INPUT (Session Method)

CALL TRANSACTION

<b>BATCH INPUT METHOD:</b>

This method is also called as ‘CLASSICAL METHOD’.

Features:

Asynchronous processing.

Synchronous Processing in database update.

Transfer data for more than one transaction.

Batch input processing log will be generated.

During processing, no transaction is started until the previous transaction has been written to the database.

<b>CALL TRANSACTION METHOD :</b>

This is another method to transfer data from the legacy system.

Features:

Synchronous processing. The system performs a database commit immediately before and after the CALL TRANSACTION USING statement.

Updating the database can be either synchronous or asynchronous. The program specifies the update type.

Transfer data for a single transaction.

Transfers data for a sequence of dialog screens.

No batch input processing log is generated.

Differences between Call Transaction and Sessions Method:

<b>Session method.</b>

1) synchronous processing.

2) can tranfer large amount of data.

3) processing is slower.

4) error log is created

5) data is not updated until session is processed.

6) generally used for back ground jobs.

7) at atime we can update to more than one screens.

<b>Call transaction.</b>

1) asynchronous processing

2) can transfer small amount of data

3) processing is faster.

4) errors need to be handled explicitly

5) data is updated automatically

6) for background n fore ground jobs.

7) at atime we can update to a single screen.

<b>For BDC:</b>

http://myweb.dal.ca/hchinni/sap/bdc_home.htm

https://www.sdn.sap.com/irj/sdn/wiki?path=/display/home/bdc&;

http://www.sap-img.com/abap/learning-bdc-programming.htm

http://www.sapdevelopment.co.uk/bdc/bdchome.htm

http://www.sap-img.com/abap/difference-between-batch-input-and-call-transaction-in-bdc.htm

http://help.sap.com/saphelp_47x200/helpdata/en/69/c250684ba111d189750000e8322d00/frameset.htm

http://www.sapbrain.com/TUTORIALS/TECHNICAL/BDC_tutorial.html

<b>Check these link:</b>

http://www.sap-img.com/abap/difference-between-batch-input-and-call-transaction-in-bdc.htm

http://www.sap-img.com/abap/question-about-bdc-program.htm

http://www.itcserver.com/blog/2006/06/30/batch-input-vs-call-transaction/

http://www.planetsap.com/bdc_main_page.htm

<b>Sample BDC</b>

report ZABSALES_ORDER no standard page heading line-size 255.

TABLES: vbak, vbap.

DATA: bdcdata LIKE bdcdata OCCURS 0 WITH HEADER LINE.

DATA: messtab LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE.

SELECTION-SCREEN BEGIN OF SCREEN 500 AS WINDOW.

SELECTION-SCREEN BEGIN OF BLOCK salesblock WITH FRAME TITLE text-001.

SELECTION-SCREEN SKIP 1.

PARAMETERS : Order_ty LIKE vbak-auart DEFAULT 'OR'

modif id SC1,

Customer LIKE kuagv-kunnr OBLIGATORY VALUE CHECK,

Purchase LIKE vbkd-bstkd OBLIGATORY VALUE CHECK,

Material LIKE vbap-matnr OBLIGATORY VALUE CHECK,

Quantity LIKE rv45a-kwmeng OBLIGATORY VALUE CHECK.

SELECTION-SCREEN END OF BLOCK salesblock.

SELECTION-SCREEN END OF SCREEN 500.

CALL SELECTION-SCREEN 500.

*At selection-screen on Order_ty.

*IF Order_ty <> 'OR'.

  • MESSAGE E080(ZVIKALP).

  • ENDIF.

start-of-selection.

perform bdc_dynpro using 'SAPMV45A' '0101'.

perform bdc_field using 'BDC_CURSOR'

'VBAK-AUART'.

perform bdc_field using 'BDC_OKCODE'

'=UER1'.

perform bdc_field using 'VBAK-AUART'

Order_ty.

perform bdc_dynpro using 'SAPMV45A' '4001'.

perform bdc_field using 'BDC_OKCODE'

'=SICH'.

perform bdc_field using 'VBKD-BSTKD'

Purchase.

perform bdc_field using 'KUAGV-KUNNR'

Customer.

perform bdc_dynpro using 'SAPMSSY0' '0120'.

perform bdc_field using 'BDC_CURSOR'

'04/05'.

perform bdc_field using 'BDC_OKCODE'

'SICH'.

perform bdc_field using 'RV45A-KETDAT'

'06-05-2005'.

perform bdc_field using 'RV45A-KPRGBZ'

'D'.

perform bdc_field using 'VBKD-PRSDT'

'05-29-2005'.

perform bdc_field using 'BDC_CURSOR'

'RV45A-KWMENG(01)'.

perform bdc_field using 'RV45A-MABNR(01)'

Material.

perform bdc_field using 'RV45A-KWMENG(01)'

Quantity.

perform bdc_dynpro using 'SAPLSPO2' '0101'.

perform bdc_field using 'BDC_OKCODE'

'=OPT1'.

CALL TRANSACTION 'VA01' USING bdcdata

MODE 'N'

UPDATE 'A'

MESSAGES INTO messtab.

loop at messtab.

if messtab-msgtyp = 'S'.

FORMAT COLOR 5.

elseif messtab-msgtyp = 'E'.

FORMAT COLOR 6.

ENDIF.

write: / messtab-msgtyp, messtab-msgnr.

endloop.

AT SELECTION-SCREEN OUTPUT.

LOOP AT SCREEN.

IF SCREEN-GROUP1 = 'SC1'.

screen-input = 0.

MODIFY SCREEN.

ENDIF.

ENDLOOP.

----


  • 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 <> ' '.

CLEAR BDCDATA.

BDCDATA-FNAM = FNAM.

BDCDATA-FVAL = FVAL.

APPEND BDCDATA.

ENDIF.

ENDFORM.

***********By session method

report ZAB_PURCHASE no standard page heading line-size 255.

tables: ekko, ekpo, t100.

DATA: BDCDATA LIKE BDCDATA OCCURS 0 WITH HEADER LINE.

DATA: MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.

DATA: BEGIN OF T_MSG OCCURS 0.

INCLUDE STRUCTURE T100.

DATA: END OF T_MSG.

*include bdcrecx1.

*SELECTION-SCREEN BEGIN OF SCREEN 500 AS WINDOW.

SELECTION-SCREEN BEGIN OF BLOCK salesblock WITH FRAME TITLE text-001.

PARAMETERS : Vendor like Ekko-lifnr OBLIGATORY,

Order_ty like RM06E-BSART obligatory,

Pur_org like ekko-ekorg obligatory value check,

Pur_grp like ekko-ekgrp obligatory value check,

Material like ekpo-ematn obligatory value check,

Quantity(13) type n,

Plant like ekpo-werks obligatory value check.

SELECTION-SCREEN END OF BLOCK salesblock.

*SELECTION-SCREEN END OF SCREEN 500.

*CALL SELECTION-SCREEN 500.

TOP-OF-PAGE.

WRITE :/40 'Creating Purchase Order' COLOR 4 INTENSIFIED ON INVERSE ON

.

ULINE.

FORMAT COLOR 7 INVERSE ON.

WRITE :/1(20) 'Mesaage Type',

25(20) 'Message'.

ULINE.

start-of-selection.

*perform open_group.

perform bdc_dynpro using 'SAPMM06E' '0100'.

perform bdc_field using 'BDC_CURSOR'

'EKKO-EKGRP'.

perform bdc_field using 'BDC_OKCODE'

'/00'.

perform bdc_field using 'EKKO-LIFNR'

Vendor.

perform bdc_field using 'RM06E-BSART'

Order_ty.

perform bdc_field using 'RM06E-BEDAT'

'05-30-2005'.

perform bdc_field using 'EKKO-EKORG'

Pur_org.

perform bdc_field using 'EKKO-EKGRP'

Pur_grp.

perform bdc_field using 'RM06E-LPEIN'

'T'.

perform bdc_dynpro using 'SAPMM06E' '0120'.

perform bdc_field using 'BDC_CURSOR'

'EKPO-WERKS(01)'.

perform bdc_field using 'BDC_OKCODE'

'=BU'.

perform bdc_field using 'EKPO-EMATN(01)'

Material.

perform bdc_field using 'EKPO-MENGE(01)'

Quantity.

perform bdc_field using 'EKPO-WERKS(01)'

Plant.

*perform bdc_transaction using 'ME21'.

*CALL TRANSACTION 'ME21' USING BDCDATA

  • MODE 'A'

  • UPDATE 'A'

  • MESSAGES INTO MESSTAB.

CALL FUNCTION 'BDC_OPEN_GROUP'

EXPORTING

CLIENT = SY-MANDT

  • DEST = FILLER8

GROUP = 'ZAP3'

  • HOLDDATE = FILLER8

KEEP = 'X'

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.

CALL FUNCTION 'BDC_INSERT'

EXPORTING

TCODE = 'ME21'

  • POST_LOCAL = NOVBLOCAL

  • PRINTING = NOPRINT

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.

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.

end-of-selection.

loop at messtab.

CALL FUNCTION 'FORMAT_MESSAGE'

EXPORTING

ID = messtab-MSGID

LANG = 'EN'

NO = messtab-msgnr

V1 = messtab-MSGV1

V2 = messtab-MSGV2

V3 = messtab-MSGV3

V4 = messtab-MSGV4

IMPORTING

MSG = T_MSG

  • EXCEPTIONS

  • NOT_FOUND = 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.

if messtab-msgtyp = 'S'.

FORMAT COLOR 5 inverse on.

elseif messtab-msgtyp = 'E'.

FORMAT COLOR 6 inverse on.

else.

FORMAT COLOR 3 inverse on.

ENDIF.

WRITE : /1(20) messtab-msgtyp ,

25(60) t_msg.

endloop.

*perform close_group.

----


  • 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 <> ' '.

CLEAR BDCDATA.

BDCDATA-FNAM = FNAM.

BDCDATA-FVAL = FVAL.

APPEND BDCDATA.

ENDIF.

ENDFORM.

<b>Reward points if Useful</b>

Regards

Gokul

Read only

Former Member
0 Likes
1,191

Hi,

REPORT zsdr_price_upload MESSAGE-ID zsdr_bdc_vk11 LINE-SIZE 150

LINE-COUNT 75 .

******Internal Table for Header Data.

TYPES : BEGIN OF type_header,

kschl LIKE konv-kschl,

vkorg LIKE vbak-vkorg,

vtweg LIKE komg-spart,

matnr LIKE mvke-matnr,

kbetr(11) TYPE c,

datab(10) TYPE c,

datbi(10) TYPE c,

END OF type_header.

****Internal Table for Item Level.

TYPES : BEGIN OF type_item,

kschl LIKE konv-kschl,

vkorg LIKE vbak-vkorg,

vtweg LIKE komg-spart,

matnr LIKE mvke-matnr,

kbetr(11) TYPE c,

datab(10) TYPE c,

datbi(10) TYPE c,

END OF type_item.

******Error Table For not found in MVKE.

TYPES : BEGIN OF type_error ,

kschl LIKE konv-kschl,

vkorg LIKE vbak-vkorg,

vtweg LIKE komg-spart,

matnr LIKE mvke-matnr,

kbetr LIKE konp-kbetr,

datab(10) TYPE c,

datbi(10) TYPE c,

text(100) TYPE c,

END OF type_error.

****For error Messages

TYPES : BEGIN OF type_mtab,

matnr LIKE mara-matnr,

msgtyp LIKE bdcmsgcoll-msgtyp,

msgid LIKE bdcmsgcoll-msgid,

msgnr LIKE bdcmsgcoll-msgnr,

text(100) TYPE c,

END OF type_mtab.

****Internal Table

TYPES: BEGIN OF type_mvke,

matnr LIKE mvke-matnr,

vkorg LIKE mvke-vkorg,

vtweg LIKE mvke-vtweg,

END OF type_mvke.

****Internal Table

TYPES : BEGIN OF type_tvkov,

vkorg LIKE tvkov-vkorg,

vtweg LIKE tvkov-vtweg,

END OF type_tvkov.

    • Declaring Internal Tables

DATA : t_header TYPE STANDARD TABLE OF type_header,

t_item TYPE STANDARD TABLE OF type_item,

t_mvke TYPE STANDARD TABLE OF type_mvke,

t_tvkov TYPE STANDARD TABLE OF type_tvkov,

t_error TYPE STANDARD TABLE OF type_error,

t_mtab TYPE STANDARD TABLE OF type_mtab.

    • Work Area Declaration.

DATA : wa_header LIKE LINE OF t_header,

wa_item LIKE LINE OF t_item,

wa_error LIKE LINE OF t_error,

wa_mtab LIKE LINE OF t_mtab,

wa_tvkov LIKE LINE OF t_tvkov,

wa_mvke LIKE LINE OF t_mvke.

*Rows for Table with Excel Data*******

DATA: t_xls_file LIKE alsmex_tabline OCCURS 0 WITH HEADER LINE.

***Constant.

DATA : c_params LIKE ctu_params.

DATA : c_ans(1) TYPE c.

DATA : v_count(4) TYPE c. " To show No.of records

DATA : bdctab LIKE bdcdata OCCURS 10 WITH HEADER LINE. " BDCDATA

DATA : tmess_mtab LIKE bdcmsgcoll OCCURS 10 WITH HEADER LINE.

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

**

    • SELECTION SCREEN

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

**

SELECTION-SCREEN : BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

PARAMETERS : p_fname LIKE rlgrap-filename OBLIGATORY.

SELECTION-SCREEN : END OF BLOCK b1.

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

  • END OF SELECTION SCREEN.

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

DATA : repid LIKE sy-repid.

DATA : v_matnr(50) TYPE c, "used for line items

v_kbetr(50) TYPE c, "used for line items

v_dat1(50) TYPE c, "used for line items

v_dat(50) TYPE c. "used for line items

DATA : v_lindx(5) TYPE n ,"index counter for first 14 records.

v_lindx1(5) TYPE n VALUE '01', "index counter for 13 records.

v_item(5) TYPE c, "To increment the line index

v_pgedwn2 TYPE i . "For Pagedown Counter

DATA: v_currentrow TYPE i. "For Current Row

DATA v_bdc(50) TYPE c." Text to apper in Confrim POPUP Window.

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

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.

PERFORM get_filename USING p_fname.

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

START-OF-SELECTION.

******Values for Ctu_params to Transaction

c_params-defsize = 'X'.

c_params-dismode = 'N'.

c_params-updmode = 'S'.

******Refresh

PERFORM f_refresh.

*********To upload File.

PERFORM upload_file.

****User Confrimation only BDC will Process

IF c_ans = '1'.

    • *** BDC Process.

PERFORM read_data.

ELSE.

FORMAT COLOR 3 INTENSIFIED .

WRITE:/ 'Selected not to Process the Upload'.

EXIT.

ENDIF.

******On completion of Process Refresh the Internal Table

REFRESH : t_xls_file,

t_header,

t_item,

t_mvke,

t_tvkov.

CLEAR : t_xls_file,

wa_header,

wa_item,

wa_mvke,

wa_tvkov.

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

***********Display Messages

WRITE : /01 'Status',19 'Status Text'.

WRITE AT 0(150) sy-uline.

LOOP AT t_mtab INTO wa_mtab.

WRITE :/01 wa_mtab-msgtyp,19 wa_mtab-text.

ENDLOOP.

SKIP 2.

SORT t_error BY matnr.

WRITE AT 0(150) sy-uline.

WRITE 'ERROR MESSAGES'.

WRITE AT 0(150) sy-uline.

WRITE :/01 'Material.No',20 'Status Text'.

WRITE AT 0(150) sy-uline.

LOOP AT t_error INTO wa_error WHERE matnr NE ' '.

!

WRITE:/01 wa_error-matnr,20 wa_error-text.

ENDLOOP.

----


*& Form get_filename

&----


  • text

----


  • -->P_FILENAME text

----


FORM get_filename USING p_fname.

*****To read the file from Presentation Server

CALL FUNCTION 'KD_GET_FILENAME_ON_F4'

EXPORTING

program_name = repid

dynpro_number ! ; = syst-dynnr

field_name = p_fname

  • STATIC = ' '

mask = '*.XLS'

CHANGING

file_name = p_fname

EXCEPTIONS

mask_too_long = 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.

ENDFORM. " get_filename

&----


*& Form upload_file

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM upload_file.

DATA : frow VALUE 2 TYPE i,

fcol VALUE 1 TYPE i,

erow VALUE 10000 TYPE i,

ecol VALUE 7 TYPE i,

ecol1 VALUE 1 TYPE i,

c_col1 TYPE i VALUE '0001',

c_col2 TYPE i VALUE '0002',

c_col3 TYPE i VALUE '0003',

&nb! sp; c_col4 TYPE i VALUE '0004',

c_col5 TYPE i VALUE '0005',

c_col6 TYPE i VALUE '0006',

c_col7 TYPE i VALUE '0007'.

***FM used to UPLOAD data from Flat file

CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'

EXPORTING

filename = p_fname

i_begin_col = fcol

i_begin_row = frow

i_end_col = ecol

i_end_row = erow

TABLES

intern = t_xls_file

EXCEPTIONS

inconsistent_parameters = 1

upload_ole = 2

OTHERS = 3.

IF sy-subrc <> 0.

MESSAGE e000.

ENDIF.

****T_XLS_FILE is initial, stop the process & throw message

IF t_xls_file[] IS INITIAL.

FORMAT COLOR 6 ON INTENSIFIED ON.

WRITE:/ 'No Data Exists '.

FORMAT COLOR OFF INTENSIFIED OFF.

STOP.

ELSE.

  • Sort table by rows and colums

SORT t_xls_file BY row col.

  • Get first row retrieved

READ TABLE t_xls_file INDEX 1.

  • Set first row retrieved to current row

v_currentrow = t_xls_file-row.

**Loop to move data in internal Table

LOOP AT t_xls_file .

  • Reset values for next row

IF t_xls_file-row NE v_currentrow.

APPEND wa_header TO t_header.

CLEAR wa_header.

v_currentrow = t_xls_file-row.

ENDIF.

CASE t_xls_file-col.

WHEN c_col1. "Kschl

wa_header-kschl = t_xls_file-value.

WHEN c_col2. "Vkorg

wa_header-vkorg = t_xls_file-value.

WHEN c_col3. "vtweg

wa_header-vtweg = t_xls_file-value.

WHEN c_col4. "Matnr

wa_header-matnr = t_xls_file-value.

WHEN c_col5. "Kbetr

wa_header-kbetr = t_xls_file-value.

WHEN c_col6. "FROm

wa_header-datab = t_xls_file-value.

WHEN c_col7. "TO

wa_header-datbi = t_xls_file-value.

ENDCASE.

ENDLOOP.

APPEND wa_header TO t_header.

CLEAR wa_header.

ENDIF.

****To process the data

PERFORM f_process.

ENDFORM. " upload_file

&----


*& Form READ_DATA

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM read_data.

****To make Uniq Records in Header Level

SORT t_header BY kschl vkorg vtweg.

DELETE ADJACENT DUPLICATES FROM t_header COMPARING

kschl vkorg vtweg .

SORT t_item BY vkorg vtweg matnr.

DATA : wa1_item TYPE type_item.

DATA : l_cnt TYPE i.

DATA : flag(1) TYPE c. "to process the Line item.

***Looping Header Table.

LOOP AT t_header INTO wa_header.

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

PERFORM bdc_dynpro US! ING 'SAPMV13A' '0100'.

PERFORM bdc_field USING 'BDC_CURSOR'

'RV13A-KSCHL'.

PERFORM bdc_field USING 'BDC_OKCODE'

'=ANTA'.

PERFORM bdc_field USING 'RV13A-KSCHL'

wa_header-kschl.

PERFORM bdc_dynpro USING 'SAPLV14A' '0100'.

PERFORM bdc_field USING 'BDC_CURSOR'

'RV130-SELKZ(03)'.

PERFORM bdc_field USING 'BDC_OKCODE'

'=WEIT'.

PERFORM bdc_field USING 'RV130-SELKZ(03)'

'X'.

PERFORM bdc_dynpro USING 'SAPMV13A' '1004'.

PERFORM bdc_field USING 'BDC_CURSOR'

'KOMG-VKORG'.

PERFORM bdc_field USING 'KOMG-VKORG'

wa_header-vkorg.

PERFORM bdc_field USING 'KOMG-VTWEG'

wa_header-vtweg.

****To handle Line Items.

LOOP AT t_item INTO wa1_item WHERE vkorg = wa_header-vkorg AND

vtweg = wa_header-vtweg.

wa_item = wa1_item.

******Flag Set only After processing first 14 records .

IF flag = ' '.

v_lindx = v_lindx + 01.

SHIFT v_lindx LEFT DELETING LEADING '0'.

v_item = v_lindx .

CONCATENATE 'KOMG-MATNR(' v_item ')' INTO v_matnr.

PERFORM bdc_field USING v_matnr

wa_item-matnr.

CONCATENATE 'KONP-KBETR(' v_item ')' INTO v_kbetr.

PERFORM bdc_field USING v_kbetr

wa_item-kbetr.

CONCATENATE 'RV13A-DATAB(' v_item ')' INTO v_dat.

PERFORM bdc_field USING v_dat

wa_item-datab.

CONCATENATE 'RV13A-DATBI(' v_item ')' INTO v_dat1.

PERFORM bdc_field USING v_dat1

wa_item-datbi.

IF v_item = 14.

flag = 'X'.

PERFORM bdc_field USING 'BDC_OKCODE'

'/00'.

PERFORM bdc_field USING 'BDC_OKCODE'

'=P+'.

PERFORM bdc_dynpro USING 'SAPMV13A' '1004'.

CLEAR v_lindx.

CLEAR v_item.

CONTINUE.

ENDIF.

ENDIF.

***Flag is Set after Processing of 14 records.

            • TO process rest of Records

IF flag = 'X'.

v_pgedwn2 = v_pgedwn2 + 1.

v_lindx1 = v_lindx1 + 01.

SHIFT v_lindx1 LEFT DE! LETING LEADING '0'.

v_item = v_lindx1 .

CONCATENATE 'KOMG-MATNR(' v_it! em ')' INTO v_matnr.

PERFORM bdc_field USING v_matnr

wa_item-matnr.

CONCATENATE 'KONP-KBETR(' v_item ')' INTO v_kbetr.

PERFORM bdc_field USING v_kbetr

wa_item-kbetr.

CONCATENATE 'RV13A-DATAB(' v_item ')' INTO v_dat.

PERFORM bdc_field USING v_dat

wa_item-datab.

CONCATENATE 'RV13A-DATBI(' v_item ')' INTO v_dat1.

PERFORM bdc_field USING v_dat1

wa_item-datbi.

IF v_pgedwn2 = 13.

PERFORM bdc_field USING 'BDC_OKCODE'

'/00'.

PERFORM bdc_field USING 'BDC_OKCODE'

'=P+'.

PERFORM bdc_dynpro USING 'SAPMV13A' '1004'.

v_pgedwn2 = 0.

v_lindx1 = 1.

CLEAR v_item.

CONTINUE.

ENDIF.

ENDIF.

ENDLOOP.

PERFORM bdc_field USING 'BDC_OKCODE'

'=SICH'.

            • Calling Transaction after Processing All items.

CALL TRANSACTION 'VK11' USING bdctab

OPTIONS FROM c_params MESSAGES INTO tmess_mtab.

*

REFRESH bdctab.

CLEAR : bdctab.

CLEAR : wa_item.

CLEAR : wa1_item.

CLEAR : wa_header.

CLEAR : l_cnt.

CLEAR : v_lindx1.

CLEAR: v_pgedwn2,v_lindx.

LOOP AT tmess_mtab .

l_cnt = l_cnt + 1.

READ TABLE t_item INTO wa_item INDEX l_cnt .

!

CALL FUNCTION 'MASS_MESSAGE_GET' "To get the Message Text

EXPORTING

arbgb = tmess_mtab-msgid

msgnr = tmess_mtab-msgnr

msgv1 = tmess_mtab-msgv1

msgv2 = tmess_mtab-msgv2

msgv3 = tmess_mtab-msgv3

msgv4 ! = tmess_mtab-msgv4

IMPORTING

msgtext = wa_mtab-text

EXCEPTIONS

message_not_found = 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.

wa_mtab-matnr = wa_item-matnr.

wa_mtab-msgtyp = tmess_mtab-msgtyp.

wa_mtab-msgid = tmess_mtab-msgid.

wa_mtab-msgn! r = tmess_mtab-msgnr.

APPEND wa_mtab TO t_mtab.

CLEAR wa_mtab-text.

CLEAR wa_item.

ENDLOOP.

ENDLOOP.

ENDFORM. " READ_DATA

&----


*& Form BDC_DYNPRO

&----


  • text

----


  • -->P_0300 text

  • -->P_0301 text

!

----


----


  • Start new screen *

----


FORM bdc_dynpro USING program dynpro.

CLEAR bdctab.

bdctab-program = program.

bdctab-dynpro = dynpro.

bdctab-dynbegin = 'X'.

APPEND bdctab.

ENDFORM. " BDC_DYNPRO

&----


*& Form BDC_FIELD

&----


  • text

----


  • -->P_0305 text

  • -->P_WA_HEADER_KSCHL text

----


----


  • Insert field *

----


FORM bdc_field USING fnam fval.

CLEAR bdctab.

bdctab-fnam = fnam.

bdctab-fval = fval.

APPEND bdctab.

ENDFORM. " BDC_FIELD

&----


*& Form bdc_trc_ansaction

&----


  • text

----


  • -->P_0527 text

*----


&----


*& Form f_Process

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


!

FORM f_process.

DATA : l_todate(12) TYPE c,

l_frdate(12) TYPE c.

***Select for all entries of material in Header "Flat File Materials".

IF NOT t_header[] IS INITIAL.

SELECT matnr vkorg vtweg FROM mvke INTO TABLE t_mvke FOR ALL ENTRIES

IN t_header WHERE matnr = t_header-matnr AND

vkorg = t_header-vkorg AND

vtweg = t_header-vtweg.

ENDIF.

*********select Sales.org & Dist.channel.

IF NOT t_header[] IS INITIAL.

SELECT vkorg vtweg FROM tvkov INTO TABLE t_tvkov FOR ALL ENTRIES IN

t_header WHERE vkorg = t_header-vkorg

AND vtweg = t_header-vtweg.

ENDIF.

***Checking for material in Sales Master Table

SORT t_mvke BY matnr vkorg vtweg.

SORT t_tvkov BY vkorg vtweg.

LOOP AT t_header INTO wa_header.

READ TABLE t_mvke INTO wa_mvke WITH KEY matnr = wa_header-matnr

vkorg = wa_header-vkorg

! ; vtweg = wa_header-vtweg BINARY SEARCH.

IF sy-subrc <> 0.

wa_error = wa_header.

&nb! sp; MOVE text-011 TO wa_error-text.

APPEND wa_error TO t_error.

DELETE TABLE t_header FROM wa_header.

ELSE.

********Date Validations

IF ( wa_header-datab NE ' ! ; ' AND wa_header-datbi NE ' ' ) .

l_todate = wa_header-datab.

l_frdate = wa_header-datbi.

REPLACE '.' INTO l_toda! te WITH ''.

REPLACE '.' INTO l_todate WITH ''.

CONDENSE l_todate NO-GAPS.

REPLACE '.' INTO l_frdate WITH ''.

REPLACE '.' INTO l_frdate WITH ''.

CONDENSE l_frdate NO-GAPS.

IF l_frdate < l_todate.

wa_error = wa_header .

MOVE text-012 TO wa_error-text.

APPEND wa_error TO t_error.

DELETE TABLE t_header FROM wa_header.

ENDIF.

ELSE.

wa_error = wa_header .

MOVE text-016 TO wa_error-text.

APPEND wa_error TO t_error.

DELETE TABLE t_header FROM wa_header.

ENDIF.

ENDIF.

********Rate Validation.

IF wa_header-kbetr = ' '.

wa_error = wa_header .

MOVE text-017 TO wa_error-text.

APPEND wa_error TO t_error.

DELETE TABLE t_header FROM wa_header.

ENDIF.

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

READ TABLE t_tvkov INTO wa_tvkov WITH KEY vkorg = wa_header-vkorg

BINARY SEARCH.

IF sy-subrc = 0.

READ TABLE t_tvkov INTO wa_tvkov WITH KEY vtweg = wa_header-vtweg

BINARY SEARCH.

IF sy-subrc <> 0.

wa_error = wa_header.

MOVE text-015 TO wa_error-text.

WRITE wa_header-vtweg TO wa_error-text+13(4).

APPEND wa_error TO t_error.

ENDIF.

ELSE.

wa_error = wa_header.

MOVE text-013 TO wa_error-text.

WRITE wa_header-vkorg TO wa_error-text+9(4).

APPEND wa_error TO t_error.

ENDIF.

CLEAR wa_header.

ENDLOOP.

*****Deleting Duplicate Material Form Header "Flat File Data".

SORT t_header BY kschl vkorg vtweg matnr.

DELETE ADJACENT DUPLICATES FROM t_header COMPARING

kschl! vkorg vtweg matnr .

  • ****Data Moving from Header to Item Level.

t_item[] = t_header[].

*To count No.of records in Item Table.

DESCRIBE TABLE t_item LINES v_count.

CONCATENATE text-014 ' ' v_count INTO v_bdc.

****Popup to get Confirmation from user to process BDC

CALL FUNCTION 'POPUP_TO_CONFIRM'

EXPORTING

titlebar = 'Confirmation of File Data'

text_question = v_bdc

text_button_1 = 'Confirm'

text_button_2 = 'Cancel Run'

default_button = '1'

IMPORTING

answer = c_ans.

.

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. " f_Process

*

&----


*& Form f_Refresh

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM f_refresh.

REFRESH : t_xls_file,

t_header,

t_item,

t_mvke,

t_tvkov,

t_error,

t_mtab.

CLEAR : t_xls_file,

wa_header,

wa_item,

wa_mvke,

wa_tvkov,

wa_error,

wa_mtab.

ENDFORM. " f_Refresh

Regards

Read only

Former Member
0 Likes
1,191

<b>Transaction method</b>

report ZRKBDC1

no standard page heading line-size 255.

DATA: BEGIN OF ITAB OCCURS 0,

MATNR LIKE MARA-MATNR,

MBRSH LIKE MARA-MBRSH,

MTART LIKE MARA-MTART,

MAKTX LIKE MAKT-MAKTX,

MEINS LIKE MARA-MEINS,

END OF ITAB.

include bdcrecx1.

parameters: dataset(132) lower case.

      • DO NOT CHANGE - the generated data section - DO NOT CHANGE ***

*

  • If it is nessesary to change the data section use the rules:

  • 1.) Each definition of a field exists of two lines

  • 2.) The first line shows exactly the comment

  • '* data element: ' followed with the data element

  • which describes the field.

  • If you don't have a data element use the

  • comment without a data element name

  • 3.) The second line shows the fieldname of the

  • structure, the fieldname must consist of

  • a fieldname and optional the character '_' and

  • three numbers and the field length in brackets

  • 4.) Each field must be type C.

*

      • Generated data section with specific formatting - DO NOT CHANGE ***

data: begin of record,

  • data element: MATNR

MATNR_001(018),

  • data element: MBRSH

MBRSH_002(001),

  • data element: MTART

MTART_003(004),

  • data element: XFELD

KZSEL_01_004(001),

  • data element: MAKTX

MAKTX_005(040),

  • data element: MEINS

MEINS_006(003),

  • data element: MTPOS_MARA

MTPOS_MARA_007(004),

end of record.

      • End generated data section ***

start-of-selection.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

FILENAME = 'D:\SAPPROGRAMS\BDC.TXT'

FILETYPE = 'ASC'

  • HAS_FIELD_SEPARATOR = ' '

  • HEADER_LENGTH = 0

  • READ_BY_LINE = 'X'

  • DAT_MODE = ' '

  • CODEPAGE = ' '

  • IGNORE_CERR = ABAP_TRUE

  • REPLACEMENT = '#'

  • CHECK_BOM = ' '

  • IMPORTING

  • FILELENGTH =

  • HEADER =

TABLES

DATA_TAB = ITAB

  • 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.

perform open_dataset using dataset.

perform open_group.

read dataset dataset into record.

if sy-subrc <> 0. exit. endif.

LOOP AT ITAB.

perform bdc_dynpro using 'SAPLMGMM' '0060'.

perform bdc_field using 'BDC_CURSOR'

'RMMG1-MATNR'.

perform bdc_field using 'BDC_OKCODE'

'=AUSW'.

perform bdc_field using 'RMMG1-MATNR'

record-MATNR_001.

perform bdc_field using 'RMMG1-MBRSH'

record-MBRSH_002.

perform bdc_field using 'RMMG1-MTART'

record-MTART_003.

perform bdc_dynpro using 'SAPLMGMM' '0070'.

perform bdc_field using 'BDC_CURSOR'

'MSICHTAUSW-DYTXT(01)'.

perform bdc_field using 'BDC_OKCODE'

'=ENTR'.

perform bdc_field using 'MSICHTAUSW-KZSEL(01)'

record-KZSEL_01_004.

perform bdc_dynpro using 'SAPLMGMM' '4004'.

perform bdc_field using 'BDC_OKCODE'

'=BU'.

perform bdc_field using 'MAKT-MAKTX'

record-MAKTX_005.

perform bdc_field using 'BDC_CURSOR'

'MARA-MEINS'.

perform bdc_field using 'MARA-MEINS'

record-MEINS_006.

perform bdc_field using 'MARA-MTPOS_MARA'

record-MTPOS_MARA_007.

perform bdc_transaction using 'MM01'.

ENDLOOP.

perform close_group.

perform close_dataset using dataset.

CALL TRANSACTION 'MM01' USING ITAB MODE 'A' UPDATE 'A'.

<b>session method</b>

&----


*& Report ZSAG_BS *

*& *

&----


*& *

*& *

&----


REPORT ZSAG_BS .

********TABLES

TABLES : MARA,MAKT .

*******INTERNAL TABLE

DATA : BEGIN OF ITAB OCCURS 0 ,

MATNR LIKE MARA-MATNR,

MBRSH LIKE MARA-MBRSH,

MTART LIKE MARA-MTART ,

MAKTX LIKE MAKT-MAKTX,

MEINS LIKE MARA-MEINS ,

END OF ITAB.

DATA : BDCTAB LIKE BDCDATA OCCURS 0 WITH HEADER LINE .

********WS_UPLOAD

CALL FUNCTION 'WS_UPLOAD'

EXPORTING

FILENAME = 'C:\DAT1.TXT'

FILETYPE = 'DAT'

TABLES

DATA_TAB = ITAB .

****************BDC_OPEN_GROUP

CALL FUNCTION 'BDC_OPEN_GROUP'

EXPORTING

CLIENT = SY-MANDT

GROUP = 'CHANDU'

KEEP = 'X'

USER = SY-UNAME

PROG = SY-CPROG .

LOOP AT ITAB .

REFRESH BDCTAB .

*********FIRST SCREEN

BDCTAB-PROGRAM = 'SAPLMGMM'.

BDCTAB-DYNPRO = '0060'.

BDCTAB-DYNBEGIN = 'X' .

APPEND BDCTAB .

CLEAR BDCTAB.

BDCTAB-FNAM = 'RMMG1-MATNR' .

BDCTAB-FVAL = ITAB-MATNR .

APPEND BDCTAB .

CLEAR BDCTAB.

BDCTAB-FNAM = 'RMMG1-MBRSH' .

BDCTAB-FVAL = ITAB-MBRSH .

APPEND BDCTAB .

CLEAR BDCTAB.

BDCTAB-FNAM = 'RMMG1-MTART' .

BDCTAB-FVAL = ITAB-MTART .

APPEND BDCTAB .

CLEAR BDCTAB.

BDCTAB-FNAM = 'BDC_OKCODE'.

BDCTAB-FVAL = '=AUSW' .

APPEND BDCTAB .

CLEAR BDCTAB.

********SECOND SCREEN

BDCTAB-PROGRAM = 'SAPLMGMM'.

BDCTAB-DYNPRO = '0070'.

BDCTAB-DYNBEGIN = 'X' .

APPEND BDCTAB .

CLEAR BDCTAB.

BDCTAB-FNAM = 'MSICHTAUSW-KZSEL(01)' .

BDCTAB-FVAL = 'X' .

APPEND BDCTAB .

CLEAR BDCTAB.

BDCTAB-FNAM = 'BDC_OKCODE'.

BDCTAB-FVAL = '=ENTR' .

APPEND BDCTAB .

CLEAR BDCTAB.

***************THIRD SCREEN

BDCTAB-PROGRAM = 'SAPLMGMM'.

BDCTAB-DYNPRO = '4004'.

BDCTAB-DYNBEGIN = 'X' .

APPEND BDCTAB .

CLEAR BDCTAB.

BDCTAB-FNAM = 'MAKT-MAKTX' .

BDCTAB-FVAL = ITAB-MAKTX.

APPEND BDCTAB .

CLEAR BDCTAB.

BDCTAB-FNAM = 'MARA-MEINS'.

BDCTAB-FVAL = ITAB-MEINS .

APPEND BDCTAB .

CLEAR BDCTAB.

BDCTAB-FNAM = 'BDC_OKCODE'.

BDCTAB-FVAL = '=BU' .

APPEND BDCTAB .

CLEAR BDCTAB.

CALL FUNCTION 'BDC_INSERT'

EXPORTING

TCODE = 'MM01'

TABLES

DYNPROTAB = BDCTAB .

ENDLOOP .

CALL FUNCTION 'BDC_CLOSE_GROUP' .

regards,

srinivas

<b><b>*reward for useful answers*</b></b>

Read only

Former Member
0 Likes
1,191

Thanks for Support.

Read only

0 Likes
1,191

May be you can give points for the answers you are satisfied with!

regards,

srinivas