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

need help on BDC..

Former Member
0 Likes
895

Hi,

I have an excel file in which i have data for SAP material master.

I want to transfer this data into SAP using BDC. I know that I need to use SHDB transaction and record my transaction for material master data entry. I dont understand how I should transfer this excel data to a data file understood by SAP !! Can anyone help me with this ? I need the steps and the code if any.

thanks in advance..

Hi,

I have an excel file in which i have data for SAP material master.

I want to transfer this data into SAP using BDC. I know that I need to use SHDB transaction and record my transaction for material master data entry. I dont understand how I should transfer this excel data to a data file understood by SAP !! Can anyone help me with this ? I need the steps and the code if any.

thanks in advance..

8 REPLIES 8
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
862

Sure, there are many ways to do this. I will give one here. First make your excel sheet a comma delimted file, .csv file extension.

Notice, here I use the GUI_UPLOAD function module to upload to a flat itab, now im looping at the itab and separating the record at the commas(,) into the specific fields of another interanl table. Now using that itab, call the transaction giving the data.



report zrich_0002.

type-pools: icon, slis.

types: begin of tdata,
        rec(150) type c,
       end of tdata,

       begin of tmtgp,
        matkl type t023t-matkl,
        wgbez type t023t-wgbez,
        wgbez60 type t023t-wgbez60,
       end of tmtgp,

       begin of tmess,
        stats(4) type c,
        matkl type t023t-matkl,
        wgbez type t023t-wgbez,
        wgbez60 type t023t-wgbez60,
        message(200) type c,
       end of tmess.

data: idata type table of tdata with header line.
data: imtgp type table of tmtgp with header line.


data: mode(1) type c value 'N'.
data: messtab type table of bdcmsgcoll with header line.
data: bdcdata type table of bdcdata with header line.

<b>selection-screen begin of block b1 with frame title text-001.
parameters: p_file type localfile default 'C:Material Groups.txt'.
selection-screen end of block b1.

at selection-screen on value-request for p_file.

  call function 'KD_GET_FILENAME_ON_F4'
       exporting
            static    = 'X'
       changing
            file_name = p_file.</b>

start-of-selection.

  perform upload_data.
  perform update_table_via_bdc.


************************************************************************
* Upload_Data
************************************************************************
form upload_data.

<b>  data: filename type string.

  clear idata. refresh idata.
  filename = p_file.

  call function 'GUI_UPLOAD'
       exporting
            filename        = filename
            filetype        = 'ASC'
       tables
            data_tab        = idata
       exceptions
            file_open_error = 1
            file_read_error = 2
            no_authority    = 6
            others          = 17.

  check sy-subrc = 0.

  loop at idata.
    clear imtgp.
    split idata at ',' into imtgp-matkl imtgp-wgbez imtgp-wgbez60.
    append imtgp.
  endloop.</b>

endform.
************************************************************************
*      Form  update_table_via_bdc
************************************************************************
form update_table_via_bdc.


  loop at imtgp.

* BDC code goes here

    call transaction 'MM01' using bdcdata mode mode
                                  update 'S'
                                  messages into messtab.

    if sy-subrc = 0.
    endif.

    clear bdcdata. refresh bdcdata.
    clear messtab. refresh messtab.

  endloop.

endform.

************************************************************************
*      Form  BDC_DYNPRO
************************************************************************
form bdc_dynpro using  program dynpro.

  clear bdcdata.
  bdcdata-program = program.
  bdcdata-dynpro = dynpro.
  bdcdata-dynbegin = 'X'.
  append bdcdata.

endform.

************************************************************************
*      Form  BDC_FIELD
************************************************************************
form bdc_field using fnam fval.

  clear bdcdata.
  bdcdata-fnam = fnam.
  bdcdata-fval = fval.
  append bdcdata.

endform.

Regards,

Rich HEilman

Read only

Former Member
0 Likes
862

report zzupload.

parameters: filename like rlgrap-filename

default 'c:\my documents\yourfile.prn',

filetype default 'A',

*

data: rc type i,

type(10).

data: wa(1000).

data: filenamea type string.

"Declare this with your excel file fields

data: begin of it_tab occurs 0,

text(10) type c,

end of tab.

start-of-selection.

filenamea = filename.

call function 'GUI_UPLOAD'

exporting

filename = filenamea

filetype = 'ASC'

  • HAS_FIELD_SEPARATOR = ' '

  • HEADER_LENGTH = 0

  • READ_BY_LINE = 'X'

  • DAT_MODE = ' '

  • CODEPAGE = ' '

  • IGNORE_CERR = ABAP_TRUE

  • REPLACEMENT = '#'

  • CHECK_BOM = ' '

importing

filelength = rc

  • HEADER =

tables

data_tab = it_tab

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.

*

case sy-subrc.

when 0.

when 1.

message e999 with 'Error Opening File'. "// open-error

when 2.

message e999 with 'File Read Error'. "// read-error

when others.

message e999 with 'Undefined Error' sy-subrc.

endcase.

loop at tab.

write: / tab.

endloop.

write:/ 'No of Bytes Transferred', rc. "// transferred Bytes

Read only

0 Likes
862

..this is the BDC part..

Perform Open_group. should be written first after the GUI_UPLOAD.

in the loop at it_tab...write perform BDC_TRANSACTION USING TCODE.

at the end write perform BDC CLOSE_GROUP.

----


  • create batchinput session *

----


FORM OPEN_GROUP.

SKIP.

WRITE: /(20) 'Create group'(I01), GROUP.

SKIP.

  • open batchinput group

CALL FUNCTION 'BDC_OPEN_GROUP'

EXPORTING CLIENT = SY-MANDT

GROUP = GROUP

USER = USER

KEEP = KEEP

HOLDDATE = HOLDDATE.

WRITE: /(30) 'BDC_OPEN_GROUP'(I02),

(12) 'returncode:'(I05),

SY-SUBRC.

ENDFORM.

----


  • end batchinput session *

----


FORM CLOSE_GROUP.

  • close batchinput group

CALL FUNCTION 'BDC_CLOSE_GROUP'.

WRITE: /(30) 'BDC_CLOSE_GROUP'(I04),

(12) 'returncode:'(I05),

SY-SUBRC.

ENDFORM.

----


  • Start new transaction *

----


FORM BDC_TRANSACTION USING TCODE.

CALL FUNCTION 'BDC_INSERT'

EXPORTING TCODE = TCODE "This is your

transaction code

TABLES DYNPROTAB = BDCDATA.

IF SMALLLOG <> 'X'.

WRITE: /(25) 'BDC_INSERT'(I03),

TCODE,

(12) 'returncode:'(I05),

SY-SUBRC,

'RECORD:',

SY-INDEX.

ENDIF.

REFRESH BDCDATA.

ENDFORM.

----


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

CLEAR BDCDATA.

BDCDATA-FNAM = FNAM.

BDCDATA-FVAL = FVAL.

APPEND BDCDATA.

ENDIF.

ENDFORM.

Read only

Former Member
0 Likes
862

Hi

Hope this piece of code helps u in uploading data form excel file to internal table.

TYPES t_itab1 TYPE alsmex_tabline.

DATA: it_itab1 TYPE STANDARD TABLE OF t_itab1 WITH HEADER LINE.

DATA : v_index TYPE i.

PARAMETERS: p_file1 TYPE rlgrap-filename.

Call this subroutine to upload data from excel file to internal table.

FORM load_local_file_name.

CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'

EXPORTING

filename = p_file1

i_begin_col = 1

i_begin_row = 2

i_end_col = 50

i_end_row = 10000

TABLES

intern = it_itab1

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.

IF it_itab1[] IS INITIAL.

MESSAGE s003(zz) WITH 'No Data Uploaded'.

EXIT.

ELSE.

SORT it_itab1 BY row col.

LOOP AT it_itab1.

MOVE : it_itab1-col TO v_index.

ASSIGN COMPONENT v_index OF STRUCTURE

it_tech TO <fs>.

MOVE : it_itab1-value TO <fs>.

AT END OF row.

MOVE-CORRESPONDING it_tech TO it_tech_comp.

APPEND it_tech_comp.

CLEAR it_tech_comp.

CLEAR it_tech.

ENDAT.

ENDLOOP.

ENDIF.

Hope this helps you in uploading the data into internal table.

- Kalpana

ENDFORM.

Read only

Former Member
0 Likes
862

HI Rad,

One more best thing.After recording your transaction flow in the SHDB.Select the recording & click the button "Program".SAP will generate the program for you for that record,then you do the required changes like writing the GUI_UPLOAD instead of open dataset etc etc.& then loop at the internal table for the BDC_INSERT Function Module.

Read only

Former Member
0 Likes
862

Thanks to all !! All of you have given me an elaborate answer.

But you all need to wait for me to award the points !! I have to work on this first !!

thank u

Read only

0 Likes
862

Don't do a BDC, just use LSMW and it will take care of everything.

Read only

0 Likes
862

Here is the LSMW object to use


   Object               0020   Material master     
   Method               0000                       
   Program name         RMDATIND                   
   Program type         D   Direct input