This section shows how to use the Transaction class to execute SAP transactions in the foreground as well as in a background process (Batch Input).
When executing transactions in a background process, mass data can be processed and transferred to the SAP system.
This technique is often used if no suitable BAPI exists.
How to use SAP Transactions #
The following sample application shows how to use the Transaction class to directly execute an SAP transaction.
In this application the user can enter a material number and the name of a plant.
By clicking a button, the SAP GUI is launched and the transaction MMBE (stock overview) is executed to list the entered materials and plants.
Tip: The installation package of ERPConnect includes the Transaction-Recorder tool. This tool records transactions and implements them to code, see Transaction-Recorder.
The code below shows how to add batch steps with the method AddStep. When connecting to SAP it is important to set the UseGui property to true. The SAP GUI is launched using the method Execute.
private void button1_Click(object sender, System.EventArgs e)
{
using (R3Connection con = new R3Connection("SAPServer", 00, "SAPUser", "Password", "EN", "800"))
{
Transaction transaction1 = new Transaction();
transaction1.Connection = con;
// Reset the batch steps
transaction1.BatchSteps.Clear();
// fill new steps
transaction1.ExecutionMode = ERPConnect.Utils.TransactionDialogMode.ShowOnlyErrors;
transaction1.TCode = "MMBE";
transaction1.AddStepSetNewDynpro("RMMMBEST", "1000");
transaction1.AddStepSetOKCode("ONLI");
transaction1.AddStepSetCursor("MS_WERKS-LOW");
transaction1.AddStepSetField("MS_MATNR-LOW", textBox1.Text);
transaction1.AddStepSetField("MS_WERKS-LOW", textBox2.Text);
// connect to SAP
con.UseGui = true;
con.Open(false);
// Run
transaction1.Execute();
}
}
Note: If you only want to execute a single transaction without adding several batch steps, simply set the property TCode and execute the transaction.
The screenshot below shows the sample program in action.
Background Processing (Batch Input) #
The following sample shows how to create a purchase order using Batch Input techniques in background processing. The transaction for creating a purchase order is ME21.
At the end the code loops over the Returns collection to check the BatchReturn objects that contain the return messages of the Batch Input transaction.
using (R3Connection con = new R3Connection("SAPServer", 00, "SAPUser", "Password", "EN", "800"))
{
con.Open(false);
Transaction trans = new Transaction();
trans.Connection = con;
trans.TCode = "ME21";
//Begin a new Dynpro
trans.AddStepSetNewDynpro("SAPMM06E", "0100");
trans.AddStepSetCursor("EKKO-EKGRP");
trans.AddStepSetOKCode("/00"); // Enter
trans.AddStepSetField("EKKO-LIFNR", "1070"); // Vendor
trans.AddStepSetField("RM06E-BSART", "NB"); // Order Type
trans.AddStepSetField("RM06E-BEDAT", "01.01.2006"); //Purch.Date
trans.AddStepSetField("EKKO-EKORG", "1000"); // Purchase Org
trans.AddStepSetField("EKKO-EKGRP", "010"); // Purchase Group
trans.AddStepSetField("RM06E-LPEIN", "T");
//Begin a new Dynpro
trans.AddStepSetNewDynpro("SAPMM06E", "0120");
trans.AddStepSetCursor("EKPO-WERKS(01)");
trans.AddStepSetOKCode("=BU");
trans.AddStepSetField("EKPO-EMATN(01)", "B-7000"); // Material
trans.AddStepSetField("EKPO-MENGE(01)", "20"); // Quantity
trans.AddStepSetField("EKPO-WERKS(01)", "1000"); // Plant
trans.Execute();
foreach (ERPConnect.Utils.BatchReturn br in trans.Returns)
MessageBox.Show(br.Message);
if (trans.Returns.Count == 0)
MessageBox.Show("No Messages");
}