Application Development 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: 

BDC Program

Former Member
0 Kudos

Hi,

I want to create material master using bdc session method. The data has to be transferred from csv file to sap system.

Please tell me how to achieve this and how to split the fields by commas while uploading.

Please provide me the code.

Thanks,

sriram.

8 REPLIES 8

Former Member
0 Kudos

Sriram, why don't u use the Batch input program RMDATIND to achieve the same purpose.. See the documentation for this program and it contains how the input structure must be provide..

Former Member
0 Kudos

Sriram,

You can use the SPLIT command.

Load your file data into a TEXT variable.

SPLIT variable into workarea at ','.

workarea is structure of type of the internal table which you are going to upload the data from the file.

However, I am not sure why you are using BDC instead of a BAPI method.

Regards,

Ravi

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Have you tried using the LSMW tool?

Regards,

Rich Heilman

0 Kudos

Hi Rich,

I want to use only BDCs session method for this purpose.

If you provide me the code it will be very useful for me.

Thanks,

sriram.

0 Kudos

Here is a skeleton program for upload from a comma delimted text file. It expects material number and material group. This was used to update material groups at one time. I have removed the BDC code.



report zrich_0002.

type-pools: icon, slis.

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

       begin of tmtgp,
        matnr type mara-matnr
        matkl type mara-matkl,
       end of tmtgp,

       begin of tmess,
        stats(4) type c,
        matnr type mara-matnr,
        matkl type mara-matkl,
       end of tmess.

data: idata type table of tdata with header line.
data: imtgp type table of tmtgp with header line.
data: imess type table of tmess with header line.
data: it023 type table of t023 with header line.

data: fieldcat type slis_t_fieldcat_alv.

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.

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.

start-of-selection.

  perform upload_data.
  perform update_via_bdc.
  perform call_alv.

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

  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-matnr imtgp-matkl.
    append imtgp.
  endloop.

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

  clear imess. refresh imess.

  loop at imtgp.

* BDC Code goes here

    call transaction 'MM02' using bdcdata mode mode
                                  update 'S'
                                  messages into messtab.
    if sy-subrc = 0.
      move-corresponding imtgp to imess.
      write icon_green_light as icon to imess-stats.
      imess-message = 'Transaction Completed Successfully'.
      append imess.
    else.
      move-corresponding imtgp to imess.
      write icon_red_light as icon to imess-stats.
      read table messtab index 1.
      if sy-subrc = 0.
        perform bdc_message using imess-message.
      else.
        imess-message = 'Transaction Failed'.
      endif.
      append imess.
    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.

************************************************************************
*  BDC_MESSAGE
************************************************************************
form bdc_message using message.

  clear message.

* Move parameters into message text.
  call function 'MESSAGE_PREPARE'
       exporting
            language = sy-langu
            msg_id   = messtab-msgid
            msg_no   = messtab-msgnr
            msg_var1 = messtab-msgv1(50)
            msg_var2 = messtab-msgv2(50)
            msg_var3 = messtab-msgv3(50)
            msg_var4 = messtab-msgv4(50)
       importing
            msg_text = message.

endform.

************************************************************************
*      Form  call_alv
************************************************************************
form call_alv.

* Call ABAP List Viewer (ALV)
  perform build_field_catalog.
  call function 'REUSE_ALV_GRID_DISPLAY'
       exporting
            it_fieldcat  = fieldcat
            i_grid_title = 'Transaction Messages'
       tables
            t_outtab     = imess.

endform.

************************************************************************
* BUILD_FIELD_CATALOG
************************************************************************
form build_field_catalog.

  data: tmp_fc type slis_t_fieldcat_alv with header line.

  clear: fieldcat. refresh: fieldcat.

  clear tmp_fc.
  tmp_fc-reptext_ddic = ' '.
  tmp_fc-fieldname    = 'STATS'.
  tmp_fc-tabname      = 'IMESS'.
  tmp_fc-outputlen    = '5'.
  tmp_fc-icon         = 'X'.
  append tmp_fc to fieldcat.

  clear tmp_fc.
  tmp_fc-reptext_ddic = 'Material Number'.
  tmp_fc-fieldname    = 'MATNR'.
  tmp_fc-tabname      = 'IMESS'.
  tmp_fc-outputlen    = '12'.
  append tmp_fc to fieldcat.

  clear tmp_fc.
  tmp_fc-reptext_ddic = 'Material Group'.
  tmp_fc-fieldname    = 'MATKL'.
  tmp_fc-tabname      = 'IMESS'.
  tmp_fc-outputlen    = '12'.
  append tmp_fc to fieldcat.


  clear tmp_fc.
  tmp_fc-reptext_ddic = 'Message'.
  tmp_fc-fieldname    = 'MESSAGE'.
  tmp_fc-tabname      = 'IMESS'.
  tmp_fc-outputlen    = '200'.
  append tmp_fc to fieldcat.

endform.

Regards,

Rich Heilman

0 Kudos

Please make sure to award points for helpful answers. Thanks.

Regards,

Rich Heilman

0 Kudos

Hello Sriram,

Do a recording using SHDB transaction and you will get an idea over the screens used in this process.

If the file is an .csv file then you can convert it into a comma delimited test file.

Is the data in the csv file is in legacy format or in SAP format. If not use may have to develop a conversion program to do that.

Here is a sample code.

SELECTION-SCREEN BEGIN OF BLOCK bl1 WITH FRAME TITLE text-tx1.

PARAMETERS : p_input LIKE filename-fileextern OBLIGATORY, " Input File

p_error LIKE filename-fileextern OBLIGATORY. " Output File

SELECTION-SCREEN END OF BLOCK bl1.

Specify the comma delimited text file name on the screen then upload that data into an internal table.

perform bdc_open_group.

LOOP AT i_leg. "internal table which you have to use

*-- First screen

CLEAR w_insert_line.

PERFORM bdc_dynpro USING 'SAPMM06E' '0100'.

PERFORM bdc_field USING 'BDC_CURSOR' 'RM06E-BSART'.

PERFORM bdc_field USING 'BDC_OKCODE' '/00'.

PERFORM bdc_field USING 'EKKO-LIFNR' i_sap-lifnr.

PERFORM bdc_field USING 'RM06E-BSART' c_yb.

PERFORM bdc_field USING 'EKKO-EKORG' p_ekorg.

PERFORM bdc_field USING 'EKKO-EKGRP' i_sap-ekgrp.

etc----


call transaction xxxx .....

perform bdc_close_group.

&----


*& Form OPEN_GROUP

&----


FORM open_group.

CALL FUNCTION 'BDC_OPEN_GROUP'

EXPORTING

client = sy-mandt

group = p_sess

user = p_uname

keep = 'X'.

ENDFORM. " OPEN_GROUP

&----


*& Form BDC_DYNPRO

&----


FORM bdc_dynpro USING program dynpro.

CLEAR bdcdata.

bdcdata-program = program.

bdcdata-dynpro = dynpro.

bdcdata-dynbegin = w_flg.

APPEND bdcdata.

CLEAR bdcdata.

ENDFORM. " BDC_DYNPRO

&----


*& Form BDC_FIELD

&----


FORM bdc_field USING fnam fval.

CLEAR bdcdata.

bdcdata-fnam = fnam.

bdcdata-fval = fval.

APPEND bdcdata.

CLEAR bdcdata.

ENDFORM. " BDC_FIELD

Hope this helps you.

Regards,

0 Kudos

Please award points for helpful answers and close if answered completely, Thanks.

Regards,

Rich Heilman