Accessing SAP from LotusScript using the LC LSX

The LC LSX can be used to develop LotusScript programs that can be invoked through the Domino® server agent manager or as an HEI Scripted Activity.

There are four properties and methods to be especially aware of. You need them to make an SAP transaction:
<LCConnection>.Procedure
<LCConnection>.ModuleType
<LCConnection>.ScreenFields
<LCConnection>.Message
Where LCConnection is a connection object, the following additional property must be called:
<LCConnection>.EnableSAPGUI

This property is used during log on to the SAP system if your RFC or BAPI invokes the SAPGUI. It is only available on Win32.

The RFCs and Transactions in SAP change their interfaces from release to release. Therefore, the Connector is supplied with the following property to enable you to write conditional LotusScript code should you need to work around these differences.
<LCConnection>.SAPVersion

This is a read-only property that is available after a successful <LCConnection>.Connect is called.

See the LSX examples that follow to see how you use these methods and properties.

Example 1

The following LC LSX code examples of Direct Transfer are provided as an aide to you in developing your own code. They demonstrate code that is used to access and move data from SAP® using the LC LSX.
' This script performs the following actions:
' First all documents in our PR Database of form type MATERIALGROUP are 
' opened and the value of field T_MAT_GROUPSMATKL
' is obtained. This value is used to build the Select statement in our
' call to ECC's MM_MATERALS_FOR_GROUP.
' We call this RFC once for each group document found.
' We then take the result set of each of these calls and create Domino 
' documents from them - of form type MATERIAL.
' We are finished when all materials for each material group have been 
' downloaded to Domino. 

' Variable declarations
Dim r3src As New LCConnection ("sap")     
	' This object will be our connection to SAP
Dim notessrc As New LCConnection("notes")  
	' This object is our connection to 'MATERIALGROUP documents in our PR db
Dim dest As New LCConnection ("notes") 
	' This object is used to write the MATERIALs to Domino
Dim notessrcfldLst As New LCFieldList
Dim r3srcfldLst As New LCFieldList
Dim destfldLst As LCFieldList
Dim ref As LCField
Dim r3count, notescount, inserted As Integer
Dim commandstatement As String
Dim msg As New LCStream
Dim groupno As New LCField (LCTYPE_TEXT, 1)

	' Set the appropriate properties to connect to SAP
r3src.Database = "MM_MATERIALS_FOR_GROUP"
r3src.Userid = "DSERVER"
r3src.Password = "XXXX"
r3src.Destination = "LD1"
r3src.Client="101"
r3src.SystemNo=11
r3src.Language="E"
r3src.Server = "pr1.gg.com"

	'Set the properties to connect to Notes
dest.Server = "Sales"
dest.Database= "purch.nsf"

notessrc.Server = "Sales"
notessrc.Database = "purch.nsf"

	' Connect to our two data sources and one data destination 
r3src.Connect
notessrc.Connect
dest.Connect

	'Want data from the table T_MATERIALS returned by M_MATERIALS_FOR_GROUP
r3src.Metadata = "T_MATERIALS"

    'Use the MATERIALGROUP form for the Domino data source
notessrc.Metadata = "MATERIALGROUP"

	'Use the MATERIAL form for the destination data
dest.Metadata = "MATERIAL"

	'Our form has same field names as in our modified result set 'from SAP
dest.MapByName = True

	' We want all of the documents of form "MATERIALGROUP"
commandstatement = "SELECT @ALL"

	'Get the result set with all of the MATERIALGROUP documents
notescount = notessrc.Execute(commandstatement, notessrcfldLst)
If notescount <> 0 Then
          
    ' Fetch a record from the result set
     notescount =  notessrc.Fetch (notessrcfldLst, 1, 1)
     While notescount <> 0  
	'0 is returned when there are no more records
    	' Now get the T_MAT_GROUPMATKL field's value & put into a variable
          Set groupno =  notessrcfldLst.GetField(3)
               
    	' Build the select statement for our call to MM_MATERIALS_FOR_GROUP
          commandstatement = +_ "I_LANGUAGE=""E"",I_PLANT=""1000"",I_MATERIAL_GROUP=""" +_  "groupno.text(0) + """" "
               
        	' Now get the MATERIALs for the current MATERIALGROUP
          r3count = r3src.Execute (commandstatement,  r3srcfldLst)

  While (r3count > 0) 
    
        	' You want to have the group number saved in your MATERIAL
       	'documents in Domino - but it is not returned in the
        	' call to MM_MATERIALS_FOR_GROUP. Therefor, make a copy 
       	'of your SAP result set, add a new field
        	' to the current record, and place your group value in this 
       	'field. Then use this copy result set to send to Domino
        	' which already has a form waiting for this extra field.
     Set destfldLst = r3srcfldLst.Copy
     Set ref = +_ "destfldLst.Append("MATERIAL_GROUP",LCTYPE_TEXT)"
                    msg.text = groupno.text(0)
     Call ref.SetStream (1, msg)
                    
    		' Insert our modified found row into Domino
      Call dest.Insert(destfldLst, 1, r3count)
      Delete destfldLst  'You do not need this copy anymore
    
   		'Get the next row from the real SAP result set
      r3count = r3src.Fetch(r3srcfldLst,1,1)
    
   Wend
               
       	'We clean up because the next r3src.Execute needs an empty clean list
          Delete r3srcfldLst 
          Set r3srcfldLst = New LCFieldList
     
      	'Get the next MATERIALGROUP document
          notescount =  notessrc.Fetch(notessrcfldLst, 1, 1)
 Wend
End If

Print "All MATERIALs successfully downloaded."

Example 2

This script shows calling a transaction ME21 against version 3.1H. You may need to modify this to work against other versions.
Dim session As New LCSession
Dim srcCon As New LCConnection ("notes")
Dim destCon As New LCConnection ("sap")  
Dim fldLstParms As New LCFieldList (1, 0)
Dim fldLstResult As New LCFieldList (1, 0)
Dim fldParm As LCField 
Dim count As Long
Dim mb As Long
Dim fldName As String
Dim wintitle As String

On Error Goto ErrorHandler
mb =	MB_IconInformation + MB_OK
    	wintitle = "Connection Call Example"
    	
    	REM set properties to connect to both data sources
    	destCon.Userid = "muster"
    	destCon.Password = "chorus"
    	destCon.Client="800"
    	destCon.SystemNo=0
    	destCon.Language="E"
    	destCon.Server = "/H/sapgate1/H/155.51.64.20"
    	destCon.Destination = "LD1[Public]"
     
    	REM set the connection property to the stored procedure name
    	destCon.Procedure = "ME21"
    	REM set the flag that tells us we are calling an SAP transaction     
    	destCon.ModuleType = 1
    	
   	REM set the SAP Connector Options    
    	destCon.ScreenFields = + _ 
"$SAPMM06E=100,EKKO-LIFNR,EKKO-EKORG,EKKO-EKGRP,BDC_OKCODE=/00," +_ "EKPO-EMATN(01),EKPO-EMATN(02),EKPO-MENGE(01),EKPO-MENGE(02), " +_ "EKPO-WERKS(01), EKPO-WERKS(02),EKPO-LGORT(01),EKPO-LGORT(02), " +_ "RM06E-EEIND(01),RM06E-EEIND(02),BDC_OKCODE=BU"

     REM now connect
     destCon.Connect

Set fldParm = fldLstParms.Append ("EKKO-LIFNR", LCTYPE_TEXT)
    	fldParm.text = "100"
    	Set fldParm = fldLstParms.Append ("EKKO-EKORG", LCTYPE_TEXT)
    	fldParm.text = "1000"
    	Set fldParm = fldLstParms.Append ("EKKO-EKGRP", LCTYPE_TEXT)
  	fldParm.text = "000"
     Set fldParm = fldLstParms.Append ("EKPO-EMATN(01)", LCTYPE_TEXT)
     fldParm.text = "AM2-520"
     Set fldParm = fldLstParms.Append ("EKPO-EMATN(02)", LCTYPE_TEXT)
     fldParm.text = "AM2-520"
     Set fldParm = fldLstParms.Append ("EKPO-MENGE(01)", LCTYPE_TEXT)
     fldParm.text = "5"
     Set fldParm = fldLstParms.Append ("EKPO-MENGE(02)", LCTYPE_TEXT)
     fldParm.text = "7"
     Set fldParm = fldLstParms.Append ("EKPO-WERKS(01)", LCTYPE_TEXT)
     fldParm.text = "1000"
     Set fldParm = fldLstParms.Append ("EKPO-WERKS(02)", LCTYPE_TEXT)
     fldParm.text = "1000"
     Set fldParm = fldLstParms.Append ("EKPO-LGORT(01)", LCTYPE_TEXT)
     fldParm.text = "0001"
     Set fldParm = fldLstParms.Append ("EKPO-LGORT(02)", LCTYPE_TEXT)
     fldParm.text = "0001"
     Set fldParm = fldLstParms.Append ("RM06E-EEIND(01)", LCTYPE_TEXT)
     fldParm.text = "30.12.1999"
     Set fldParm = fldLstParms.Append ("RM06E-EEIND(02)", LCTYPE_TEXT)
     fldParm.text = "30.12.1999"
     
     
    	REM Now using the fieldlist containing the field with the 
	REM stored procedure parameter, call the stored procedure
     count = destCon.Call (fldLstParms, 1, fldLstResult)
     Msgbox destCon.Message
     End
     
ErrorHandler:
     Dim Msg As String
     Dim MsgCode As Long
     Dim status As Integer
     Dim result As String
     
     If session.status <> LCSUCCESS Then
          status = session.GetStatus (result, msgcode, msg)
     Else
          result =  "Error " & Err() & ": " & Error()
     End If
     
     Messagebox (result), mb, wintitle
     
     End

Example 3

This example shows how to have SAP® as a target and get your EXPORT parameters back.
Dim dest As New LCConnection ("sap")
	Dim src As New lcconnection ("notes")
	Dim fldLst As New LCFieldList
	Dim fldLst2 As New LCFieldList
	Dim count, inserted As Integer
	Dim commandstatement As String
	
    ' Set the appropriate properties to connect to SAP
	dest.Database = "RFC_CUSTOMER_UPDATE"
	dest.Userid = "muster"
	dest.Password = "chorus"
	dest.Client="800"
	dest.SystemNo=0
	dest.Language="E"
	dest.Server = "lotcc09"
	dest.Destination="LD2"
	dest.DebugLevel = 0
	
   	'Set the properties to connect to Notes
	src.Server = ""
	src.Database= "leisap.nsf"
	
	dest.Connect
	src.Connect
	
	dest.Metadata = "*" ‘Used because we want to access not only 
                         	'tables but also the EXPORT 
                        	' parameters of RFC_CUSTOMER_UPDATE.
	src.Metadata = "Customer"
	commandstatement = "Select FORM=""Customer"" & IsMod=""1"""
	' now connected, we can execute a selection statement against Domino
	If (src.Execute(commandstatement,  fldLst) = 0) Then
		Print "No records were fetched."
		End
	End If
		
‘Our form has the same field names as in the result set from SAP
	dest.MapByName = True		
' Below we are using the Call routine because at this time it is 
' the only way to get the EXPORT parameters returned to use 
' when SAP is target. The limitation is that Call must be called 
' once for each row of data to be sent to SAP.
While (src.Fetch (fldLst) > 0) 'Get a document from our Notes result set
Dim fldLstResult As New LCFieldList (1, 0)
count = dest.Call (fldLst, 1, fldLstResult)  
	'Send this document to SAP and get the EXPORTS back
		Dim error_text, name1 As LCField
		Set name1 = fldLstResult.GetField(3)
		Set error_text =  fldLstResult.GetField(10)' Our result message
                                                ' from the call
		If dest.Fetch (fldLstResult) > 0 Then
			Msgbox name1.text(0) &" : " & error_text.text(0)
		End If
		Delete fldLstResult
	Wend

Example 4

This detailed example describes how to use the LCConnection Call method with SAP® as a target.
Sub Initialize
	
	Dim src As New LCConnection ("sap")
	Dim fldLstParms As New LCFieldList
	Dim fldLstResult As New LCFieldList
	
	Dim myval As Double 
	
	Dim count, inserted As Integer
	Dim commandstatement As String
	
	
	src.Database = "Z_DATATYPES"
	src.Userid = "muster"
	src.Password = "xxx"
	src.Client="800"
	src.Destination="LD2"
	src.SystemNo=0
	src.Language="E"
	src.Server = "lotcc09"
	
	src.Connect
	src.Metadata = "*"
	
	Dim flParm As lcfield
	Dim number As New lcnumeric
	
	number.text = "12345678901234567890"
	Set fldParm = fldLstParms.Append ("INTABLEBIGNUMC", LCTYPE_NUMERIC)
	Call fldParm.setnumeric(1,number)
	
	number.text = "123456789012345555555"
	Set fldParm = fldLstParms.Append ("IMPORTSINPUT_NUMC", LCTYPE_NUMERIC)
	Call fldParm.setnumeric(1,number)
	
	number.text = "1234567890123456666666"
	Set fldParm = fldLstParms.Append("IMPORTSINSTRUCTBIGNUMC", LCTYPE_NUMERIC)
	Call fldParm.setnumeric(1,number)
	
	number.text = "12345678901234567.8787"
	Set fldParm = fldLstParms.Append ("INTABLEBIGTYPP", LCTYPE_NUMERIC)
	Call fldParm.setnumeric(1,number)
	
	number.text = "12345678901234567.8787"
	Set fldParm = fldLstParms.Append ("IMPORTSINPUT_TYPP", LCTYPE_NUMERIC)
	Call fldParm.setnumeric(1,number)
	
	number.text = "12345678901234567.8787"
	Set fldParm = fldLstParms.Append("IMPORTSINSTRUCTBIGTYPP", LCTYPE_NUMERIC)
	Call fldParm.setnumeric(1,number)
	
	Dim money As New lccurrency
	money.value = 92233.58
	Set fldParm = fldLstParms.Append("IMPORTSINPUT_CURRENCY", LCTYPE_CURRENCY)
	Call fldParm.setcurrency(1,money)
	
	Dim myflt As Double
	myflt = 1234567.88656
	Set fldParm = fldLstParms.Append ("IMPORTSINPUT_FLOAT", LCTYPE_FLOAT)
	Call fldParm.setfloat(1,myflt)
	
	Dim mydate As New lcdatetime(1999,12,31)
	Set fldParm = fldLstParms.Append ("IMPORTSINPUT_DATE", LCTYPE_DATETIME)
	Call fldParm.setdatetime(1,mydate)
	src.MapByName = True
	
	count = src.Call (fldLstParms, 1, fldLstResult)
	
	While (src.Fetch (fldLstResult) > 0)
		Set field = fldlstResult.lookup("OUTTABLEBIGNUMC",index)
		Print "OUTTABLEBIGNUMC= " & field.text(0)
		Set field = fldlstResult.lookup("OUTTABLEBIGTYPP",index)
		Print "OUTTABLEBIGTYPP= " & field.text(0)
		Set field = fldlstResult.lookup("EXPORTSOUTPUT_DATE",index)
		Print "EXPORTSOUTPUT_DATE= " & field.text(0)
		Set field = fldlstResult.lookup("EXPORTSOUTPUT_FLOAT",index)
		Print "EXPORTSOUTPUT_FLOAT= " & field.text(0)
		Set field = fldlstResult.lookup("EXPORTSOUTPUT_CURRENCY",index)
		Print "EXPORTSOUTPUT_CURRENCY= " & field.text(0)
		Set field = fldlstResult.lookup("EXPORTSOUTPUT_NUMC",index)
		Print " EXPORTSOUTPUT_NUMC= " & field.text(0)
		Set field = fldlstResult.lookup("EXPORTSOUTPUT_TYPP",index)
		Print "EXPORTSOUTPUT_TYPP= " & field.text(0)
		Set field = fldlstResult.lookup("EXPORTSOUTSTRUCTBIGNUMC",index)
		Print "EXPORTSOUTSTRUCTBIGNUMC= " & field.text(0)
		Set field = fldlstResult.lookup("EXPORTSOUTSTRUCTBIGTYPP",index)
		Print "EXPORTSOUTSTRUCTBIGTYPP= " & field.text(0)
		
	Wend
End Sub

Example 5

This example uses ABAP for Z_DATATYPES.
FUNCTION Z_DATATYPES.
*"------------------------------------------------------------
*"*"Local interface:
*"       IMPORTING
*"             VALUE(INPUT_NUMC) LIKE  ZTABLE-BIGNUMC
*"             VALUE(INPUT_TYPP) LIKE  ZTABLE-BIGTYPP
*"             VALUE(INSTRUCT) LIKE  ZTESTSTRUCT
*"                               STRUCTURE  ZTESTSTRUCT
*"             VALUE(INPUT_FLOAT) LIKE  ZFLOAT-TEST_FLOAT
*"             VALUE(INPUT_CURRENCY) LIKE  KNA1-UMSA1
*"             VALUE(INPUT_DATE) LIKE  KNA1-ERDAT
*"       EXPORTING
*"             VALUE(OUTPUT_NUMC) LIKE  ZTABLE-BIGNUMC
*"             VALUE(OUTPUT_TYPP) LIKE  ZTABLE-BIGTYPP
*"             VALUE(OUTSTRUCT) LIKE  ZTESTSTRUCT
*"                             STRUCTURE  ZTESTSTRUCT
*"             VALUE(OUTPUT_FLOAT) LIKE  ZFLOAT-TEST_FLOAT
*"             VALUE(OUTPUT_CURRENCY) LIKE  KNA1-UMSA1
*"             VALUE(OUTPUT_DATE) LIKE  KNA1-ERDAT
*"       TABLES
*"              INTABLE STRUCTURE  ZTABLE
*"              OUTTABLE STRUCTURE  ZTABLE
*"------------------------------------------------------------
CLEAR OUTTABLE.
REFRESH OUTTABLE.
LOOP AT INTABLE.
OUTTABLE-BIGNUMC = INTABLE-BIGNUMC.
OUTTABLE-BIGTYPP = INTABLE-BIGTYPP.
APPEND OUTTABLE.
EXIT.
ENDLOOP.

OUTPUT_FLOAT = INPUT_FLOAT.
OUTPUT_CURRENCY = INPUT_CURRENCY.
OUTPUT_DATE = INPUT_DATE.
OUTPUT_NUMC = INPUT_NUMC.
OUTPUT_TYPP = INPUT_TYPP.
OUTSTRUCT-BIGNUMC = INSTRUCT-BIGNUMC.
OUTSTRUCT-BIGTYPP = INSTRUCT-BIGTYPP.

ENDFUNCTION.

Example 6

This example creates a generic function that can be used to connect. What is special about this routine is that it uses the session pooling functionality available with the LC LSX.
Function AssertConnection (doc As NotesDocument, FunctionName As String, SAPGUIFlag As Integer) As Integer     
	Set sess = New LCSession
	sess.connectionpooling = True

	Dim ns As New NotesSession


	If doc.Password(0) = "" Or doc.SAPUser(0) = "" Or doc.Client(0) = "" Or doc.Server(0) = "" Or Str$(doc.SystemNo(0)) = "" Then
		AssertConnection = True
		Exit Function
	End If

   	' Create server object and fill the necessary  properties for logon.
	Set r3src = New LCConnection("sap")
	r3src.Destination = doc.SystemName(0) 
	r3src.Server= doc.Server(0)
	r3src.Database = FunctionName
	r3src.SystemNo = doc.SystemNo(0)
	r3src.Client = doc.Client(0)
	r3src.UserID = doc.SAPUser(0)
	r3src.Language = doc.Language(0)
	r3src.Password = doc.Password(0)
	Cleanup = doc.Cleanup(0)
	Update = doc.Update(0)
	FromName = doc.SystemName(0)
	r3src.debuglevel=0


	r3src.EnableSAPGUI = SAPGUIFlag

   	'logon    
	r3src.connect

	AssertConnection = False

End Function