The sample code on how to handle multiple selected objects (such as Test or Test Instance .etc) in workflow

  • KM01083626
  • 07-Aug-2014
  • 07-Aug-2014

This document is under revision.

Summary

In workflow, we can use the regular workflow code to handle the situation when single object is selected. But if we select multiple objects at the same time by holding Shift key on the keyboard, there is more work to be done.

Question

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.
 
Function TestSet_CanRemoveTests(Tests)
  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"
 
  end if
 
  PrintError "TestSet_CanRemoveTests"
 
  On Error GoTo 0
 
End Function
 
To handle the situation when multiple test instances selected, we need to apply the following code in workflow:
 
Function TestSet_CanRemoveTests(Tests)
dim oTDC, oRS, oCmd
dim strGen, strSQL
dim idx
dim iCountExec, iCountNotExec
dim blnDelete
 
'On Error Resume Next
 
    blnDelete = false
 
‘Get selected Test Instances IDs into a string
    for idx = 0 to ubound(Tests)
       strGen = strGen & Tests(idx) & ", "
    next
 
' 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
          else
             iCountExec = iCountExec + 1
           end if
            oRS.Next
       next
 
 'Decision as whether to delete or not
       msgbox "Exec Count: " &   iCountExec & "     Not exec Count: " &   iCountNotExec
 
       if iCountExec = 0 and iCountNotExec > 0 then
          blnDelete = true
       else
           msgbox "Only 'N/A' or 'No Run' test instance status can be deleted",48,"Unable to Delete Alert"
       end if
    end if
 
' 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
 
End Function
 
Special thanks for Jason Fitzpatrick to his effort on providing this sample.
 
Thanks & Regards