1

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?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
TeoVr81
  • 989
  • 15
  • 33

2 Answers2

1

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");
}
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Dirk Trilsbeek
  • 5,873
  • 2
  • 25
  • 23
  • Is it possible call a BAPI with a list of CUSTOMERNUMBER and receive back all the details of the custumers (list of custumers)? – TeoVr81 Dec 21 '17 at 16:11
  • That is unlikely. Typically SAP BAPIs either provide a basic list based on some search parameters or detailed information on a single object. – Dirk Trilsbeek Dec 22 '17 at 08:25
0

Nothing to add to Dirk answer. I just want to share an example of execution, for information, which I obtained from my SAP system.

Input parameters:

  • MAX_CNT: 10
  • PL_HOLD: 'X'
  • SELOPT_TAB (for information, below corresponds to SQL SELECT KUNNR AS CUSTOMER FROM KNA1 WHERE NAME1 LIKE 'C%'):
    TABNAME  FIELDNAME  FIELDVALUE
    -------  ---------  ----------
    KNA1     NAME1      C*
    

Output parameters (after execution, 10 out of 20 lines in KNA1 were extracted from SAP database):

  • RETURN:
    TYPE  ID  NUMBER  MESSAGE
    ----  --  ------  -----------------------
    S     FN  800     No errors have occurred
    
  • RESULT_TAB:
    TABNAME  FIELDNAME  FIELDVALUE          CUSTOMER    TYPE  ID  NUMBER  MESSAGE                                       MESSAGE_V1
    -------  ---------  ------------------  ----------  ----  --  ------  --------------------------------------------  ----------
    KNA1     NAME1      Corinne             0000000056  S     FN  800     No errors have occurred                               
    KNA1     NAME1      CUSTOMER 2 waldorf  0000000057  S     FN  800     No errors have occurred                               
    KNA1     NAME1      CUSTOMER 2 waldorf  0000000058  S     FN  800     No errors have occurred                               
    KNA1     NAME1      CUSTOMER 1 waldorf  0000000059  S     FN  800     No errors have occurred                               
    KNA1     NAME1      CUSTOMER 1 waldorf  0000000061  S     FN  800     No errors have occurred                               
    KNA1     NAME1      CUSTOMER 2 waldorf  0000000141  S     FN  800     No errors have occurred                               
    KNA1     NAME1      Corinne Schneller   0000000182  S     FN  800     No errors have occurred                               
    KNA1     NAME1      Customer master11   6000000007  S     FN  800     No errors have occurred                               
    KNA1     NAME1      Customer master111  6000000008  S     FN  800     No errors have occurred                               
    KNA1     NAME1      Customer master1    6000000011  I     FN  063     There are more than        10 search results        10
    
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48