Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

How to use BAPI BAPI_MATERIAL_SAVEDATA

Hi,

I'm tryng to use the bapi BAPI_MATERIAL_SAVEDATA to create and change material.

Thanks to his forum, I created a simple program to understand how the BAPI works.

My problem is that the BAPI doesn't give me error message but doesn't create the material.

Any suggest?

REPORT Z_BAPI_MAT.

**********************************************************************

  • STRUCTURE DECLARATIONS *

**********************************************************************

TABLES: BAPIMATHEAD, "Headerdata

BAPI_MARA, "Clientdata

BAPI_MARAX, "Clientdatax

BAPI_MAKT, "Material description

BAPIRET2. "Return messages

DATA:V_FILE TYPE STRING.

DATA:

BEGIN OF LSMW_MATERIAL_MASTER,

MATNR(018) TYPE C, "Material number

MTART(004) TYPE C, "Material type

MBRSH(001) TYPE C, "Industry sector

WERKS(004) TYPE C, "Plant

MAKTX(040) TYPE C, "Material description

MEINS(003) TYPE C, "Base unit of measure

MATKL(009) TYPE C, "Material group

END OF LSMW_MATERIAL_MASTER.

**********************************************************************

  • INTERNAL TABLE DECLARATIONS *

**********************************************************************

*to store the input data

DATA:

BEGIN OF it_matmaster OCCURS 0.

INCLUDE STRUCTURE LSMW_MATERIAL_MASTER.

DATA:

END OF it_matmaster.

**for material description

DATA:BEGIN OF IT_MATERIALDESC OCCURS 0.

INCLUDE STRUCTURE BAPI_MAKT .

DATA:END OF IT_MATERIALDESC.

*to return messages

DATA:BEGIN OF IT_RETURN OCCURS 0.

INCLUDE STRUCTURE BAPIRET2.

DATA:END OF IT_RETURN.

DATA:BEGIN OF BAPI_MATRETURN2 OCCURS 0.

INCLUDE STRUCTURE BAPI_MATRETURN2.

DATA:END OF BAPI_MATRETURN2.

DATA:BEGIN OF BAPIRET3 OCCURS 0.

INCLUDE STRUCTURE BAPIRET2.

DATA:END OF BAPIRET3.

***********************************************************************

*

  • SELECTION SCREEN

***********************************************************************

*

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-002.

PARAMETERS:P_FILE LIKE RLGRAP-FILENAME OBLIGATORY default 'c:\test.txt'.

SELECTION-SCREEN END OF BLOCK B1 .

**********************************************************************

  • TO UPLOAD THE DATA *

**********************************************************************

START-OF-SELECTION.

CALL FUNCTION 'WS_UPLOAD'

EXPORTING

  • CODEPAGE = ' '

FILENAME = p_file

FILETYPE = 'DAT'

TABLES

data_tab = IT_MATMASTER

.

**********************************************************************

  • DATA POPULATIONS *

**********************************************************************

LOOP AT IT_MATMASTER.

BAPIMATHEAD-MATERIAL = IT_MATMASTER-MATNR.

BAPIMATHEAD-IND_SECTOR = IT_MATMASTER-Mbrsh.

BAPIMATHEAD-MATL_TYPE = IT_MATMASTER-Mtart.

BAPIMATHEAD-BASIC_VIEW = 'X'.

BAPI_MARA-MATL_GROUP = IT_MATMASTER-MATKL.

BAPI_MARA-BASE_UOM = IT_MATMASTER-MEINS.

BAPI_MARAX-MATL_GROUP = 'X'.

BAPI_MARAX-BASE_UOM = 'X'.

IT_MATERIALDESC-LANGU = 'EN'.

IT_MATERIALDESC-MATL_DESC = IT_MATMASTER-MAKTX.

append IT_materialdesc.

CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'

EXPORTING

headdata = BAPIMATHEAD

CLIENTDATA = BAPI_MARA

CLIENTDATAX = BAPI_MARAx

FLAG_ONLINE = ' '

  • FLAG_CAD_CALL = ' '

IMPORTING

RETURN = IT_RETURN

TABLES

MATERIALDESCRIPTION = IT_MATERIALDESC

RETURNMESSAGES = BAPI_MATRETURN2

.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

EXPORTING

WAIT = 'X'

IMPORTING

RETURN = BAPIRET3

.

  • Output message

WRITE:/ IT_MATMASTER-matnr,'->',

IT_RETURN-TYPE,

20 IT_RETURN-ID,

42 IT_RETURN-NUMBER,

45 IT_RETURN-MESSAGE.

loop at BAPI_MATRETURN2.

write: / BAPI_MATRETURN2.

endloop.

loop at BAPIRET3.

write: / BAPIRET3.

endloop.

ENDLOOP.

Tags:

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question