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: 

Help with BDCs and flat files.....

Former Member
0 Kudos

Hi,

one help required. This is regarding Contracts in SAP

Transaction va41.

Can anyone of you here..give me a flat file

having details of "contracts" with header information and item level information, and a BDC program to upload it to SAP? I'm sure someone would have worked here with a similar scenario. For that matter any transaction, can anyone give me a flat file containing header and item details and a BDC to read the flat file and upload data onto SAP.

6 REPLIES 6

Former Member
0 Kudos

Hey any help...?

0 Kudos

Run the transaction thru SHDB and create a recording. Then implement that BDC code in a program. Here is a sample "Upload" program which uses a BDC. This particular program allocates BOMs to plants. You can use this program as a template for your program.



report zmmbomallplt
       no standard page heading.

type-pools: icon, slis.

types: begin of tflatf,
        rec(300) type c,
       end of tflatf,

       begin of tdata,
       matnr type mast-matnr,
       werks type mast-werks,
       stlan type mast-stlan,
       zwerk type mast-werks,
       end of tdata,

       begin of tmess,
        status type icon,
        matnr type mast-matnr,
        werks type mast-werks,
        stlan type mast-stlan,
        zwerk type mast-werks,
        mgtxt(100) type c,
       end of tmess.

data: iflatf type table of tflatf.
data: idata  type table of tdata  with header line.
data: imess  type table of tmess  with header line.
data: bdcmess type table of bdcmsgcoll with header line.
data: bdcdata type table of bdcdata    with header line.
data: fieldcat type slis_t_fieldcat_alv.

* Selection Screen
selection-screen begin of block b1 with frame title text-002 .
parameters: p_file type localfile default 'C:BomAllocation.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.

at selection-screen.

  clear idata. refresh idata.
  perform upload_data.
  perform check_file_format.


start-of-selection.

  clear imess. refresh imess.

  perform show_status using 'Processing File'.

  loop at idata.
    perform call_transaction.
  endloop.

  perform call_alv.

************************************************************************
* CHECK_FILE_FORMAT
************************************************************************
form check_file_format.

  data: xflatf like line of iflatf.
  data: xmara type mara.
  data: xt001w type t001w.
  data: message(100) type c.

  read table iflatf into xflatf index 1.
  search xflatf-rec for ','.
  if sy-subrc <> 0.
    message e016(mm).
  else.
    loop at iflatf into xflatf.
      split xflatf-rec at ',' into idata-matnr idata-werks
                                   idata-stlan idata-zwerk.

* Convert material number to internal.
      call function 'CONVERSION_EXIT_MATN1_INPUT'
           exporting
                input  = idata-matnr
           importing
                output = idata-matnr.

      select single * from mara into xmara
                 where matnr = idata-matnr.
      if sy-subrc <> 0.
        message e398(00) with 'Material' space idata-matnr
                  'is not a valid material. Please check file'.
      endif.

      select single * from t001w into xt001w
                 where werks = idata-werks.
      if sy-subrc <> 0.
        message e398(00) with 'Plant' idata-werks
                          'is not a valid plant. Please check file.'.
      endif.

      select single * from t001w into xt001w
                 where werks = idata-zwerk.
      if sy-subrc <> 0.
        message e398(00) with 'Allocate To Plant' idata-zwerk
                          'is not a valid plant. Please check file.'.
      endif.

      append idata.
    endloop.
  endif.

endform.

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

  data: filename type string.

  filename = p_file.

  perform show_status using 'Uploading File..........'.

  call method cl_gui_frontend_services=>gui_upload
         exporting
              filename                = filename
              filetype                = 'ASC'
         changing
              data_tab                = iflatf
         exceptions
              file_open_error         = 1
              file_read_error         = 2
              no_batch                = 3
              gui_refuse_filetransfer = 4
              no_authority            = 6
              unknown_error           = 7
              bad_data_format         = 8
              unknown_dp_error        = 12
              access_denied           = 13
              others                  = 17.

  if sy-subrc ne 0.
    message id sy-msgid type sy-msgty number sy-msgno
                with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    exit.
  endif.

endform.

************************************************************************
*  FORM CALL_TRANSACTION
************************************************************************
form call_transaction.

  data: bdc_mode(1) type c value 'N'.

  perform bdc_dynpro      using 'SAPLCSAL' '0100'.
  perform bdc_field       using 'BDC_CURSOR'
                                'RC29N-ZWERK'.
  perform bdc_field       using 'BDC_OKCODE'
                                '/00'.
  perform bdc_field       using 'RC29N-MATNR'
                          idata-matnr.
  perform bdc_field       using 'RC29N-WERKS'
                          idata-werks.
  perform bdc_field       using 'RC29N-STLAN'
                          idata-stlan.
  perform bdc_field       using 'RC29N-ZWERK'
                          idata-zwerk.
  perform bdc_dynpro      using 'SAPLCSAL' '0120'.
  perform bdc_field       using 'BDC_CURSOR'
                                'RC29K-STLNR'.
  perform bdc_field       using 'BDC_OKCODE'
                                '=FCBU'.

  call transaction 'CS07' using bdcdata
          mode bdc_mode
          update 'S'
          messages into bdcmess.

* Handle "Success" message.
  read table bdcmess with key msgid = '29'
                              msgnr = '230'.
  if sy-subrc = 0.
    perform prepare_message using imess-mgtxt.
    move-corresponding idata to imess.
    write icon_green_light as icon to imess-status.
    append imess.
  else.
* Handle "Error Messages" messsages
    read table bdcmess with key msgtyp = 'E'.
    if sy-subrc = 0.
      perform prepare_message using imess-mgtxt.
    else.
      imess-mgtxt = 'Unknown Error'.
    endif.
    move-corresponding idata to imess.
    write icon_red_light as icon to imess-status.
    append imess.
  endif.

  clear: bdcdata, bdcmess.
  refresh: bdcdata, bdcmess.

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.

************************************************************************
*  FORM PREPARE_MESSAGE
************************************************************************
form prepare_message using message.

* Prepare message.
  clear message.
  call function 'MESSAGE_PREPARE'
       exporting
            language = sy-langu
            msg_id   = bdcmess-msgid
            msg_no   = bdcmess-msgnr
            msg_var1 = bdcmess-msgv1(50)
            msg_var2 = bdcmess-msgv2(50)
            msg_var3 = bdcmess-msgv3(50)
            msg_var4 = bdcmess-msgv4(50)
       importing
            msg_text = message.

endform.

************************************************************************
* SHOW_STATUS
************************************************************************
form show_status using text.

  call function 'SAPGUI_PROGRESS_INDICATOR'
       exporting
            text = text.

endform.

************************************************************************
*  CALL_ALV
************************************************************************
form call_alv.

  clear: fieldcat. refresh: fieldcat.

  data: tmp_fc type slis_fieldcat_alv .

  clear tmp_fc.
  tmp_fc-reptext_ddic = ' '.
  tmp_fc-fieldname    = 'STATUS'.
  tmp_fc-tabname      = 'IMESS'.
  tmp_fc-outputlen    = '4'.
  append tmp_fc to fieldcat.

  clear tmp_fc.
  tmp_fc-reptext_ddic = 'Material Number'.
  tmp_fc-fieldname    = 'MATNR'.
  tmp_fc-ref_fieldname = 'MATNR'.
  tmp_fc-ref_tabname  =  'MARA'.
  tmp_fc-tabname      = 'IMESS'.
  tmp_fc-outputlen    = '18'.
  append tmp_fc to fieldcat.

  clear tmp_fc.
  tmp_fc-reptext_ddic = 'Plant'.
  tmp_fc-fieldname    = 'WERKS'.
  tmp_fc-tabname      = 'IMESS'.
  tmp_fc-outputlen    = '4'.
  append tmp_fc to fieldcat.

  clear tmp_fc.
  tmp_fc-reptext_ddic = 'Bom Usage'.
  tmp_fc-fieldname    = 'STLAN'.
  tmp_fc-tabname      = 'IMESS'.
  tmp_fc-outputlen    = '4'.
  append tmp_fc to fieldcat.

  clear tmp_fc.
  tmp_fc-reptext_ddic = 'Allocate To Plant'.
  tmp_fc-fieldname    = 'ZWERK'.
  tmp_fc-tabname      = 'IMESS'.
  tmp_fc-outputlen    = '4'.
  append tmp_fc to fieldcat.

  clear tmp_fc.
  tmp_fc-reptext_ddic = 'Message'.
  tmp_fc-fieldname    = 'MGTXT'.
  tmp_fc-tabname      = 'IMESS'.
  tmp_fc-outputlen    = '60'.
  append tmp_fc to fieldcat.

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

endform.

Regards,

Rich Heilman

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Please award points if this post was helpful. Thanks.

Regards,

Rich Heilman

0 Kudos

Hi Rich,

Thanks for the program. Can you provide me the flat file also.........just a request.

0 Kudos

One more help guys....

The above program was for BOM creation.

Does anyone have a BDC program that uploads HEADER info. and ITEM information from a flat file onto SAP.

0 Kudos

Anybody??