This sample shows how to create and read material master data in SAP using the BAPI BAPI_MATERIAL_SAVEDATA.
About
A material object consists of multiple layers or so called views. Each view may exist multiple times, e.g., one plant view for plant 1000 and one for plant 2000 - both for the same material. To keep it simple, the provided sample code only shows how to handle the basic views. The other views work the same way.
The attributes of the basic view include:
the Material Number
the Old Material Number (which can come from a legacy system)
the Industry Sector
the Material Type (in this case HAWA - Trading Goods)
the basic measure unit (mandatory).
Write Material Master Data
The BAPI BAPI_MATERIAL_SAVEDATA can be used for both creating a new material or changing / extending an existing Material. Aside from the regular structure for submitting data (CLIENTDATA) there is an additional checkbox structure called CLIENTDATAX. Any data that is submitted in CLIENTDATA must be confirmed by an X value in the CLIENTDATAX structure.
The following sample code adds material attributes:
usingSystem;usingERPConnect;// Set your ERPConnect licenseLIC.SetLic("xxxx");usingvarconnection=newR3Connection(host:"server.acme.org",systemNumber:00,userName:"user",password:"passwd",language:"EN",client:"001"){Protocol=ClientProtocol.NWRFC,};connection.Open();RFCFunctionfunc=connection.CreateFunction("BAPI_MATERIAL_SAVEDATA");RFCStructureheader=func.Exports["HEADDATA"].ToStructure();RFCStructurebasedata=func.Exports["CLIENTDATA"].ToStructure();RFCStructurebasedatax=func.Exports["CLIENTDATAX"].ToStructure();header["MATERIAL"]="SAMPLE001";header["IND_SECTOR"]="M";// M stands for Mechanical Engineeringheader["MATL_TYPE"]="HAWA";// Type HAWA stands for Trading Goodsheader["BASIC_VIEW"]="X";// Just an X to indicate, that we want to create the basic viewbasedata["OLD_MAT_NO"]="4711";// Old material numberbasedata["BASE_UOM"]="MM";// Base Unit MM for milimeterbasedatax["OLD_MAT_NO"]="X";// X indicates, that we want to set this valuebasedatax["BASE_UOM"]="X";// X indicates, that we want to set this value// Add a row to the description text tablesRFCStructuredescriptionrow=func.Tables["MATERIALDESCRIPTION"].AddRow();descriptionrow["LANGU"]="EN";// Language of the textdescriptionrow["MATL_DESC"]="My New Material";// Actual Textfunc.Execute();// process return messageConsole.WriteLine(func.Imports["RETURN"].ToStructure()["MESSAGE"].ToString());// And Commit everythingRFCFunctionfunccommit=connection.CreateFunction("BAPI_TRANSACTION_COMMIT");funccommit.Execute();Console.WriteLine("\r\nPress Enter to exit");Console.ReadLine();
Output:
The material SAMPLE001 has been created or extended
usingSystem;usingERPConnect;// Set your ERPConnect licenseLIC.SetLic("xxxx");usingvarconnection=newR3Connection(host:"server.acme.org",systemNumber:00,userName:"user",password:"passwd",language:"EN",client:"001"){Protocol=ClientProtocol.NWRFC,};connection.Open();RFCFunctionfunc=connection.CreateFunction("BAPI_MATERIAL_GET_DETAIL");func.Exports["MATERIAL"].ParamValue="SAMPLE001";func.Execute();// Read dataRFCStructurebasedata=func.Imports["MATERIAL_GENERAL_DATA"].ToStructure();Console.WriteLine("Description Text: "+basedata["MATL_DESC"].ToString());Console.WriteLine("Old Material No: "+basedata["OLD_MAT_NO"].ToString());Console.WriteLine("Industry Sector: "+basedata["IND_SECTOR"].ToString());Console.WriteLine("Material Type: "+basedata["MATL_TYPE"].ToString());Console.WriteLine("\r\nPress Enter to exit");Console.ReadLine();
Output:
Description Text: My New Material
Old Material No: 4711
Industry Sector: M
Material Type: HAWA