How to get result of a RFC

user3004443 picture user3004443 · Nov 18, 2013 · Viewed 12.5k times · Source

I am totally new when it comes to fetching the data from a SAP RFC. I am following many links to make a connection to the SAP with the following set of codes.

<appSettings> 
   <add key=”ServerHost” value=”127.0.0.1″/>
   <add key=”SystemNumber” value=”00″/>
   <add key=”User” value=”sample”/>  
   <add key=”Password” value=”pass”/>
   <add key=”Client” value=”50″/>
   <add key=”Language” value=”EN”/>
   <add key=”PoolSize” value=”5″/>
   <add key=”PeakConnectionsLimit” value=”10″/>
   <add key=”IdleTimeout” value=”600″/>   
</appSettings>

cs file

SAPSystemConnect sapCfg = new SAPSystemConnect();
RfcDestinationManager.RegisterDestinationConfiguration(sapCfg);
RfcDestination rfcDest = null;
rfcDest = RfcDestinationManager.GetDestination(“Dev”);.

Now I don't know how to call a specific RFC and get its result in Dataset or a list. I have installed SAP.net Connector also.

Please help.

Answer

Dirk Trilsbeek picture Dirk Trilsbeek · Nov 24, 2013

After creating a working RFC destination you have to create an IRfcFunction object using the RfcDestionation-Method CreateFunction:

var rfc_read_table = rfcDest.CreateFunction("RFC_READ_TABLE");

now you can use the created object to set parameters. Import/Export-Parameters can be set or retrieved using the SetValue or GetValue-methods:

// we want to read from table kna1 (customer master data)
rfc_read_table.SetValue("QUERY_TABLE", "kna1");
// Delimiter ';'
rfc_read_table.SetValue("DELIMITER", ";");

table paramters are accessed using method GetTable. It returns an IRfcTable object representing the table parameter:

var fieldsTable = rfc_read_table.GetTable("FIELDS");
// append a new row
fieldsTable.Append();
// set value for field "FIELDNAME" on new row
fieldsTable.SetValue("FIELDNAME", "mandt");
fieldsTable.Append();
fieldsTable.SetValue("FIELDNAME", "kunnr");

now you can execute the function

rfc_read_table.Invoke(rfcDest);

RFC_READ_TABLE returns its data in parameter table "data" of type TAB512 with a single field called "WA". After invoking the function, create a new IRfcTable object using method GetTable and iterate over its contents.

var returnTable = rfc_read_table.GetTable("DATA");
foreach(IRfcStructure row in returnTable)
{
   string[] fieldValues = row.GetValue("WA").Split(';');
   // process field values
}

please note that this is just an example. The function module RFC_READ_TABLE for instance isn't released for customers, so no support from SAP for using this function. I hope i got the syntax right, i don't have an IDE available right now and can't check for errors.