Skip to Content

This document will explain how to automate the execution of steps in SAP using the Script Recording and Playback.

You should be able to make your own script by following this document. It is not necessary to have programming knowledge.

You can use Script to automate business transactions, to automate master data creation, to automate configuration, etc..

Some comments about SAP Script Recording and Playback:

  • This is an interface exposed by SAP GUI.
  • It is a standard SAP tool and could available or disable.
  • Can make life easier for users by automating repeating tasks.

1- Generate the Script (*.vbs)

The Script file can be generated automatically by recording the steps executed in SAP.

The following screenshots show how to create the main script file in a few minutes or even second:

SCRIP01.jpg

SCRIP02.jpg

It is important to select the file extension .vbs:

SCRIP03.jpg

Once you press Start Recording, you will see this indicator at the bottom of the screen:

SCRIP04.jpg

Execute the transaction that you want to save in the script. Depending on what you are trying to automate, you will need to enter the SAP tcode with /n. If you do this you will leave and you will access the t-code again in each iteration.

SCRIP05.jpg

Enter all the fields that you are going to automate:

SCRIP07.jpg

SCRIP08.jpg

Once one iteration is completed, press Stop Recording. This will complete the generation of the Script and the file will be saved in the selected Path:

SCRIP09.jpg

2- Enhance the Script (*.vbs) to allow the automation via Excel

Find and open with the Notepad the Script file generated in the previous step.

SCRIP10.jpg

To allow the automation, it is necessary to do two things:

  • Add two blocks of programming code in the vbs: There is no need to understand how to program a script. The code is always the same. The only thing that could change is the number of columns used in the excel. In this example we are going to use 5 columns.

     SCRIP11.jpg

This is the code of each block of code. If you need to add columns in the excel, you will need to add the sentence in red color:

Block of code: Block A

REM ADDED BY EXCEL *************************************

Dim objExcel
Dim objSheet, intRow, i
Set objExcel = GetObject(,”Excel.Application”)
Set objSheet = objExcel.ActiveWorkbook.ActiveSheet

For i = 2 to objSheet.UsedRange.Rows.Count
COL1 = Trim(CStr(objSheet.Cells(i, 1).Value)) ‘Column1
COL2 = Trim(CStr(objSheet.Cells(i, 2).Value)) ‘Column2
COL3 = Trim(CStr(objSheet.Cells(i, 3).Value)) ‘Column3
COL4 = Trim(CStr(objSheet.Cells(i, 4).Value)) ‘Column4
COL5 = Trim(CStr(objSheet.Cells(i, 5).Value)) ‘Column5

COL6 = Trim(CStr(objSheet.Cells(i, 6).Value)) ‘Column6

REM ADDED BY EXCEL *************************************

Block of code: Block B

REM FINALIZATION CONTROL CHECK ************************

aux=col1 & ” ” & col2 & ” ” & col3  & ” ” & col4 & ” ” & col5  & ” ” & col6
CreateObject(“WScript.Shell”).run(“cmd /c @echo %date% %time% ” & aux & ” >> C:\SCRIPT\PlOrCreationLog.txt”)
next
msgbox “Process Completed”

REM FINALIZATION CONTROL CHECK ************************

  • Replace the “hardcoaded code” by the Columns#:

     SCRIP12.jpg

This is the final code of the Script (in red color the text added):

If Not IsObject(application) Then
   Set SapGuiAuto  = GetObject(“SAPGUI”)
   Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
   Set connection = application.Children(0)
End If
If Not IsObject(session) Then
   Set session    = connection.Children(0)
End If
If IsObject(WScript) Then
   WScript.ConnectObject session,     “on”
   WScript.ConnectObject application, “on”
End If
session.findById(“wnd[0]”).maximize

REM ADDED BY EXCEL *************************************

Dim objExcel
Dim objSheet, intRow, i
Set objExcel = GetObject(,”Excel.Application”)
Set objSheet = objExcel.ActiveWorkbook.ActiveSheet

For i = 2 to objSheet.UsedRange.Rows.Count
COL1 = Trim(CStr(objSheet.Cells(i, 1).Value)) ‘Column1
COL2 = Trim(CStr(objSheet.Cells(i, 2).Value)) ‘Column2
COL3 = Trim(CStr(objSheet.Cells(i, 3).Value)) ‘Column3
COL4 = Trim(CStr(objSheet.Cells(i, 4).Value)) ‘Column4
COL5 = Trim(CStr(objSheet.Cells(i, 5).Value)) ‘Column5

REM ADDED BY EXCEL *************************************

session.findById(“wnd[0]/tbar[0]/okcd”).text = “/nmd11”
session.findById(“wnd[0]”).sendVKey 0
session.findById(“wnd[0]/usr/ctxtRM61P-PASCH”).text = COL1
session.findById(“wnd[0]/usr/ctxtRM61P-PASCH”).caretPosition = 2
session.findById(“wnd[0]”).sendVKey 0
session.findById(“wnd[0]/usr/ctxtPLAF-MATNR”).text = COL2
session.findById(“wnd[0]/usr/ctxtPLAF-PLWRK”).text = COL3
session.findById(“wnd

[0]/usr/tabsTABTC/tabpTAB01/ssubINCLUDE1XX:SAPLM61O:0711/subINCLUDE711_1:SAPLM61O:0802/txtPLAF

-GSMNG”).text = COL4
session.findById(“wnd

[0]/usr/tabsTABTC/tabpTAB01/ssubINCLUDE1XX:SAPLM61O:0711/subINCLUDE711_1:SAPLM61O:0802/ctxtPLA

F-PSTTR”).text = COL5
session.findById(“wnd

[0]/usr/tabsTABTC/tabpTAB01/ssubINCLUDE1XX:SAPLM61O:0711/subINCLUDE711_1:SAPLM61O:0802/ctxtPLA

F-PSTTR”).setFocus
session.findById(“wnd

[0]/usr/tabsTABTC/tabpTAB01/ssubINCLUDE1XX:SAPLM61O:0711/subINCLUDE711_1:SAPLM61O:0802/ctxtPLA

F-PSTTR”).caretPosition = 10
session.findById(“wnd[0]”).sendVKey 0
session.findById(“wnd[0]/tbar[0]/btn[11]”).press

REM FINALIZATION CONTROL CHECK ************************

aux=col1 & ” ” & col2 & ” ” & col3  & ” ” & col4 & ” ” & col5
CreateObject(“WScript.Shell”).run(“cmd /c @echo %date% %time% ” & aux & ” >> C:\SCRIPT

\PlOrCreationLog.txt”)
next
msgbox “Process Completed”

REM FINALIZATION CONTROL CHECK ************************

3- Maintain the Excel file

Enter all the data to be used in the automation. The script will always start reading from the second line. Use the first line for the header:

Important: Only one excel file should be opened to execute the Script

SCRIP13.jpg

4- Script Execution

Remember the previous message: Only one excel file should be opened to execute the Script

Keep your Excel file opened. Open the Script Recording and Playback toolkit, Select the path and the file and press Play…

You will see how it works!!

SCRIP14.jpg

5- Enableling SapScript Recording and Playback

SAP Script Recording and Playback is disable by Default.

It can be enable using a profile parameter. Note 480149 describes the requirement to enable this functionality:

480149
– New profile parameter for user scripting at the front end

The sapgui/user_scripting parameter is imported again. If this parameter is
set to TRUE, the scripting functions can be used with a GUI as of
version 6.20 on the front end.

The default value of the parameter
is FALSE so that scripting to the system is not possible.

Note
that you must enter the values in upper case;

There are 2 options for
setting the parameter: In transaction rz11 and in the server profile. If the
parameter is only set in rz11, the change is lost when you restart the
server.

Setting the parameter in the SAP
system
===============================================

If possible,
dynamic setting of the parameter is executed using transaction rz11. Specify the
parameter name sapgui/user_scripting and select ‘Display’.
Provided that the
current value is set to FALSE, select the ‘Change value’ button in
the toolbar. A window now appears, in which you can enter the new value
TRUE. When you save the change, the window closes and the current value
of the parameter changes to TRUE. This change only becomes effective when
you log onto the system again.

If the parameter is not found, you must
import the relevant Support Package in accordance with the list below.

If
the current value does not change accordingly after you have saved the change,
it means that the kernel is too old. In this case, import the required kernel
patch, as specified below.

Setting the parameter in the server
profile
======================================

If you have not
imported the Support Package, you can switch the scripting on if you set the
parameter in the profile file of the application server with the following
line:
sapgui/user_scripting = TRUE
This procedure only requires the
specified kernel patch level, however, you must restart the application
server.

5- More functionalities of Sap Script Recording and Playback


There are much more functionalities that can be used. For example, you can collect the SAP messages or add conditional logic. You can read about it in the following article:

Script Recording & Playback for Dummies (intelligence)

To report this post you need to login first.

110 Comments

You must be Logged on to comment or reply to a post.

  1. Amaury Van Espen

    Dear Mariano Cabalen,

    by using Script Recording and Playback, could you tell me if the field name in script (from views) lets us identify the name of field stored in SAP Tables ?

    Otherwise, do you know a simple way to get it ?

    Best

    Amaury

    (0) 
  2. Sumanth Kristam

    This is pretty cool. Encountered when searching for a some other requirement.

     

    do you know how long has this been available in SAP and from which version it is available?

    (0) 
  3. Debra Miller

    Hello, these are excellent instructions but I got the dreaded “invalid character” and it turns out when you copy and paste from either Word or a web page some of the apostrophes, etc change.

    I had to update the  REM ADDED BY EXCEL ************************************* to add ‘ at the beginning and also had to update and instances of ” as in

    Set objExcel = GetObject(,”Excel.Application”) to the proper character of ”

    http://ascii.cl/

    The differences are hard to see.  Luckily, when I right click on the notepad document there is an Open with option of Microsoft Windows Based Script Host and when I selected it I was provided the line and character location of the bad data.

    (1) 
  4. Eric Shelton

    Hey everyone.  Have we found a solution to the error:

    “ActiveX component can’t create object: ‘GetObject’ –

     

    It’s disappointing as I have a very small script to run but can’t launch it due to this error.  I even changed the quotation marks/apostrophes as most recently recognized by Debra Miller.  The script is as follows:

     

     

    Dim objExcel
    Dim objSheet, intRow, i
    Set objExcel = GetObject(,”Excel.Application”)
    Set objSheet = objExcel.ActiveWorkbook.ActiveSheet

    For i = 2 to objSheet.UsedRange.Rows.Count
    COL1 = Trim(CStr(objSheet.Cells(i, 1).Value)) ‘Column1
    COL2 = Trim(CStr(objSheet.Cells(i, 2).Value)) ‘Column2
    COL3 = Trim(CStr(objSheet.Cells(i, 3).Value)) ‘Column3
    COL4 = Trim(CStr(objSheet.Cells(i, 4).Value)) ‘Column4
    COL5 = Trim(CStr(objSheet.Cells(i, 5).Value)) ‘Column5

    COL6 = Trim(CStr(objSheet.Cells(i, 6).Value)) ‘Column6

    REM ADDED BY EXCEL *************************************

     

     

    session.findById(“wnd[0]”).maximize
    session.findById(“wnd[0]/tbar[0]/okcd”).text = “zsdpas”
    session.findById(“wnd[0]”).sendVKey 0
    session.findById(“wnd[0]/usr/ctxt[1]”).text = “COL1”
    session.findById(“wnd[0]/usr/ctxt[1]”).setFocus
    session.findById(“wnd[0]/usr/ctxt[1]”).caretPosition = 7
    session.findById(“wnd[0]/tbar[1]/btn[8]”).press
    session.findById(“wnd[0]/tbar[1]/btn[31]”).press
    session.findById(“wnd[0]/usr/sub/1/sub/1/2/tabsTAB_CONTROL/tabpREC/ssub/1/2/1/2/tblSAPLSO04REC_CONTROL/ctxt[0,0]”).text = “Xxxxx.xxxx@xxxxxx.com”
    session.findById(“wnd[0]/usr/sub/1/sub/1/2/tabsTAB_CONTROL/tabpREC/ssub/1/2/1/2/tblSAPLSO04REC_CONTROL/ctxt[0,0]”).setFocus
    session.findById(“wnd[0]/usr/sub/1/sub/1/2/tabsTAB_CONTROL/tabpREC/ssub/1/2/1/2/tblSAPLSO04REC_CONTROL/ctxt[0,0]”).caretPosition = 23
    session.findById(“wnd[0]/tbar[1]/btn[20]”).press
    session.findById(“wnd[0]/tbar[0]/btn[3]”).press
    session.findById(“wnd[0]/tbar[0]/btn[3]”).press

    REM FINALIZATION CONTROL CHECK ************************

    aux=col1 & ” ” & col2 & ” ” & col3  & ” ” & col4 & ” ” & col5  & ” ” & col6
    CreateObject(“WScript.Shell”).run(“cmd /c @echo %date% %time% ” & aux & ” >> C:\SCRIPT\PlOrCreationLog.txt”)
    next
    msgbox “Process Completed”

    REM FINALIZATION CONTROL CHECK ************************

     

    Granting this capability would immensely assist in my every day work!  Thank you all in advance for your support and time!

    (0) 
    1. Jacob Walsweer

      Hi Eric,

      Probably a bit late, but I had the same issue today. It was because I did not have the Excel file open. Once I opened the Excel, it worked fine.

      KR

      Jaap

       

      (0) 
  5. Rohit Mansukh Gugale

    Hello Friends,

     

    I am getting the below error while executing my script ” The control could not be found by id”

     

    i have pasted the script below. Can you please help.

    If Not IsObject(application) Then
    Set SapGuiAuto = GetObject(“SAPGUI”)
    Set application = SapGuiAuto.GetScriptingEngine
    End If
    If Not IsObject(connection) Then
    Set connection = application.Children(0)
    End If
    If Not IsObject(session) Then
    Set session = connection.Children(0)
    End If
    If IsObject(WScript) Then
    WScript.ConnectObject session, “on”
    WScript.ConnectObject application, “on”
    End If
    session.findById(“wnd[0]”).maximize
    Dim objExcel
    Dim objSheet, intRow, i
    Set objExcel = GetObject(, “Excel.Application”)
    Set objSheet = objExcel.ActiveWorkbook.ActiveSheet
    For i = 2 to objSheet.UsedRange.Rows.Count
    COL1 = Trim(CStr(objSheet.Cells(i, 1).Value)) ‘column1
    COL2 = Trim(CStr(objSheet.Cells(i, 2).Value)) ‘column2
    COL3 = Trim(CStr(objSheet.Cells(i, 3).Value)) ‘column3
    COL4 = Trim(CStr(objSheet.Cells(i, 4).Value)) ‘column4
    COL5 = Trim(CStr(objSheet.Cells(i, 5).Value)) ‘column5
    session.findById(“wnd[0]/tbar[0]/okcd”).text = “/nfpp2”
    session.findById(“wnd[0]”).sendVKey 0
    session.findById(“wnd[1]/usr/ctxtBUS_JOEL_MAIN-OPEN_NUMBER”).text = COL1
    session.findById(“wnd[1]/usr/ctxtBUS_JOEL_MAIN-OPEN_NUMBER”).caretPosition = 10
    session.findById(“wnd[1]/tbar[0]/btn[0]”).press
    session.findById(“wnd[0]/usr/subSCREEN_3000_RESIZING_AREA:SAPLBUS_LOCATOR:2036/subSCREEN_1010_RIGHT_AREA:SAPLBUPA_DIALOG_JOEL:1000/ssubSCREEN_1000_WORKAREA_AREA:SAPLBUPA_DIALOG_JOEL:1100/ssubSCREEN_1100_MAIN_AREA:SAPLBUPA_DIALOG_JOEL:1101/tabsGS_SCREEN_1100_TABSTRIP/tabpSCREEN_1100_TAB_01/ssubSCREEN_1100_TABSTRIP_AREA:SAPLBUSS:0028/ssubGENSUB:SAPLBUSS:7016/subA05P01:SAPLBUA0:0400/subADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0304/ctxtADDR1_DATA-STREET”).text = “PO BOX 2440”
    session.findById(“wnd[0]/usr/subSCREEN_3000_RESIZING_AREA:SAPLBUS_LOCATOR:2036/subSCREEN_1010_RIGHT_AREA:SAPLBUPA_DIALOG_JOEL:1000/ssubSCREEN_1000_WORKAREA_AREA:SAPLBUPA_DIALOG_JOEL:1100/ssubSCREEN_1100_MAIN_AREA:SAPLBUPA_DIALOG_JOEL:1101/tabsGS_SCREEN_1100_TABSTRIP/tabpSCREEN_1100_TAB_01/ssubSCREEN_1100_TABSTRIP_AREA:SAPLBUSS:0028/ssubGENSUB:SAPLBUSS:7016/subA05P01:SAPLBUA0:0400/subADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0304/ctxtADDR1_DATA-CITY1”).text = “SPOKANE”
    session.findById(“wnd[0]/usr/subSCREEN_3000_RESIZING_AREA:SAPLBUS_LOCATOR:2036/subSCREEN_1010_RIGHT_AREA:SAPLBUPA_DIALOG_JOEL:1000/ssubSCREEN_1000_WORKAREA_AREA:SAPLBUPA_DIALOG_JOEL:1100/ssubSCREEN_1100_MAIN_AREA:SAPLBUPA_DIALOG_JOEL:1101/tabsGS_SCREEN_1100_TABSTRIP/tabpSCREEN_1100_TAB_01/ssubSCREEN_1100_TABSTRIP_AREA:SAPLBUSS:0028/ssubGENSUB:SAPLBUSS:7016/subA05P01:SAPLBUA0:0400/subADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0304/ctxtADDR1_DATA-REGION”).text = “WA”
    session.findById(“wnd[0]/usr/subSCREEN_3000_RESIZING_AREA:SAPLBUS_LOCATOR:2036/subSCREEN_1010_RIGHT_AREA:SAPLBUPA_DIALOG_JOEL:1000/ssubSCREEN_1000_WORKAREA_AREA:SAPLBUPA_DIALOG_JOEL:1100/ssubSCREEN_1100_MAIN_AREA:SAPLBUPA_DIALOG_JOEL:1101/tabsGS_SCREEN_1100_TABSTRIP/tabpSCREEN_1100_TAB_01/ssubSCREEN_1100_TABSTRIP_AREA:SAPLBUSS:0028/ssubGENSUB:SAPLBUSS:7016/subA05P01:SAPLBUA0:0400/subADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0304/txtADDR1_DATA-POST_CODE1”).text = “99210-2440”
    session.findById(“wnd[0]/usr/subSCREEN_3000_RESIZING_AREA:SAPLBUS_LOCATOR:2036/subSCREEN_1010_RIGHT_AREA:SAPLBUPA_DIALOG_JOEL:1000/ssubSCREEN_1000_WORKAREA_AREA:SAPLBUPA_DIALOG_JOEL:1100/ssubSCREEN_1100_MAIN_AREA:SAPLBUPA_DIALOG_JOEL:1101/tabsGS_SCREEN_1100_TABSTRIP/tabpSCREEN_1100_TAB_01/ssubSCREEN_1100_TABSTRIP_AREA:SAPLBUSS:0028/ssubGENSUB:SAPLBUSS:7016/subA05P01:SAPLBUA0:0400/subADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0304/txtADDR1_DATA-POST_CODE1”).setFocus
    session.findById(“wnd[0]/usr/subSCREEN_3000_RESIZING_AREA:SAPLBUS_LOCATOR:2036/subSCREEN_1010_RIGHT_AREA:SAPLBUPA_DIALOG_JOEL:1000/ssubSCREEN_1000_WORKAREA_AREA:SAPLBUPA_DIALOG_JOEL:1100/ssubSCREEN_1100_MAIN_AREA:SAPLBUPA_DIALOG_JOEL:1101/tabsGS_SCREEN_1100_TABSTRIP/tabpSCREEN_1100_TAB_01/ssubSCREEN_1100_TABSTRIP_AREA:SAPLBUSS:0028/ssubGENSUB:SAPLBUSS:7016/subA05P01:SAPLBUA0:0400/subADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0304/txtADDR1_DATA-POST_CODE1”).caretPosition = 2
    session.findById(“wnd[0]/tbar[0]/btn[11]”).press
    session.findById(“wnd[1]/tbar[0]/btn[0]”).press
    aux=col1 & ” ” & col2 & ” ” & col3 & ” ” & col4 & ” ” & col5
    CreateObject(“WScript.Shell”).run(“cmd /c @echo %date% %time% ” & aux & ” >> C:\SCRIPT\PlOrCreationLog.txt”)
    next
    msgbox “Process Completed”

     

    (0) 
  6. Nick Horkov

    Hi All, could you help me. Ive created script according the manual. Very thank you for sharing. But i have a problem:

    I have script .vbs recorded on one notebook[A]. The part of code:

    session.findById(“wnd[0]/usr/ctxtLIKP-LIFNR”).text = “103005”

    On the other notebook[B] the same part looks like:

    session.findById(“wnd[0]/usr/ctxt[0]”).text = “103005”

    When i try to run scrip from the first Notebook[A] on the other [B], the second one gives message: “The control could not be found by id”

    The same error i have got when run script from notebook[B] on notebook [A].

    Also we have many others notebooks in company. They record scripts in the same way as the notebook [B] (session.findById(“wnd[0]/usr/ctxt[0]”).text)

    But we have huge script recorded on the notebook [A] and we need to run it on others notebooks.

    Is there any ways to make other notebooks read the script (session.findById(“wnd[0]/usr/ctxtLIKP-LIFNR”).text = “103005”) correctly?

    Thank you in advance.

    (0) 

Leave a Reply