Online Tutorials & Training Materials | STechies.com
Register Login

MM01 Upload using BAPI

|| 4

MM01 Upload using BAPI
Stechies

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

*& 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

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

*& Written by: Karthik Potharaju

*& URL: http://allaboutsap.blogspot.com

*&---------------------------------------------------------------------*
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

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

* text

*----------------------------------------------------------------------*

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

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

* text

*----------------------------------------------------------------------*

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

* FORECASTPARAMETERS =

* FORECASTPARAMETERSX =

* PLANNINGDATA =

* PLANNINGDATAX =

* STORAGELOCATIONDATA =

* STORAGELOCATIONDATAX =

* VALUATIONDATA = BAPI_MBEW1

* VALUATIONDATAX = BAPI_MBEWX

* WAREHOUSENUMBERDATA =

* WAREHOUSENUMBERDATAX =

* SALESDATA = BAPI_MVKE1

* SALESDATAX = BAPI_MVKEX

* STORAGETYPEDATA =

* STORAGETYPEDATAX =

IMPORTING

RETURN = BAPI_RETURN

TABLES

MATERIALDESCRIPTION = IT_MAKT

* UNITSOFMEASURE =

* UNITSOFMEASUREX =

* INTERNATIONALARTNOS =

* MATERIALLONGTEXT =

* TAXCLASSIFICATIONS =

* RETURNMESSAGES =

* PRTDATA =

* PRTDATAX =

* EXTENSIONIN =

* EXTENSIONINX =

.

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


Comments

  • 22 Jun 2008 7:17 am
    Excellent work , its very much usefull for material master using BAPI , Thank you, keep this work continuing

    Mohan
  • 02 Oct 2008 12:50 pm
    Good presentation in simple way.

    Thanks a lot

    Nik
  • 20 Jan 2010 7:02 am
    Good and Clear step by step presentation.
  • 07 Nov 2012 9:55 am
    nice presentation and easy to understanding.