Using Rfc Connector with Embarcadero ™ Delphi XE

Importing the Type Library

To be able to access the Rfc Connector components in Delphi, you need to import the Type Library first.

To import the Type Library, select "Component"->"Import Component" from the menu. Then, in the wizard, select "Import a Type Library" in the first screen, press "Next", then select "secept Rfc Connector" from the list in the second screen. Skip the third screen by pressing "Next", then select "Add unit to projectname" in the fourth screen.

After the wizard is complete, Delphi will create a unit which contains the necessary components for Rfc Connector.

Creating a session

To create a session, call CoRfcSession.Create() or CoSoapSession.Create(). Then use RfcSystemData (or HttpSystemData for CoSoapSession) and LogonData to configure the connection to SAP, and the client, user and password:

  // create an instance of RfcSession
  // for SOAP, use SoapSession instead
  session:=CoRfcSession.Create();
  session.RfcSystemData.ConnectString := 'SAPLOGON_ID=N4S';
 
  // set up logon data
  session.LogonData.Client := '001';
  session.LogonData.User := 'DEVELOPER';
  session.LogonData.Password := '***';
  session.LogonData.Language := 'EN';  

Connecting to SAP

To connect with the SAP system, simply call session.Connect(). Make sure to check the Error property afterwards, it will be set to True when something goes wrong, for example because of a network problem

  // connect to SAP system
  session.Connect();
  // check for error
  if session.Error then
    raise Exception.Create(session.ErrorInfo.Message);

Calling functions

To call a function, you need to import the prototype first, then you can set values in Importing (and eventually Changing and Tables, depending on the module to be called).

After calling the function, returned values will appear in Exporting, Changing and Tables.

  // import the function call
  fn := session.ImportCall('BAPI_FLIGHT_GETLIST', True);
 
  // set IMPORTING parameter (=sent to SAP)
  fn.Importing['AIRLINE'].value := 'LH';
 
  // call the function
  session.CallFunction(fn, True);
 
  // check for error
  if session.Error then
    raise Exception.Create(session.ErrorInfo.Message);
 
  // read out the data returned from the SAP system
  for I := 1 to fn.Tables['FLIGHT_LIST'].Rows.Count do begin
    row := fn.Tables['FLIGHT_LIST'].Rows[I];
    writeln(string(row['AIRLINEID'].value) +
            string(row['FLIGHTDATE'].value));
  end;

Reading Tables

If you want to read the contents of a SAP database table, you can use TableReader:

  // get a table reader
  tr := session.GetTableReader('SFLIGHT');
 
  // set a select criterium
  tr.Query.Add('CARRID EQ ''LH''');
 
  // read 100 rows, starting at the first row
  // Note: use tr.Read(0,0) to read all rows
  tr.Read(100, 0);
 
  // show the result
  for I := 1 to tr.Rows.Count do begin
    row := tr.Rows[I];
    writeln(string(row['FLDATE'].value) + ' '
      + string(row['PRICE'].value));
  end;

Example Source

The following file contains a complete sample project for RAD Studio XE2:

icon Rfc Connector Delphi XE2 Example DelphiXE2_RfcExample.zip | 29.65 kB | Date: 2012-04-11

Example how to use Rfc Connector with Delphi


Limitations

Since there is not yet a 64-bit Rfc Connector available you cannot use Rfc Connector with 64-bit Delphi executables. Regarding the availability of 64-bit Rfc Connector, please refer to Knowledgebase Entry #0005.