I need to search customers in SAP system from a C# application. I'm using C# .NET connector.
I tried to call the BAPI BAPI_CUSTOMER_FIND
to get all the customers with name that starts with "C" character, this is my code:
SAPConnectionConfigurator cfg = new SAPConnectionConfigurator();
RfcDestinationManager.RegisterDestinationConfiguration(cfg);
RfcDestination dest = RfcDestinationManager.GetDestination("mySAPdestination");
RfcRepository repo = dest.Repository;
IRfcFunction customerList = repo.CreateFunction("BAPI_CUSTOMER_FIND");
customerList.SetValue("MAX_CNT", "100");
IRfcTable searchFields = customerList.GetTable("SELOPT_TAB");
searchFields.Insert();
searchFields.CurrentRow.SetValue("COMP_CODE", "");
searchFields.CurrentRow.SetValue("TABNAME", "KNA1");
searchFields.CurrentRow.SetValue("FIELDNAME", "NAME1");
searchFields.CurrentRow.SetValue("FIELDVALUE", "C*");
customerList.Invoke(dest);
IRfcTable results = customerList.GetTable("RESULT_TAB");
The call works correctly but I don't know how to read the result. I need a list of customers but the RESULT_TAB
table has this strange structure:
comp_code bukrs char(4) Company Code
tabname tabname char(30) Table Name
fieldname fieldname char(30) Field Name
fieldvalue fieldvalue char(132) Field value
customer kunnr char(10) Customer Number
pstg_blk_g sperb_x char(1) Central posting block
pstg_blk_c sperb_b char(1) Posting block for company code
del_flag_g loevm_x char(1) Central Deletion Flag for Master Record
del_flag_c loevm_b char(1) Deletion Flag for Master Record (Company Code Level)
type bapi_mtype char(1) Message type: S Success, E Error, W Warning, I Info, A Abort
id symsgid char(20) Message Class
number symsgno numc(3) Message Number
message bapi_msg char(220) Message Text
log_no balognr char(20) Application Log: Log Number
log_msg_no balmnr numc(6) Application Log: Internal Message Serial Number
message_v1 symsgv char(50) Message Variable
message_v2 symsgv char(50) Message Variable
message_v3 symsgv char(50) Message Variable
message_v4 symsgv char(50) Message Variable
How can I get customers list? Am I calling the wrong BAPI?
You likely didn't get any results, in that case table RESULT_TAB
contains only one row with your original search parameters and a warning message. You need to set parameter PL_HOLD
to 'X'
to allow using placeholders.
When there are results, you'll see several lines in table RESULT_TAB
, with field FIELDVALUE
containing the actual customer name (because you searched in field NAME1
- change the search field and the result changes too) and CUSTOMER
containing the customer number.
If there are more results than set in MAX_CNT,
you'll see a message type I
, ID FN
, Number 063
in the last row of your result set (with a message in your logon language telling you there are more than X results).
If your search didn't yield any results at all, the structure RETURN
will contain a warning message (message type W
, ID FN
, number 802
) and the single row in table RESULT_TAB
should contain another warning message type W
, ID FN
, number 065
and an explanatory message text in your logon language telling you there were no accounts found in your search.
In case you're wondering how to read an IRfcTable
at all, you can just iterate over its contents. It is essentially a list of IRfcStructure items.
foreach(IRfcStructure row in returnTable)
{
var customerNumber = row.GetString("CUSTOMER");
}