SAP

The SAP functions are not included in projects by default. They are provided in the installation directory and can be added to a given project as an external function directory.

OpenSAPConnection

Opens a new SAP connection with a specific protocol.

void OpenSAPConnection(

connectionString,

[protocol] )

The function consists of the following elements:

connectionString

A string that contains the connection string to open the connection. This must not be NULL or empty.

protocol

An integer value that indicates the protocol to use. If this is not specified, SAP_RFC is used.

Valid values for this parameter are:

  • SAP_RFC: uses the classical RFC protocol to open a SAP connection (librfc32.dll)

  • SAP_NWRFC: uses the Netweaver protocol to open a SAP connection

Return

None

The connection is established continuously for the remaining runtime of the project. To close the connection, CloseSAPConnection() must be used with the same string as the argument.

Any function generated by the TDM Wizard for SAP BAPI must also be called with the string specified in connectionString, so that the function can use the connection that was opened with OpenSAPConnection().

The following example shows the use of the functions OpenSAPConnection() and CloseSAPConnection():

String theConnectionString = "ASHOST=XXXXXXXX LANG=XX CLIENT=XXX SYSNR=XX USER=XXXXX PASSWD=XXXXXXXX";

void OpenSAPConnection(theConnectionString, SAP_NWRFC);

// Call SAP BAPI functions here...

CloseSAPConnection(theConnectionString);

CloseSAPConnection

Closes a connection to an SAP server that was opened with OpenSAPConnection().

CloseSAPConnection(

connectionString

)

The function consists of the following elements:

connectionString

A string that contains all the necessary information to establish a connection. This must be the same string that was previously used for OpenSAPConnection().

Return

None

The following example shows the use of the functions OpenSAPConnection() and CloseSAPConnection():

String theConnectionString = "ASHOST=XXXXXXXX LANG=XX CLIENT=XXX SYSNR=XX USER=XXXXX PASSWD=XXXXXXXX";

OpenSAPConnection(theConnectionString);

// Call SAP BAPI functions here...

CloseSAPConneciton(theConnectionString);

ReadSAPTable

Reads SAP tables via a generic user function.

ReadSAPTable( connectionString, [protocol,] tableName, columns, [filter,] [numRecords] )

This function consists of the following elements:

Element

Description

connectionString

A string that contains the connection used to connect to SAP.

This is the same as the values in a previous call to OpenSAPConnection().

protocol

An integer value that indicates the protocol to use. If no value is specified, the system uses SAP_RFC. The valid values are:

  • SAP_RFC: uses the classical RFC protocol to open a SAP connection (librfc32.dll).

  • SAP_NWRFC: uses the Netweaver protocol to open a SAP connection.

This argument is ignored if a connection for the specified connection string is already established via OpenSAPConnection() and not closed yet.

If no connection is currently open, this function opens a temporary connection that uses the specified protocol.

tableName

A string that contains the name of the SAP table to read.

If this is NULL, empty, or the table does not exist, the function exits with an error.

columns

An array that contains strings with the names of the columns to read.

If this is NULL, empty, or if one of the columns does not exist, the function exits with an error.

filter

A string that contains the filter to use. The syntax of the filter is similar to SQL.

If this is NULL or empty, no filter is used.

numRecords

The maximum number of records to return.

If this is less than 1, all available records that match the filter (if specified) are returned.

Returns

A 2-dimensional array that contains all read records of the specified table that matches the filter (if specified).

The returned array is empty if no record matches the filter (if specified) or if the table is empty.

The sequence of read values in the returned array is identical to the sequence of columns specified in columns.

The following example reads 10 rows from the specified columns of the specified table with the specified filter and writes the lines into the log:

string myConnectionString = "..."; array result = ReadSAPTable(myConnectionString, "MAKT", "MATNR", "MAKTX", "SPRAS = 'EN'", 10); for integer i = 0 to Length(result) - 1 do array line = result[i]; Trace(TRACELEVEL_INFO, Format("MATNR: {0}, MAKTX: {1}", line[0], line[1]); endfor