SAP GUI Scripting can be used instead of an ABAP program or can be used instead of an LSMW.
In a previous blog post, I have discussed how to write SAP Scripting – BOM creation and this blog post will tell you about another one attempt to demonstrate selection condition creation in SAP VC.
In this blog post, I have given the program recorded for creating the selection condition in SAP VC through SAP GUI Scripting.
If you want to understand what SAP Variant Configuration is, you can read the previous blog where I have explained what SAP VC is and how it is used in the manufacturing industry.
Before we are trying to talk about Scripting for selection conditions, let me explain what selection condition is and how it is used in SAP VC.
What is the selection condition in SAP VC?
Selection Condition is one type of Object dependency in SAP VC which is connecting each BOM item with character value.
The character value will be chosen in sales order from characteristics or specifications of a class or product while placing sales order for a particular variant.
For Example, Volvo FM 400 Truck is class or Product, Color is Characteristics and value can be Blue, Yellow, or red.
Here If the customer wants to get a Yellow color Variant in Volvo Truck, he can choose the Yellow color in the classification used in the sales order.
Selection Condition name: Volvo_FM_Truck_SC
Object Dependency: $self.volvo_fm_truck_color = ‘Yellow’ where volvo_fm_truck_color is characteristics and Yellow is the Character Value.
So, I have recorded the SAP Scripting for the selection condition for one item.
And I have modified the same program for multiple BOM items with a While loop.
To do this modification you should know how macro coding is working in an excel sheet and macro setting needs to enable execution.
So, you can find the below sap scripting for selection condition,
Above sheet used to fill and upload the SC rules in SAP VC.
Program as follows,
Option Explicit
Public SapGuiAuto, WScript, msgcol
Public objGui As GuiApplication
Public objConn As GuiConnection
Public objSess As GuiSession
Public objSBar As GuiStatusbar
Public objSheet As Worksheet
Dim W_System
Dim iCtr As Integer
Const tcode = “CU01”
Function Attach_Session(iRow, Optional mysystem As String) As Boolean
Dim il, it
Dim W_conn, W_Sess
‘ Unless a system is provided (XXXYYY where XXX is SID and YYY client)
‘ get the system from the sheet (in this case it is in cell A8)
If mysystem = “” Then
W_System = ActiveSheet.Cells(iRow, 1)
Else
W_System = mysystem
End If
‘ If we are already connected to a session, exit do not try again
If W_System = “” Then
Attach_Session = False
Exit Function
End If
‘ If the session object is not nil, use that session (assume connected to the correct session)
If Not objSess Is Nothing Then
If objSess.Info.SystemName & objSess.Info.Client = W_System Then
Attach_Session = True
Exit Function
End If
End If
‘ If not connected to anything, set up the objects
If objGui Is Nothing Then
Set SapGuiAuto = GetObject(“SAPGUISERVER”)
Set objGui = SapGuiAuto.GetScriptingEngine
End If
‘ Cycle through the open SAP GUI sessions and check which is in the same system running the matching transaction
For il = 0 To objGui.Children.Count – 1
Set W_conn = objGui.Children(il + 0)
For it = 0 To W_conn.Children.Count – 1
Set W_Sess = W_conn.Children(it + 0)
If W_Sess.Info.SystemName & W_Sess.Info.Client = W_System And W_Sess.Info.Transaction = tcode Then
Set objConn = objGui.Children(il + 0)
Set objSess = objConn.Children(it + 0)
Exit For
End If
Next
Next
‘ If nothing is found, display and error message
If objSess Is Nothing Then
MsgBox “No active session to system ” + W_System + ” with transaction ” + tcode + “, or scripting is not enabled.”, vbCritical + vbOKOnly
Attach_Session = False
Exit Function
End If
‘ Turn on scripting
If IsObject(WScript) Then
WScript.ConnectObject objSess, “on”
WScript.ConnectObject objGui, “on”
End If
‘ Maximize the window of the connected session
Set objSBar = objSess.findById(“wnd[0]/sbar”)
objSess.findById(“wnd[0]”).Maximize
Attach_Session = True
End Function
Public Sub StartProcessing()
Dim W_Obj1, W_Obj2, W_Obj3, W_Obj4, iRow
Dim W_Func
Dim W_Src_Ord
Dim W_Ret As Boolean
Dim itemcount As Integer
Dim itemmax As Integer
Const startrow As Integer = 11 ‘First row with actual data
Set objSheet = ActiveWorkbook.ActiveSheet
‘ Connect to a system stored in cell A8
W_Ret = Attach_Session(8)
If Not W_Ret Then
MsgBox “Not connected to client”
GoTo MyEnd
End If
itemcount = 0
itemmax = 0
‘ Determine the number of items to be processed: where the status is zero
For iRow = startrow To objSheet.UsedRange.Rows.Count
If objSheet.Cells(iRow, 4) = “0” Then
itemmax = itemmax + 1
End If
Next
‘ Update the counter in cell A9
objSheet.Cells(9, 1) = itemcount & “/” & itemmax
‘ Cycle through the rows with status 0 and call the ProcessRow function to process them
For iRow = startrow To objSheet.UsedRange.Rows.Count
If objSheet.Cells(iRow, 4) = “0” Then
Call ProcessRow(iRow)
itemcount = itemcount + 1
objSheet.Cells(9, 1) = itemcount & “/” & itemmax
End If
Next
MyEnd:
‘ destory the objects, free up memory
Set objSess = Nothing
Set objGui = Nothing
Set SapGuiAuto = Nothing
MsgBox “SC Rule Created.”, vbInformation + vbOKOnly
End Sub
Function ProcessRow(iRow)
Dim W_BPNumber, W_SearchTerm, W_Odrule
Dim lineitems As Long
‘ Set the line status to “processing…”
objSheet.Cells(iRow, 4) = 1
‘BP Number
If objSheet.Cells(iRow, 1) <> “” Then
W_BPNumber = objSheet.Cells(iRow, 1)
Else
W_BPNumber = “xxxxxx”
End If
‘Seartch term
If objSheet.Cells(iRow, 2) <> “” Then
W_SearchTerm = objSheet.Cells(iRow, 2)
Else
W_SearchTerm = “”
End If
‘Seartch term
If objSheet.Cells(iRow, 3) <> “” Then
W_Odrule = objSheet.Cells(iRow, 3)
Else
W_Odrule = “”
End If
‘ Set error handling to the below code where we can capture if a line had failed in the GUI script
On Error GoTo myerr
‘ SAP GUI Script starts here
objSess.findById(“wnd[0]”).Maximize
objSess.findById(“wnd[0]/tbar[0]/okcd”).Text = “/ncu01”
objSess.findById(“wnd[0]”).sendVKey 0
objSess.findById(“wnd[0]/usr/ctxtRCUKD-KNNAM”).Text = W_BPNumber
objSess.findById(“wnd[0]/usr/ctxtRCUKD-KNNAM”).caretPosition = 10
objSess.findById(“wnd[0]”).sendVKey 0
objSess.findById(“wnd[0]/usr/txtRCUKD-KNKTX”).Text = W_SearchTerm
objSess.findById(“wnd[0]/usr/txtRCUKD-KNKTX”).caretPosition = 14
objSess.findById(“wnd[0]/tbar[1]/btn[7]”).press
objSess.findById(“wnd[0]/usr/cntlSOURCE/shellcont/shell”).Text = W_Odrule + vbCr + “” + vbCr + “”
objSess.findById(“wnd[0]/usr/cntlSOURCE/shellcont/shell”).SetSelectionIndexes 22, 22
objSess.findById(“wnd[0]/tbar[0]/btn[3]”).press
objSess.findById(“wnd[0]/usr/radRCUKD-KNABD”).Select
objSess.findById(“wnd[0]/usr/ctxtRCUKD-KNSTA”).Text = “1”
objSess.findById(“wnd[0]/usr/ctxtRCUKD-KNSTA”).SetFocus
objSess.findById(“wnd[0]/usr/ctxtRCUKD-KNSTA”).caretPosition = 1
objSess.findById(“wnd[0]”).sendVKey 0
objSess.findById(“wnd[0]/tbar[0]/btn[11]”).press
‘ Gets the message from the status bar and save it in column B
objSheet.Cells(iRow, 5) = objSBar.Text
‘ Update the Status to “Completed” and exit
objSheet.Cells(iRow, 4) = 2
Exit Function
myerr:
‘ Update the status to “Error”
objSheet.Cells(iRow, 4) = 3
End Function
So, if you want you can copy and paste it in your excel sheet and you can check how it is working and please feel free to ask any questions. Thanks, and Cheers.