Skip to Content
Technical Articles
Author's profile photo Stefan Schnell

How To Use bgRFC Calls With VBScript Via CCo

I introduced COM Connector (CCo) here. Here now an example how to use background RFC calls with VBScript via CCo. The queued RFC calls are commented. You can find more information about the different RFC variants here. I presented the possibility to use transactional (tRFC) and queued (qRFC) RFC calls here.

 

'-Begin-----------------------------------------------------------------
'-
'- Look also in TAC SBGRFCCONF and SBGRFCMON
'- Hint: You need SAP 7.0 EHP 1 for SAP NetWeaver 7.0 Support Package 4
'-       and higher
'-
'-----------------------------------------------------------------------

  '-Directives----------------------------------------------------------
    Option Explicit

  '-Constants-----------------------------------------------------------
    Const RFC_OK = 0

  '-Sub Main------------------------------------------------------------
    Sub Main()

      '-Variables-------------------------------------------------------
        Dim SAP, CCo, hRFC, rc, UID, hUnit, hFuncDesc, hFunc
        Dim unitAttrID, unitAttr, unitIdentID, unitIdent

      Set SAP = CreateObject("COMNWRFC")
      If Not IsObject(SAP) Then
        Exit Sub
      End If

      Set CCo = CreateObject("COMNWRFCHELP")
      If Not IsObject(CCo) Then
        Exit Sub
      End If

      hRFC = SAP.RfcOpenConnection("ASHOST=ABAP, SYSNR=00, " & _
        "CLIENT=001, USER=BCUSER")
      If hRFC = 0 Then
        Set SAP = Nothing
        Set CCo = Nothing
        Exit Sub
      End If

      hFuncDesc = SAP.RfcGetFunctionDesc(hRFC, "RFC_PING")
      If hFuncDesc = 0 Then
        rc = SAP.RfcCloseConnection(hRFC)
        Set SAP = Nothing
        Set CCo = Nothing
        Exit Sub
      End If

      hFunc = SAP.RfcCreateFunction(hFuncDesc)
      If hFunc = 0 Then
        rc = SAP.RfcCloseConnection(hRFC)
        Set SAP = Nothing
        Set CCo = Nothing
        Exit Sub
      End If

      If SAP.RfcGetUnitID(hRFC, UID) <> RFC_OK Then
        rc = SAP.RfcCloseConnection(hRFC)
        Set SAP = Nothing
        Set CCo = Nothing
        Exit Sub
      End If

      '-Create structures RFC_UNIT_ATTRIBUTES and RFC_UNIT_IDENTIFIER---
        If CCo.AllocateMemory(273, unitAttrID, unitAttr) = vbFalse Or _
          CCo.AllocateMemory(70, unitIdentID, unitIdent) = vbFalse Then
          rc = SAP.RfcCloseConnection(hRFC)
          Set SAP = Nothing
          Set CCo = Nothing
          Exit Sub
        End If

      '-qRFC------------------------------------------------------------
'       hUnit = SAP.RfcCreateUnit(hRFC, UID, "STEFANSQUEUE", unitAttr, _
'         unitIdent)

      '-tRFC------------------------------------------------------------
        hUnit = SAP.RfcCreateUnit(hRFC, UID, "", unitAttr, unitIdent)
        If hUnit = 0 Then
          rc = SAP.RfcCloseConnection(hRFC)
          Set SAP = Nothing
          rc = CCo.FreeMemory(unitIdentID)
          rc = CCo.FreeMemory(unitAttrID)
          Set CCo = Nothing
          Exit Sub
        End If

      If SAP.RfcInvokeInUnit(hUnit, hFunc) = RFC_OK Then

        If SAP.RfcSubmitUnit(hUnit) = RFC_OK Then

          MsgBox "Look in table BGRFC_SRV_STATE for UID " & UID

          SAP.ErrorMsgTarget = 2
          If SAP.RfcConfirmUnit(hRFC, unitIdent) = RFC_OK Then
            MsgBox "Look in table BGRFC_SRV_CFM for UID " & UID
          Else
            MsgBox SAP.ErrorMessage
          End If

        End If

      End If

      rc = SAP.RfcDestroyUnit(hUnit)
      rc = SAP.RfcDestroyFunction(hFunc)
      rc = SAP.RfcCloseConnection(hRFC)
      Set SAP = Nothing
      rc = CCo.FreeMemory(unitIdentID)
      rc = CCo.FreeMemory(unitAttrID)
      Set CCo = Nothing

    End Sub

  '-Main----------------------------------------------------------------
    Main()

'-End-------------------------------------------------------------------

Background RFCs are the recommended kind of RFCs.You should never use a synchronous RFC to write data into an SAP system, only to read data. To write data use tRFC or qRFC, or better the bgRFC. Now you have the easy possibility to use any kind of RFC with your favorite scripting language.

Enjoy it.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.