Skip to Content
Author's profile photo Mariano Cabalen

Script Recording & Playback for Dummies (intelligence)

In the previous document Script Recording & Playback for Dummies we learnt how to create a simple script in order to execute steps automatically in SAP.

Now, let´s introduce two new concepts to make this tool more powerful.

1- Collect ion of SAP Message

Let´s suppose that you create a script to create documents in SAP. In this case you probably need to get the document number generated by the system when Save button is pressed.

In this case, we need to add the following sentence:

     objExcel.Cells(i, 6).Value = session.findById(“wnd[0]/sbar”).Text

     where 6 is the number of the column where the message will be collected.

For example, suppose that we create a script to create planner orders, where the information that we introduce is:

Column1 = Order Type = LA

Column2 = Material

Column3 = MRP Area or Plant

Column4 = Quantity

Column5 = End Basic Date

The script will be the following:

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 CONEXAO COM O EXCEL ***********************

Dim objExcel

Dim objSheet, intRow, i

Set objExcel = GetObject(,”Excel.Application”)

Set objSheet = objExcel.ActiveWorkbook.ActiveSheet

REM OJO SIEMPRE LOS TITULOS DEL EXCEL TIENEN QUE ESTAR EN EL RENGLON 2 Y SOLO TENES QUE TENER UN SOLO EXCEL ACTIVO AL MOMENTO DE LANZAR EL JOB.

For i = 2 to objSheet.UsedRange.Rows.Count

cOL1 = Trim(CStr(objSheet.Cells(i, 1).Value)) ‘Ordertype

cOL2 = Trim(CStr(objSheet.Cells(i, 2).Value)) ‘Material

cOL3 = Trim(CStr(objSheet.Cells(i, 3).Value)) ‘MRP Area

cOL4 = Trim(CStr(objSheet.Cells(i, 4).Value)) ‘Quntity

cOL5 = Trim(CStr(objSheet.Cells(i, 5).Value)) ‘Date

REM CONEXAO COM O 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]”).sendVKey 0

session.findById(“wnd[0]/usr/ctxtPLAF-MATNR”).text = cOL2

session.findById(“wnd[0]/usr/ctxtPLAF-BERID”).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/ctxtPLAF-PEDTR”).text = cOL5

session.findById(“wnd[0]/usr/tabsTABTC/tabpTAB01/ssubINCLUDE1XX:SAPLM61O:0711/subINCLUDE711_1:SAPLM61O:0802/ctxtPLAF-PEDTR”).setFocus

session.findById(“wnd[0]/usr/tabsTABTC/tabpTAB01/ssubINCLUDE1XX:SAPLM61O:0711/subINCLUDE711_1:SAPLM61O:0802/ctxtPLAF-PEDTR”).caretPosition = 8

session.findById(“wnd[0]”).sendVKey 0

session.findById(“wnd[0]/tbar[0]/btn[11]”).press

objExcel.Cells(i, 6).Value = session.findById(“wnd[0]/sbar”).Text

NEXT




2- Add IF logic in the script


Suppose that we need to execute some steps only if we achieve certain condition. In this case, we can add if logic in the script.

For example, we can enter in one column an X to indicate that the condition was achieved. Then in the script we have to add the IF and END IF:

IF COL10 = “X” THEN

END IF

where 6 is the number of the column where the message will be collected.

As an example, here you can see a script that createnew BINs in WM (LS01N) and execute the stock transfer (LT10) from the old to the new BIN only if there was stock in the old BIN:

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 –> Conexion con 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))  ‘Warehouse

COL2   = Trim(CStr(objSheet.Cells(i, 2).Value))  ‘Storage Type

COL3    = Trim(CStr(objSheet.Cells(i, 3).Value))  ‘Bin

COL4    = Trim(CStr(objSheet.Cells(i, 4).Value))  ‘Storage Section

COL5    = Trim(CStr(objSheet.Cells(i, 5).Value))  ‘Verification ID

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

COL7    = Trim(CStr(objSheet.Cells(i, 7).Value))  ‘Storage Bin Type

COL8    = Trim(CStr(objSheet.Cells(i, 8).Value))  ‘Maximum Weight

COL9    = Trim(CStr(objSheet.Cells(i, 9).Value))  ‘Total Capacity

COL10   = Trim(CStr(objSheet.Cells(i, 10).Value)) ‘Execute LT10? If we need it populate an X in Column 10.

COL11   = Trim(CStr(objSheet.Cells(i, 11).Value)) ‘Old Storage Type

REM **************************************

session.findById(“wnd[0]/tbar[0]/okcd”).text = “/NLS01N”

session.findById(“wnd[0]”).sendVKey 0

session.findById(“wnd[0]/usr/ctxtLAGP-LGNUM”).text = COL1

session.findById(“wnd[0]/usr/ctxtLAGP-LGTYP”).text = COL2

session.findById(“wnd[0]/usr/ctxtLAGP-LGPLA”).text = COL3

session.findById(“wnd[0]/usr/ctxtLAGP-LGPLA”).setFocus

session.findById(“wnd[0]/usr/ctxtLAGP-LGPLA”).caretPosition = 6

session.findById(“wnd[0]”).sendVKey 0

session.findById(“wnd[0]/usr/tabsFUNC_TABSTRIP/tabpALLG/ssubD0400_S:SAPML01S:4001/ctxtLAGP-LGBER”).text = COL4

session.findById(“wnd[0]/usr/tabsFUNC_TABSTRIP/tabpALLG/ssubD0400_S:SAPML01S:4001/txtLAGP-VERIF”).text = COL5

session.findById(“wnd[0]/usr/tabsFUNC_TABSTRIP/tabpALLG/ssubD0400_S:SAPML01S:4001/ctxtLAGP-KOBER”).text = COL6

session.findById(“wnd[0]/usr/tabsFUNC_TABSTRIP/tabpALLG/ssubD0400_S:SAPML01S:4001/ctxtLAGP-LPTYP”).text = COL7

session.findById(“wnd[0]/usr/tabsFUNC_TABSTRIP/tabpALLG/ssubD0400_S:SAPML01S:4001/txtLAGP-LGEWI”).text = COL8

session.findById(“wnd[0]/usr/tabsFUNC_TABSTRIP/tabpALLG/ssubD0400_S:SAPML01S:4001/txtLAGP-LKAPV”).text = COL9

session.findById(“wnd[0]/usr/tabsFUNC_TABSTRIP/tabpALLG/ssubD0400_S:SAPML01S:4001/txtLAGP-LKAPV”).setFocus

session.findById(“wnd[0]/usr/tabsFUNC_TABSTRIP/tabpALLG/ssubD0400_S:SAPML01S:4001/txtLAGP-LKAPV”).caretPosition = 8

session.findById(“wnd[0]/tbar[0]/btn[11]”).press

IF COL10 = “X” THEN

session.findById(“wnd[0]/tbar[0]/okcd”).text = “/NLT10”

session.findById(“wnd[0]”).sendVKey 0

session.findById(“wnd[0]/usr/ctxtS1_LGNUM”).text = COL1

session.findById(“wnd[0]/usr/ctxtS1_LGTYP-LOW”).text = COL11

session.findById(“wnd[0]/usr/ctxtS1_LGPLA-LOW”).text = COL3

session.findById(“wnd[0]/usr/ctxtBWLVS-LOW”).text = “999”

session.findById(“wnd[0]/usr/radP_QUANT”).select

session.findById(“wnd[0]/usr/ctxtS1_LGPLA-LOW”).setFocus

session.findById(“wnd[0]/usr/ctxtS1_LGPLA-LOW”).caretPosition = 6

session.findById(“wnd[0]/tbar[1]/btn[8]”).press

session.findById(“wnd[0]/tbar[1]/btn[45]”).press

session.findById(“wnd[0]/tbar[1]/btn[48]”).press

session.findById(“wnd[1]/usr/chkRL03T-SQUIT”).selected = true

session.findById(“wnd[1]/usr/ctxtLAGP-LGTYP”).text = COL2

session.findById(“wnd[1]/usr/ctxtLAGP-LGPLA”).text = COL3

session.findById(“wnd[1]/usr/ctxtLTAK-BWLVS”).text = “999”

session.findById(“wnd[1]/usr/chkRL03T-SQUIT”).setFocus

session.findById(“wnd[1]/tbar[0]/btn[0]”).press

END IF

session.findById(“wnd[0]/tbar[0]/okcd”).text = “/NLS02N”

session.findById(“wnd[0]”).sendVKey 0

session.findById(“wnd[0]/usr/ctxtLAGP-LGNUM”).text = COL1

session.findById(“wnd[0]/usr/ctxtLAGP-LGTYP”).text = COL11

session.findById(“wnd[0]/usr/ctxtLAGP-LGPLA”).text = COL3

session.findById(“wnd[0]/usr/ctxtLAGP-LGPLA”).caretPosition = 6

session.findById(“wnd[0]”).sendVKey 0

session.findById(“wnd[0]/tbar[1]/btn[14]”).press

session.findById(“wnd[1]/usr/btnSPOP-OPTION1”).press


NEXT

Good luck with both new concepts!!

Assigned Tags

      11 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi Mariano,

      Once again, thank you for the script.

      When i run my script, it seemed SAP can't recognise this line. How do i get this fix?

      session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-BANFN[25,0]").Text = COL1


      Regards

      Janet

      Author's profile photo Mariano Cabalen
      Mariano Cabalen
      Blog Post Author

      Hi Janet,

      Sorry but I don´t know what could be your problem.

      Please open a thread and include more detail about your code and the error. I am not sure what is the right space for this.. I am sure the moderators can help you to move the thread in case it should not be PP.

      Kind Regards,

      Mariano

      Author's profile photo Nick Liu
      Nick Liu

      Hi Janet,

      In different scenario, the ID maybe different even though the execution seems the same. I recommend you to just re-reord the failed step and then replace the ID with the new ID you just recorded.

      Or you can use a tool named Screen reader which can be found in note: 1441550 . With this tool, you can get every field's ID and use the ID to replace the old one.

      BR,

      Nick

      Author's profile photo Alfonzo Vega
      Alfonzo Vega

      When you copy and paste from this blog, single quotes and double quotes may cause issues, something like 'instruction or character not recognized'. Just do a search and replace entering a single quote in the search and a single quote in the replace, same for double quotes, that may solve the issue.

      Author's profile photo Former Member
      Former Member

      Hello,

      I would like to know if there's any possibility to run a script with abap program or function.

      Thank you,
      Isabel

      Author's profile photo Preston Pickens
      Preston Pickens

      Hi Mariano,

       

      I was wondering if it is possible to "step" through a script after you have recorded and maintained the script? Kind of like an excel macro. Additionally, is there a way to terminate a script while it's running?

      Author's profile photo Arunesh Tripathi
      Arunesh Tripathi

      Terminating the script is bit difficult as it continuously moves from once screen to other, however, if you re-login to the system whereikn the script is running, enter your creds and select the option wherein system asks for terminating the existing logon. Alternatively, click on the top left corner of the SAP screen and click on "Stop transaction"

      Author's profile photo Sebastian Vazquez Alcocer
      Sebastian Vazquez Alcocer

      Hello, does anyone know can i make it repeat to take more data from excel?

       

      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

      session.findById("wnd[0]/tbar[0]/okcd").text = "/nqm01"
      session.findById("wnd[0]").sendVKey 0
      session.findById("wnd[0]/usr/ctxtRIWO00-QMART").text = "X4"
      session.findById("wnd[0]").sendVKey 0
      session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB02/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_1:SAPLIQS0:7530/cmbVIQMEL-PRIOK").key = "3"
      session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB02/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_1:SAPLIQS0:7530/subINTPAR:SAPLIPAR:0400/ctxtDIIHPA-I_PARNR").text = "50017627"
      session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB02/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_1:SAPLIQS0:7530/subVERA:SAPLIPAR:0450/ctxtRQM02-PARNR_VERA").text = "M003579"
      session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB02/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_3:SAPLIQS0:7322/subOBJEKT:SAPMQM00:3020/ctxtRQM00-MAWERK").text = "3306"
      session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB02/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_4:SAPLIQS0:7715/ctxtVIQMEL-QMGRP").text = "MX-C0001"
      session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB02/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_4:SAPLIQS0:7715/ctxtVIQMEL-QMCOD").text = "0110"
      session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB02/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_4:SAPLIQS0:7715/txtRIWO00-HEADKTXT").text = COL1
      session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB02/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_4:SAPLIQS0:7715/txtRIWO00-HEADKTXT").setFocus
      session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB02/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_4:SAPLIQS0:7715/txtRIWO00-HEADKTXT").caretPosition = 20
      session.findById("wnd[0]").sendVKey 0
      session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB02/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_4:SAPLIQS0:7715/cntlTEXT/shellcont/shell").text = "Segundo texto de descripcion" + vbCr + ""
      session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB02/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_4:SAPLIQS0:7715/cntlTEXT/shellcont/shell").setSelectionIndexes 28,28
      session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB10").select
      session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB10/ssubSUB_GROUP_10:SAPLIQS0:7210/tabsTAB_GROUP_20/tabp20\TAB01/ssubSUB_GROUP_20:SAPLIQS0:7110/tblSAPLIQS0POSITION_VIEWER/ctxtVIQMFE-OTEIL[2,0]").text = "0300"
      session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB10/ssubSUB_GROUP_10:SAPLIQS0:7210/tabsTAB_GROUP_20/tabp20\TAB01/ssubSUB_GROUP_20:SAPLIQS0:7110/tblSAPLIQS0POSITION_VIEWER/ctxtVIQMFE-FECOD[5,0]").text = "0210"
      session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB10/ssubSUB_GROUP_10:SAPLIQS0:7210/tabsTAB_GROUP_20/tabp20\TAB01/ssubSUB_GROUP_20:SAPLIQS0:7110/tblSAPLIQS0POSITION_VIEWER/txtVIQMFE-FETXT[7,0]").text = COL2
      session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB10/ssubSUB_GROUP_10:SAPLIQS0:7210/tabsTAB_GROUP_20/tabp20\TAB01/ssubSUB_GROUP_20:SAPLIQS0:7110/tblSAPLIQS0POSITION_VIEWER/txtVIQMFE-FETXT[7,0]").setFocus
      session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB10/ssubSUB_GROUP_10:SAPLIQS0:7210/tabsTAB_GROUP_20/tabp20\TAB01/ssubSUB_GROUP_20:SAPLIQS0:7110/tblSAPLIQS0POSITION_VIEWER/txtVIQMFE-FETXT[7,0]").caretPosition = 22
      session.findById("wnd[0]").sendVKey 0
      session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB10/ssubSUB_GROUP_10:SAPLIQS0:7210/tabsTAB_GROUP_20/tabp20\TAB01/ssubSUB_GROUP_20:SAPLIQS0:7110/tblSAPLIQS0POSITION_VIEWER").getAbsoluteRow(0).selected = true
      session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB10/ssubSUB_GROUP_10:SAPLIQS0:7210/tabsTAB_GROUP_20/tabp20\TAB01/ssubSUB_GROUP_20:SAPLIQS0:7110/tblSAPLIQS0POSITION_VIEWER/txtVIQMFE-POSNR[0,0]").setFocus
      session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB10/ssubSUB_GROUP_10:SAPLIQS0:7210/tabsTAB_GROUP_20/tabp20\TAB01/ssubSUB_GROUP_20:SAPLIQS0:7110/tblSAPLIQS0POSITION_VIEWER/txtVIQMFE-POSNR[0,0]").caretPosition = 0
      session.findById("wnd[0]/tbar[1]/btn[16]").press
      session.findById("wnd[0]/tbar[0]/btn[11]").press

      objExcel.Cells(i,6).Value = session.findById("wnd[0]/sbar").Text

      aux=col1 & " " & col2
      CreateObject("WScript.Shell").run("cmd /c @echo %date% %time% " & aux & " >> C:\SCRIPT\PlOrCreationLog.txt")
      Next

      Author's profile photo Edward Garzon
      Edward Garzon
      Hello, validate your loop, it is wrong.
      
      regards
      
      Eagarzon
      +513132257388
      Author's profile photo farshad tirandazi
      farshad tirandazi

      Thanks @Mariano for your great blog.

      Actually I am facing for a solution to make some mass changes on items level.

      I have some routing operation and I want to add different no. of characteristic for each operation. in one operation there is two character and in other one there might be ten character.

      is there anyway using this method for item level.

      Best Regards,

      Author's profile photo Edward Garzon
      Edward Garzon

      Hello how are you? Of course, if possible, write to me on whatsapp. +573132257388

      saludos.

       

      https://www.youtube.com/channel/UCm1gKCg_aNAM0TKqBpAe2Rw

      ED-