Connecting to / Interfacing SAP (ERP) via OLE
Over the past few years, connecting to SAP to gather and/or input data has become more and more important in my job duties. The first obvious choice that was given to our development team was via webservices. In all honesty, that’s still the preferred method. However, for testing reasons and for a more rapid development, we have also been using OLE connectivity. The documentation I’ve found regarding this was fairly sparse and typically not directed at PB. This (being my first post on SCN) will be an attempt to make connecting to SAP via OLE a bit easier for anyone new to this. This will also be the first document of ??? showing how this method can be easily used. Any further documentation will point back to this article.
The prerequisites to accomplish this are pretty straightforward: The client must be installed on the computer making calls to SAP. If that is installed, everything in the OLE realm that’s needed should be readily available. The developer will also need the client installed.
Step one is pretty simple, and quite expected: Connect to SAP and log on.
For this to work, the only method I have had success with requires some form of a visual user control (window, object, etc) which I can place an ActiveX control on. Once you have that object, place an ActiveX control via the painter. Select the “Insert Control” tab, select “SAP Remote Function Call” from the list, and click OK.
Once the ActiveX control is placed and named, you’re ready to begin coding! I named mine ActiveX control oRFC.
Place a button or put the code in an open event. To connect, you’ll need to declare an OLEObject, instantiate it, and connect it to a new logon control.
oCONNECT = CREATE OLEObject
oCONNECT.ConnectToNewObject( ‘SAP.LogonControl.1’ )
From there, a connection object will have to be applied to your ActiveX control.
oRFC.Object.Connection = oCONNECT.NewConnection
// assign logon data below
oRFC.Object.Connection.Client = ls_client
oRFC.Object.Connection.Language = ls_language
oRFC.Object.Connection.System = ls_system
oRFC.Object.Connection.SystemNumber = ls_system_number
oRFC.Object.Connection.ApplicationServer = ls_application_server
oRFC.Object.Connection.User = ls_user
oRFC.Object.Connection.Password = ls_password
oRFC.Object.Connection.AutoLogon = TRUE
This should have your connection object ready to connect, as long as all the data you provided it is correct… and if not, it’ll prompt you for it.
oRFC.Object.Connection.Logon( 0, FALSE )
The command returns a boolean as to the status of the connection. AKA, whether or not it was successful.
The TRUE/FALSE in the logon command handle whether or not the logon screen is displayed. If set to FALSE or incomplete information is given, the logon window will appear…
Once you’re logged on, you can clean up the connection object that’s laying around…
Now that you’re connected, I’ve always been taught to clean up after yourself… Which means I need to disconnect (naturally, after you’re done doing anything requiring the SAP connection). Once again, very straightforward. Put the code in the window’s close event or in a button… Whatever floats your boat.
Now that you have the basics down, the only real suggestion I have is to include a PrivateWrite Boolean as an instance variable in the object that contains the ActiveX control. When you connect, set it to the return value of the connect command. In the beginning of the function used to connect, check to see if it’s already connected. This will save headaches if you have several functions blindly calling your connect function. This also allows you to blindly call your own disconnect function and first check the Boolean value to see if a connection was made before you attempt to disconnect.
In further documents I hope to explain better how to use this connection object. In the meantime, make up a list of your favorite SAP RFC’s. The first example will be a very simplistic way to use RFC_READ_TABLE… So stay tuned.