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

Tip: How to use ABAP with SAP GUI Scripting

A few days ago I presented here in the SAP Community the possibility to use ABAP with SAP Intelligent RPA (Robotic Process Automation). The approach is a COM library called ABAPRunner. Just as ABAP can be used there, ABAP can also be used in combination with SAP GUI Scripting. Here a tiny example.

  1. We call the TAC BC_GLOABL_SFLGH_CREA to create a record with the key fields.
  2. After pressing the create button we fill the non key fields and press save.

The Flight Date and the Airfare are random values. We check with the message in the status bar whether everything is okay. So far so well, that is an easy venture. But now follows the really interesting part.

We want to check whether the data has actually been saved in the table and if so, we want to delete it immediately. These steps can be done by a tiny ABAP report.

DATA:
  gt_sflight TYPE STANDARD TABLE OF sflight.
BREAK-POINT.
SELECT *
  INTO TABLE gt_sflight
  FROM SFLIGHT
  WHERE CARRID = 'LH' AND
        CONNID = '0400' AND
        PLANETYPE = '737-800' AND
        FLDATE = '20200923' AND
        PRICE = '685.14'.
IF sy-subrc <> 0.
  WRITE: 'Error: Record do not exists.'.
ELSE.
  DELETE SFLIGHT FROM TABLE gt_sflight.
ENDIF.

The flight date and the price must be variable.

That is all, now we can combine our SAP GUI Scripting code with the ABAP code.

'-Begin-----------------------------------------------------------------

  session.findById("wnd[0]/tbar[0]/okcd").text = "/nBC_GLOBAL_SFLGH_CREA"
  session.findById("wnd[0]/tbar[0]/btn[0]").press

  Randomize
  FlightDate = FormatDateTime(DateAdd("d", Rnd * 365, Now), vbShortDate)
  ABAPFlightDate = Right(FlightDate, 4) & Mid(FlightDate, 4, 2) & Left(FlightDate, 2)

  session.findById("wnd[0]/usr/ctxtSFLIGHT-CARRID").text = "LH"
  session.findById("wnd[0]/usr/ctxtSFLIGHT-CONNID").text = "400"
  session.findById("wnd[0]/usr/ctxtSFLIGHT-FLDATE").text = FlightDate
  session.findById("wnd[0]/usr/btn%#AUTOTEXT001").press

  StatusBarText = session.findById("wnd[0]/sbar/pane[0]").Text
  If InStr(StatusBarText, "already exists") Then
    MsgBox "An error occured"
    Exit Sub
  End If

  Randomize
  FlightPrice = CStr(FormatNumber(Round(Rnd * 1000, 2), 2))
  ABAPFlightPrice = Replace(FlightPrice, ",", ".")

  session.findById("wnd[0]/usr/txtSFLIGHT-PRICE").text = FlightPrice
  session.findById("wnd[0]/usr/ctxtSFLIGHT-PLANETYPE").text = "737-800"
  session.findById("wnd[0]/tbar[0]/btn[11]").press

  StatusBarText = session.findById("wnd[0]/sbar/pane[0]").Text
  If InStr(StatusBarText, "created") = 0 Then
    MsgBox "An error occured"
    Exit Sub
  End If

  On Error Resume Next
  Set ABAPRunner = CreateObject("ABAP.Runner")
  On Error Goto 0
  If Not IsObject(ABAPRunner) Then
    MsgBox "Can't create ABAP.Runner", vbOkOnly, "Important hint"
    Exit Sub
  End If

'-ABAP------------------------------------------------------------------
ABAPCode = _
"REPORT Z_CHECK_SFLIGHT."                                               & vbCrLf & _
"DATA:"                                                                 & vbCrLf & _
"  gt_sflight TYPE STANDARD TABLE OF sflight."                          & vbCrLf & _
"SELECT *"                                                              & vbCrLf & _
"  INTO TABLE gt_sflight"                                               & vbCrLf & _
"  FROM SFLIGHT"                                                        & vbCrLf & _
"  WHERE CARRID = 'LH' AND"                                             & vbCrLf & _
"        CONNID = '0400' AND"                                           & vbCrLf & _
"        PLANETYPE = '737-800' AND"                                     & vbCrLf & _
"        FLDATE = '" & ABAPFlightDate & "' AND"                         & vbCrLf & _
"        PRICE = '" & ABAPFlightPrice & "'."                            & vbCrLf & _
"IF sy-subrc <> 0."                                                     & vbCrLf & _
"  WRITE: 'Error: Record do not exists'."                               & vbCrLf & _
"ELSE."                                                                 & vbCrLf & _
"  DELETE SFLIGHT FROM TABLE gt_sflight."                               & vbCrLf & _
"ENDIF."
'-ABAPEND---------------------------------------------------------------
  ABAPRunner.ABAPCode = ABAPCode

  ABAPRunner.AddParameter = "ASHOST=ABAP"
  ABAPRunner.AddParameter = "SYSNR=00"
  ABAPRunner.AddParameter = "CLIENT=001"
  ABAPRunner.AddParameter = "USER=BCUSER"
  ABAPRunner.AddParameter = "PASSWD=minisap"
  ABAPRunner.AddParameter = "LANG=EN"

  Result = ABAPRunner.InstallAndRun()
  If Result <> "" Then
    MsgBox Result
  End If

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

We call the TAC, generate a random flight date, fill the fields, check the status bar, generate a random flight price, fill more fields and check the status bar again. Then the initialization of ABAPRunner follows. And now the ABAP code, but the flight date and price are variables. Before the execution the connection parameters. And it works.

If the record do not exists in the table SFLIGHT the report delivers an error message, otherwise nothing happens and it can be assumed that the execution was a success.

As we can see it is easy possible to combine SAP GUI Scripting with ABAP coding. This example shows us how to check data with SAP Open SQL statements of a test process. This data can also be removed at the end, as we saw. It is not necessary to embed the ABAP code in the VBScript, you can store it as file also and load it via File System Object, e.g. like this:

Set FSO = CreateObject("Scripting.FileSystemObject")
Set ABAPFile = FSO.OpenTextFile("ABAPRunner.Test.abap", ForReading)
ABAPRunner.ABAPCode = ABAPFile.ReadAll
ABAPFile.Close

However, variables must then be passed as parameters.

There are many possible uses for this approach. But never use this method in productive environments. It is for development and test systems only. And the COM library is not language-bound. You can also use AutoIt, PowerShell or Python respectively any other COM enabled language.

You can find ABAPRunner here. Enjoy it.

Assigned Tags

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