Here is a sample on how to handle multiple selected objects in Workflow code. Hope this will bring you some clues.
Assume if we need to create a custom rule for the deletion of Test instances in Test Lab using workflow, and we want only the test instances with 'N/A' or 'No Run' status can be deleted. For the test instances with other status, the deletion will not be allowed. If we use the regular workflow code as below, it will work when a single test instance is selected. But if we select multiple test instances by holding Shift key or Ctrl key, it will not work correctly- If the selected entities are combination of No Run, N/A, Passed, Failed, Not Completed, the code will not work and will continue to delete the entries.
On Error Resume Next
If TestSetTest_Fields.Field("TC_STATUS").Value = "No Run" or TestSetTest_Fields.Field("TC_STATUS").Value = "N/A" Then
TestSet_CanRemoveTests = True
else TestSet_CanRemoveTests = False
msgbox "Only the test instance status with No Run or N/A can be removed"
On Error GoTo 0
To handle the situation when multiple test instances selected, we need to apply the following code in workflow:
dim oTDC, oRS, oCmd
dim strGen, strSQL
dim iCountExec, iCountNotExec
'On Error Resume Next
blnDelete = false
‘Get selected Test Instances IDs into a string
for idx = 0 to ubound(Tests)
strGen = strGen & Tests(idx) & ", "
' Build SQL string using previous test Instances IDs
strGen = Left(strGen,Len(strGen)-2)
strSQL = "SELECT tc_status from testcycl where tc_testcycl_id in ( " & strGen & " )"
‘execute the SQL String
' create DB object & get data
set oTDC = TDConnection
set oCmd = oTDC.Command
oCmd.CommandText = strSQL
set oRS = oCmd.Execute
‘Count the number of executed test instances and non-executed test instances
if oRS.RecordCount > 0 then
' has records.
for idx = 0 to oRS.RecordCount -1
strGen = oRS.FieldValue(0)
if ucase(strGen) = "N/A" or ucase(strGen) = "NO RUN" then
iCountNotExec = iCountNotExec + 1
iCountExec = iCountExec + 1
'Decision as whether to delete or not
msgbox "Exec Count: " & iCountExec & " Not exec Count: " & iCountNotExec
if iCountExec = 0 and iCountNotExec > 0 then
blnDelete = true
msgbox "Only 'N/A' or 'No Run' test instance status can be deleted",48,"Unable to Delete Alert"
' Clean up and free the memory resource after been using database and record set objects
set oRS = nothing
set oCmd = nothing
set oTDC = nothing
TestSet_CanRemoveTests = blnDelete
'On Error GoTo 0
Special thanks for Jason Fitzpatrick to his effort on providing this sample.
Thanks & Regards