问题
I am trying to integrate SAP with .NET web application using SAP .NET connector.
I have successfully connected to the SAP system but when I try to create a sales order in SAP using BAPI_SALESORDER_CREATEFROMDAT1
I get following error:
Please enter sold to party or ship to party.
The code snippet I use:
public class CreateSalesOrder
{
public string CreateOrder(RfcDestination destination)
{
try
{
RfcRepository repo = destination.Repository;
IRfcFunction salesDoc = repo.CreateFunction("BAPI_SALESORDER_CREATEFROMDAT1");
IRfcFunction salesDocCommit = repo.CreateFunction("BAPI_TRANSACTION_COMMIT");
IRfcStructure salesHeader = salesDoc.GetStructure("ORDER_HEADER_IN");
IRfcTable salesItems = salesDoc.GetTable("ORDER_ITEMS_IN");
IRfcTable salesPartners = salesDoc.GetTable("ORDER_PARTNERS");
IRfcStructure salesItemsStruct = salesItems.Metadata.LineType.CreateStructure();
IRfcStructure salesPartnersStruct = salesPartners.Metadata.LineType.CreateStructure();
IRfcStructure salesPartnersStructRow2 = salesPartners.Metadata.LineType.CreateStructure();
//Sales Header
salesHeader.SetValue("DOC_TYPE", "ZDLR");
salesHeader.SetValue("SALES_ORG", "1000");
salesHeader.SetValue("DISTR_CHAN", "00");
salesHeader.SetValue("DIVISION", "00");
salesHeader.SetValue("SALES_OFF", "1001");
//Sales Items
salesItemsStruct.SetValue("ITM_NUMBER", "323");
salesItemsStruct.SetValue("PLANT", "1001");
salesItemsStruct.SetValue("MATERIAL", "20000206");
salesItemsStruct.SetValue("TARGET_QTY", "200");
salesItemsStruct.SetValue("REQ_QTY", "200");
// Partner
salesPartnersStruct.SetValue("PARTN_ROLE", "SP");
salesPartnersStruct.SetValue("PARTN_NUMB", "102003");
RfcSessionManager.BeginContext(destination);
salesDoc.Invoke(destination);
salesDocCommit.Invoke(destination);
RfcSessionManager.EndContext(destination);
return "";
}
catch (RfcCommunicationException e)
{
return e.ToString();
}
catch (RfcLogonException e)
{
// user could not logon...
return e.ToString();
}
catch (RfcAbapRuntimeException e)
{
// serious problem on ABAP system side...
return e.ToString();
}
catch (RfcAbapBaseException e)
{
return e.ToString();
// The function module returned an ABAP exception, an ABAP message
// or an ABAP class-based exception...
}
}
}
Please, suggest what I am doing wrong in the call. I properly pass partner details in the following code snippet:
// Partner
salesPartnersStruct.SetValue("PARTN_ROLE", "SP");
salesPartnersStruct.SetValue("PARTN_NUMB", "102003");
Thanks, Naveed
回答1:
sold_to_party and ship_to_party are mandantory fields.
See: BAPI_SALESORDER_CREATEFROMDAT1
And: As far as I remember, you have to add the created structures and tables to the function
RfcRepository repo = destination.Repository;
IRfcFunction salesDoc = repo.CreateFunction("BAPI_SALESORDER_CREATEFROMDAT1");
IRfcStructure salesHeader = salesDoc.GetStructure("ORDER_HEADER_IN");
salesHeader.SetValue("DOC_TYPE", "ZDLR");
salesDoc.SetStructure(salesHeader);
salesDoc.Invoke(destination);
回答2:
The below code will work with you. All you need is to append the lines to the structure.
Further, I do recommend that you use BAPI_SALESORDER_CREATEFROMDAT2 since this is the latest and you don't need to multiply the Order Qty * 1000.
I was lost in the object structure of the salesDoc but thanks to VS you can add a watch on the salesDoc and you will be able to figure out the whole structure.
RfcRepository repo = destination.Repository;
IRfcFunction salesDoc = repo.CreateFunction("BAPI_SALESORDER_CREATEFROMDAT1");
IRfcFunction salesDocCommit = repo.CreateFunction("BAPI_TRANSACTION_COMMIT");
IRfcStructure salesHeader = salesDoc.GetStructure("ORDER_HEADER_IN");
IRfcTable salesItems = salesDoc.GetTable("ORDER_ITEMS_IN");
IRfcTable salesPartners = salesDoc.GetTable("ORDER_PARTNERS");
IRfcStructure salesItemsStruct = salesItems.Metadata.LineType.CreateStructure();
IRfcStructure salesPartnersStruct = salesPartners.Metadata.LineType.CreateStructure();
//Sales Header
salesHeader.SetValue("DOC_TYPE", "ZDLR");
salesHeader.SetValue("SALES_ORG", "1000");
salesHeader.SetValue("DISTR_CHAN", "00");
salesHeader.SetValue("DIVISION", "00");
salesHeader.SetValue("SALES_OFF", "1001");
//Sales Items
salesItemsStruct.SetValue("ITM_NUMBER", "323");
salesItemsStruct.SetValue("PLANT", "1001");
salesItemsStruct.SetValue("MATERIAL", "20000206");
salesItemsStruct.SetValue("TARGET_QTY", "200");
salesItemsStruct.SetValue("REQ_QTY", "200");
salesItems.Append(salesItemsStruct); // Append to Structure
// Partner
salesPartnersStruct.SetValue("PARTN_ROLE", "AG"); //Sold to Party
salesPartnersStruct.SetValue("PARTN_NUMB", "102003");
salesPartners.Append(salesPartnersStruct); // Append to Structure
salesPartnersStruct.SetValue("PARTN_ROLE", "WE"); //Ship to Party
salesPartnersStruct.SetValue("PARTN_NUMB", "102003");
salesPartners.Append(salesPartnersStruct); // Append to Structure
RfcSessionManager.BeginContext(destination);
salesDoc.Invoke(destination);
salesDocCommit.Invoke(destination);
RfcSessionManager.EndContext(destination);
return "";
来源:https://stackoverflow.com/questions/16230723/error-please-enter-sold-to-party-or-ship-to-party-while-calling-bapi-salesorde