SAP_RFC Tutorial

Perl Interface to SAP

Overview

There are occasions where an interface between SAP and the WWW will be required. While not an Oracle interface, this is a subject closely enough related to be included in this text.

This chapter provides an overview of the outside of the WWW interface to SAP. The assumption is being made that the SAP side of things, ABAP/4, system configuration and user setup has already been accomplished. If more information is needed on RFCs in general, please look at the RFC WWW site referenced in the SAP_RFC manual page.

This chapter describes how to use the SAP_RFC extension to perl to create an interface to SAP via the SAP Remote Function Call (RFC) library.

Description

A transaction using the RFC interface to SAP requires several steps for successful execution. These steps are:

  1. Open a Connection to SAP (e.g. Connect)
  2. Log in to SAP (as a user)
  3. Send a Call to SAP
  4. Receive a Call from SAP
  5. Close the Connection to SAP (e.g. Disconnect)

Notes:

Outline of an Interface Transaction using SAP_RFC

The six steps required of an SAP RFC transaction are implemented in three SAP_RFC commands. These, in sequence, are:

SAP_RFC::open
open connects to SAP and logs the user into SAP.

Because a positive response from SAP to the open command does not indicate that the user has been logged into SAP, it is highly recommended that a SAP_RFC::ping command be used to verify that the user has, in fact, been logged into SAP.

If the trace flag is set, that is the first character is "Y", a file, named dev_rfc will be created in the local directory with the contents of the exchanges to and from SAP. This file may get to be very large. It is not recommended that this flag be set except for test and debugging reasons.

SAP_RFC::callrx
callrx sends a call to SAP and waits for a response. There are two varieties of callrx. callrx1 sends one string to and receives one string from SAP. callrx2 sends one string to and receives two strings from SAP. All production interfaces in use at RNSG manufacturing return two strings. The second string contains error handling information. callrxx is the same as callrx1 except that it allows selection of the names of the records to be passed to the SAP ABAP/4 RFC program.

SAP_RFC::close
close disconnects from SAP and closes the RFC connection. There may be several callrx transactions between the open and close commands.

If more information is desired on the SAP_RFC perl extensions, please refer to the SAP_RFC manual.

Opening the Connection

Opening the connection to SAP requires that a destination SAP system, an SAP client, an SAP user and a password for the user be specified. In addition, a trace flag needs to be specified. Upon successful return from the open command a ping command should be issued to verify that the user was, in fact, logged in. A code block, in perl with SAP_RFC extensions, showing this follows:

    # Define the connection parameters 
    $sap_system = 'DEV'; 
    $sap_client = '100'; 
    $sap_user = 'TEST_USER'; 
    $sap_passwd = 'PASSWORD'; 
    $sap_trace = 'NO'. 
    $ret = 0; 
    $ret1 = 0; 
    $ret_str = '   '; 

    # Try to connect and login to SAP 
    $ret = &SAP_RFC::open($sap_system, $sap_client, $sap_user, 
                                            $sap_passwd, $sap_trace); 

    # check that open worked - verify login OK 
    if  ($ret != 0)	# open failed !! 
    { 
    	print "SAP_RFC::open() Failed !!!\n"; 
    	exit;	 # quit the program 
    }  # open failed 
    else   # OK the open worked, are we really logged in ?? 
    { 
    	($ret1, $ret_str) = &SAP_RFC::ping(); 
    	if (ret1 != 0)    # not logged in 
    	{ 
    	    print "SAP_RFC::ping()  - Could not connect. "; 
    	    print "No login !! \n"; 
    	    exit;  # quit the program 
    	} 
    	else 
    	{ 
    	    print "User $sap_user logged in to SAP System "; 
    	    print "$sap_system  in Client $sap_client  \n"; 
    	} 
    }	# end of block to verify login 

This example block of code demonstrates what is probably a minimal set of code to connect to and login to an SAP system.

Exchanging Data

We will show examples of both callrx1 and callrx2 calls. They are the same except for the handling of the returned strings.

The parameters required to execute an SAP transaction are 1) the target ABAP program and 2) the message string to be sent to the ABAP program. This example will not worry about the structure or content of the message strings.

The following code block demonstrate the execution of a single string call-receive transaction.

    # Create dummy message string
    $mess_str = "This is a test message string I could be ";
    $mess_str = $mess_str1 . "up to 248 characters long";
    # concatenate the strings 

    # Define an ABAP program as an RFC destination
    $mess_abap = "Z_TEST_TEST";
    # initialize return variables
    $ret = 0;
    $ret_str = ' ';

    ($ret, $ret_str) = &SAP_RFC::callrx1($mess_abap, $mess_str);

    if ($ret != 0)  # the transaction failed 
     #   do something about the error
    {
    	print "The transaction failed \n";  
    }
    else  # the transaction succeed
    {			    # it was OK - now what .
    	print "The transaction was OK \n"; 
    }

The following code block demonstrate the execution of a two string return call-receive transaction.

    # Create dummy message string
    $mess_str = "This is a test message string I could be ";
    $mess_str = $mess_str1 . "up to 248 characters long";
    # concatenate the strings 

    # Define an ABAP program as an RFC destination
    $mess_abap = "Z_TEST_TEST";
    # initialize return variables
    $ret = 0;
    $ret_str1 = ' ';
    $ret_str2 = ' ';

    ($ret, $ret_str1, $ret_str2) = 
    	     &SAP_RFC::callrx2($mess_abap, $mess_str);

    if ($ret != 0)  # the transaction failed 
     #   do something about the error
    {
    	print "The transaction failed \n";  
    }
    else  # the transaction succeed
    {			    # it was OK - now what .
    	print "The transaction was OK \n"; 
    }
    

Closing the Connection

This is a simple command. In fact, if it fails there really is no error handling procedure that can be taken. That aside, the command takes no parameters. The following code block demonstrates an example. This assumes that a valid SAP_RFC::open has been executed.

    # initialize the return variable 
    $ret = 0;

    # now close the connection 
    $ret = &SAP_RFC::close(); 

    if ($ret != 0) 
    { 
    	print "the SAP_RFC::close Failed !!\n"; 
    } 
    else 
    { 
    	print "the SAP_RFC::close was OK !\n"; 
    }

Summary

This chapter showed how to connect to SAP and send messages in transactions between SAP and a perl program. This provides a simple and quick means of writing simple programs to interface to SAP.

Any Questions about this document should be directed to Garth Kennedy at garth@comm.mot.com or garth@mcs.net - Copyright Motorola Inc. 1996, 1997