0% found this document useful (0 votes)
469 views6 pages

MM01 Upload Using BAPI

The document describes a program that demonstrates how to create material master data using the BAPI_MATERIAL_SAVEDATA function. It loads data from an Excel file into internal tables, deletes any header or empty records, then calls the BAPI to create the material master records, generating a report of errors or successes.

Uploaded by

Alan Carino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
469 views6 pages

MM01 Upload Using BAPI

The document describes a program that demonstrates how to create material master data using the BAPI_MATERIAL_SAVEDATA function. It loads data from an Excel file into internal tables, deletes any header or empty records, then calls the BAPI to create the material master records, generating a report of errors or successes.

Uploaded by

Alan Carino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

*&---------------------------------------------------------------------*

*& Report ZKAR_MATMAS_BAPI


*&---------------------------------------------------------------------*
*& This program demonstrates how easy it is to create Material master
*& data using BAPI_MATERIAL_SAVEDATA
*& The program also generates a report post-execution displaying errors
*& as well as successful uploads
*&---------------------------------------------------------------------*
REPORT zkar_matmas_bapi.
*----------------------------------------------------------------------*
* TABLES
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* FLAGS *
*----------------------------------------------------------------------*
DATA: f_stop. " Flag used to stop processing
*----------------------------------------------------------------------*
* DATA DECLARATIONS *
*----------------------------------------------------------------------*
DATA: v_empty TYPE i, " No. of empty records
      v_total TYPE i. " Total no. of records.
*----------------------------------------------------------------------*
* STRUCTURES & INTERNAL TABLES
*----------------------------------------------------------------------*
*BAPI structures
*----------------------------------------------------------------------*
DATA: bapi_head LIKE bapimathead, " Header Segment with Control Information
      bapi_makt LIKE bapi_makt, " Material Description
      bapi_mara1 LIKE bapi_mara, " Client Data
      bapi_marax LIKE bapi_marax, " Checkbox Structure for BAPI_MARA
      bapi_marc1 LIKE bapi_marc, " Plant View
      bapi_marcx LIKE bapi_marcx, " Checkbox Structure for BAPI_MARC
      bapi_mbew1 LIKE bapi_mbew, " Accounting View
      bapi_mbewx LIKE bapi_mbewx, " Checkbox Structure for BAPI_MBEW
      bapi_return LIKE bapiret2. " Return Parameter
*--- Internal table to hold excel file data
DATA: it_intern TYPE alsmex_tabline OCCURS 0 WITH HEADER LINE.
*--- Internal table to hold Matetrial descriptions
DATA: BEGIN OF it_makt OCCURS 100.
        INCLUDE STRUCTURE bapi_makt.
DATA: END OF it_makt.
*--- Internal to hold the records in the text file
DATA : BEGIN OF it_data OCCURS 100,
            werks(4), " Plant
            mtart(4), " Material type
            matnr(18), " Material number
            matkl(9) , " Material group
            mbrsh(1), " Industry sector
            meins(3), " Base unit of measure
            gewei(3), " Weight Unit
            spart(2), " Division
            ekgrp(3), " Purchasing group
            vprsv(1), " Price control indicator
            stprs(12), " Standard price
            peinh(3), " Price unit
            spras(2), " Language key
            maktx(40), " Material description
            END OF it_data.
*----------------------------------------------------------------------*
* SELECTION SCREEN. *
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK scr1 WITH FRAME TITLE text-111.
PARAMETER : p_file TYPE rlgrap-filename OBLIGATORY DEFAULT " Input File
'C:\Material_master.XLS'.
PARAMETER : p_max(4) OBLIGATORY DEFAULT '100'. " no.of recs in a session
PARAMETERS: p_header TYPE i DEFAULT 0. " Header Lines
PARAMETERS: p_begcol TYPE i DEFAULT 1 NO-DISPLAY,
p_begrow TYPE i DEFAULT 1 NO-DISPLAY,
p_endcol TYPE i DEFAULT 100 NO-DISPLAY,
p_endrow TYPE i DEFAULT 32000 NO-DISPLAY.
SELECTION-SCREEN END OF BLOCK scr1.
*---------------------------------------------------------------------*
* AT SELECTION-SCREEN *
*---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
*--- Validating file
  PERFORM validate_file USING p_file.
*----------------------------------------------------------------------*
* START-OF-SELECTION
*----------------------------------------------------------------------*
START-OF-SELECTION.
*--- Perform to convert the Excel data into an internal table
  PERFORM convert_xls_itab.
  IF NOT it_data[] IS INITIAL.
*--- Perform to delete Header lines
    PERFORM delete_header_empty_recs.
  ENDIF.
*----------------------------------------------------------------------*
* END OF SELECTION. *
*----------------------------------------------------------------------*
END-OF-SELECTION.
*--- Perform to upload Material Master data
  PERFORM upload_matmas.
*----------------------------------------------------------------------*
* Form : validate_input_file
*----------------------------------------------------------------------*
* Description : To provide F4 help for file if read from PC
*----------------------------------------------------------------------*
FORM validate_file USING f_file TYPE rlgrap-filename.
  CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
    CHANGING
      file_name     = f_file
    EXCEPTIONS
      mask_too_long = 1
      OTHERS        = 2.
  IF sy-subrc <> 0.
    MESSAGE s010(zlkpl_msgclass). " 'Error in getting filename'.
  ENDIF.
ENDFORM. " validate_input_file
*&---------------------------------------------------------------------*
*& Form CONVER_XLS_ITAB
*&---------------------------------------------------------------------*
FORM convert_xls_itab.
  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename    = p_file
      i_begin_col = p_begcol
      i_begin_row = p_begrow
      i_end_col   = p_endcol
      i_end_row   = p_endrow
    TABLES
      intern      = it_intern.
  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 to move the data into an internal data
  PERFORM move_data.
ENDFORM. " CONVERT_XLS_ITAB
*&---------------------------------------------------------------------*
*& Form MOVE_DATA
*&---------------------------------------------------------------------*
FORM move_data.
  DATA : lv_index TYPE i.
  FIELD-SYMBOLS <fs>.
*--- Sorting the internal table
  SORT it_intern BY row col.
  CLEAR it_intern.
  LOOP AT it_intern.
    MOVE it_intern-col TO lv_index.
*--- Assigning the each record to an internal table row
    ASSIGN COMPONENT lv_index OF STRUCTURE it_data TO <fs>.
*--- Asigning the field value to a field symbol
    MOVE it_intern-value TO <fs>.
    AT END OF row.
      APPEND it_data.
      CLEAR it_data.
    ENDAT.
  ENDLOOP.
ENDFORM. " MOVE_DATA
*&---------------------------------------------------------------------*
*& Form DELETE_HEADER_EMPTY_RECS
*&---------------------------------------------------------------------*
* To delete the Header and empty records
*----------------------------------------------------------------------*
FORM delete_header_empty_recs.
  DATA: lv_tabix LIKE sy-tabix.
  IF NOT p_header IS INITIAL.
    LOOP AT it_data.
      IF p_header > 0 AND NOT it_data IS INITIAL.
        DELETE it_data FROM 1 TO p_header.
* P_HEADER = 0.
        EXIT.
      ENDIF.
    ENDLOOP.
  ENDIF.
  CLEAR it_data.
*--- To delete the empty lines from internal table
  LOOP AT it_data.
    lv_tabix = sy-tabix.
    IF it_data IS INITIAL.
      v_empty = v_empty + 1.
      DELETE it_data INDEX lv_tabix..
    ENDIF.
  ENDLOOP.
  CLEAR it_data.
*--- Total no of recs in file
  DESCRIBE TABLE it_data LINES v_total.
  IF v_total = 0.
    MESSAGE i013(zlkpl_msgclass). " No records in the file
    f_stop = 'X'.
    STOP.
  ENDIF.
ENDFORM. " DELETE_HEADER_EMPTY_RECS
*&---------------------------------------------------------------------*
*& Form UPLOAD_MATMAS
*&---------------------------------------------------------------------*
* to upload Material Master data
*----------------------------------------------------------------------*
FORM upload_matmas .
  LOOP AT it_data.
* Header
    bapi_head-material = it_data-matnr.
    bapi_head-ind_sector = it_data-mbrsh.
    bapi_head-matl_type = it_data-mtart.
    bapi_head-basic_view = 'X'.
    bapi_head-purchase_view = 'X'.
    bapi_head-account_view = 'X'.
* Material Description
    REFRESH it_makt.
    it_makt-langu = it_data-spras.
    it_makt-matl_desc = it_data-maktx.
    APPEND it_makt.
* Client Data - Basic
    bapi_mara1-matl_group = it_data-matkl.
    bapi_mara1-base_uom = it_data-meins.
    bapi_mara1-unit_of_wt = it_data-gewei.
    bapi_mara1-division = it_data-spart.
    bapi_marax-matl_group = 'X'.
    bapi_marax-base_uom = 'X'.
    bapi_marax-unit_of_wt = 'X'.
    bapi_marax-division = 'X'.
* Plant - Purchasing
    bapi_marc1-plant = it_data-werks.
    bapi_marc1-pur_group = it_data-ekgrp.
    bapi_marcx-plant = it_data-werks.
    bapi_marcx-pur_group = 'X'.
* Accounting
    bapi_mbew1-val_area = it_data-werks.
    bapi_mbew1-price_ctrl = it_data-vprsv.
    bapi_mbew1-std_price = it_data-stprs.
    bapi_mbew1-price_unit = it_data-peinh.
    bapi_mbewx-val_area = it_data-werks.
    bapi_mbewx-price_ctrl = 'X'.
    bapi_mbewx-std_price = 'X'.
    bapi_mbewx-price_unit = 'X'.
*--- BAPI to create material
    CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
      EXPORTING
        headdata            = bapi_head
        clientdata          = bapi_mara1
        clientdatax         = bapi_marax
        plantdata           = bapi_marc1
        plantdatax          = bapi_marcx
      IMPORTING
        return              = bapi_return
      TABLES
        materialdescription = it_makt.
    IF bapi_return-type = 'E'.
      WRITE:/ 'Error:' ,bapi_return-message ,'for material:' ,it_data-matnr.
    ELSEIF bapi_return-type = 'S'.
      WRITE: 'Successfully created material' ,it_data-matnr.
    ENDIF.
  ENDLOOP.
ENDFORM. " UPLOAD_MATMAS

You might also like