-
Notifications
You must be signed in to change notification settings - Fork 14
Examples
For a comprehensive example of calling a BAPI function module, see the following gist: https://gist.github.com/mydoghasworms/41232a1b8595704b090b
require 'rubygems'
require 'nwrfc'
include NWRFC
# Log on to the SAP system
conn = Connection.new {"user" => "martin", "passwd" => "secret",
"client" => "100", "ashost" => "ajax.domain.com", "sysnr" => "00"}
# Obtain a function module definition
function = conn.get_function("STFC_DEEP_TABLE")
# Get a callable instance of the function module
fc = function.get_function_call
# Get handle on the parameter IMPORT_TAB (table)
it = fc[:IMPORT_TAB]
# Create a new row in the table and set some values
it.new_row {|r|
r[:C] = 'a'
r[:I] = 12345
r[:STR] = 'Howdy Folks'
r[:XSTR] = "\x0A\x0D\x99\xFF"
}
# Call the function
fc.invoke
# Get the number of returned rows
fc[:EXPORT_TAB].size
# Close the connection
conn.close
function = connection.get_function("RFC_READ_TABLE")
fc = function.get_function_call
fc[:DELIMITER] = '|'
fc[:QUERY_TABLE] = 'SPFLI'
# Call function
fc.invoke
# Table mixes in Enumerator
fc[:DATA].each {|row|
puts row[:WA]
}
Some tables are defined with rows that have scalar types (i.e. no structures with field names). In these cases, the row value can be read and set by using [nil]
, e.g.
row[nil]= '024'
The following code will set up a simple RFC server.
require 'rubygems'
require 'nwrfc'
include NWRFC
# Declare a new function
function = Function.new("MY_STRING")
# Create an importing STRING parameter for the function
parameter = Parameter.new(:name => "RFC_STRING", :type => :RFCTYPE_STRING, :direction=> :RFC_IMPORT)
# Add the parameter to the function
function.add_parameter(parameter)
# Register a server with the gateway
server = Server.new({:gwhost => "host.ajax.com", :program_id => "RUBYNWRFC"})
# Make sure we can kill this with Ctrl+C
trap("SIGINT") { server.disconnect }
# Start hosting the function on the server
server.serve(function) { |func|
puts func[:RFC_STRING] # Outputs whatever was sent by the calling ABAP system
}
On the ABAP system, create a TCP/IP RFC destination in transaction SM59 of type "Registered Server Program" with the same ID you gave it in the Ruby code above ("RUBYNWRFC"). If you are already running the RFC server, you can test the connection. You can also see whether the server is connected in the Gateway Monitor (transaction SMGW).
Now you need an ABAP program to call your served RFC function. The following will do. (The destination in the CALL FUNCTION statement refers to the destination you created in SM59).
report znwrfc_test.
data: str type string.
str = 'TEST'.
call function 'MY_STRING'
destination 'RUBYRFCDEST'
exporting
rfc_string = str.
With your RFC server running, run the ABAP program, which should cause 'TEST' to be printed out to the console where the server is running.