Stack Exchange
Calling BAPI_PO_CREATE using SAP.Net Connector 3.0
I am trying to create Purchase order in SAP ERP from my .NET application using SAP.net Connector 3.0.
However I am getting following error message in RETURN parameter

ME159: Function modules invoked in wrong sequence.

I am using below code:
// helper method to get destination from destination configuration
var dest = RfcConfigurationHelper.GetDestination(); 

RfcSessionManager.BeginContext(dest);

IRfcFunction rfcFunction = dest.Repository.CreateFunction("BAPI_PO_CREATE");

IRfcFunction transaction = dest.Repository.CreateFunction("BAPI_TRANSACTION_COMMIT");
transaction.SetValue("WAIT","X");

IRfcStructure header = rfcFunction["PO_HEADER"].GetStructure();
header.SetValue("DOC_TYPE", "NB");
header.SetValue("PURCH_ORG", "0001");
header.SetValue("PUR_GROUP", "001");
header.SetValue("DOC_DATE", DateTime.Now.ToString("yyyy-MM-dd"));
header.SetValue("VENDOR", "V544100170");

IRfcTable items = rfcFunction["PO_ITEMS"].GetTable();
IRfcStructure item = items.Metadata.LineType.CreateStructure();
item.SetValue("PO_ITEM", "1");
item.SetValue("PUR_MAT", "TEST_MAT");
item.SetValue("PLANT", "0001");

IRfcTable schedules = rfcFunction["PO_ITEM_SCHEDULES"].GetTable();
IRfcStructure schedule = schedules.Metadata.LineType.CreateStructure();
schedule.SetValue("PO_ITEM", "1");
schedule.SetValue("DELIV_DATE", DateTime.Now.AddDays(7).ToString("yyyy-MM-dd"));
schedule.SetValue("QUANTITY", 10);

rfcFunction.Invoke(dest);

transaction.Invoke(dest);

IRfcTable returns = rfcFunction["RETURN"].GetTable();
RfcSessionManager.EndContext(dest);

Please help. Thanks

Finally I have figured out what I was missing. It was a silly mistake, as I haven't added the row to 2 tables items and schedules, via the method Insert.
Below is the edited code, with two lines added marked // this was missing:
// helper method to get destination from destination configuration
var dest = RfcConfigurationHelper.GetDestination(); 

RfcSessionManager.BeginContext(dest);

IRfcFunction rfcFunction = dest.Repository.CreateFunction("BAPI_PO_CREATE");

IRfcFunction transaction = dest.Repository.CreateFunction("BAPI_TRANSACTION_COMMIT");
transaction.SetValue("WAIT","X");

IRfcStructure header = rfcFunction["PO_HEADER"].GetStructure();
header.SetValue("DOC_TYPE", "NB");
header.SetValue("PURCH_ORG", "0001");
header.SetValue("PUR_GROUP", "001");
header.SetValue("DOC_DATE", DateTime.Now.ToString("yyyy-MM-dd"));
header.SetValue("VENDOR", "V544100170");

IRfcTable items = rfcFunction["PO_ITEMS"].GetTable();
IRfcStructure item = items.Metadata.LineType.CreateStructure();
item.SetValue("PO_ITEM", "1");
item.SetValue("PUR_MAT", "TEST_MAT");
item.SetValue("PLANT", "0001");

// this was missing as I haven't added the row to the table
items.Insert(item);

IRfcTable schedules = rfcFunction["PO_ITEM_SCHEDULES"].GetTable();
IRfcStructure schedule = schedules.Metadata.LineType.CreateStructure();
schedule.SetValue("PO_ITEM", "1");
schedule.SetValue("DELIV_DATE", DateTime.Now.AddDays(7).ToString("yyyy-MM-dd"));
schedule.SetValue("QUANTITY", 10);

// this was missing as I haven't added the row to the table
schedules.Insert(schedule);

rfcFunction.Invoke(dest);

transaction.Invoke(dest);

IRfcTable returns = rfcFunction["RETURN"].GetTable();
RfcSessionManager.EndContext(dest);


This is a very clean & high quality of code that explains how to call SAP BAPIs from .NET, I was able to adapt your answer easily to a different BAPI. Thankyou for this.
