Posted on 23 December 2009

Once your Quality Center users discover that they can add attachments to defects (and test cases, and test sets etc etc), your storage requirements increase dramatically. Here is a neat way to prevent your users from attaching files over a certain size anywhere in Quality Center.

A client had a requirement to reduce the use of QC attachment storage as a lot of users are using up a lot of space unnecessarily. In one instance I found over 80GB of file space taken up with a 100MB file endlessly repeated. The attachment was on a “template” test and the users had duplicated it whenever they copied and pasted the “template”

I had to get this working today so investigated the attachment object and found it has some hooks so we could get it working quite simply.

The “Attachment” object available in the sub can give you access to the “CR_ENTITY” it is linked to. This gives us ALL the areas where a user can create an attachment. From there it was simply trial and error until I had the attachment record (attRec) described correctly for each “CR_ENTITY”.

The code goes directly into the QC workflow Common scripts area and replaces the existing Sub “Attachment_New”.

Sub Attachment_New(Attachment)
  'Use ActiveModule and ActiveDialogName to get the current context.
  On Error Resume Next

    'Remove new attachment if file size is too big
    If Attachment.Type = 1 then 'Attachment is of type TDATT_FILE - a file.
       MaxFileSize = 3145728 'Set the maximum attachment size in Bytes <<- 3mb is our limit!
       If Attachment.FileSize > MaxFileSize then
          Select Case Attachment.Field("CR_ENTITY")
                 Case "REQ"
                      Set attRec = ReqFactory.Item(Attachment.Field("CR_KEY_1"))
                 Case "ALL_LISTS" 'Folder
                      Set attRec = TreeManager.NodeByID(Attachment.Field("CR_KEY_1"))
                      msgbox attRec.ID
                 Case "TEST" ' Test
                      Set attRec = TestFactory.Item(Attachment.Field("CR_KEY_1"))
                 Case "DESSTEPS" 'Test step
                      Set attTest = TestFactory.Item(Test_Fields("DS_TEST_ID").Value)
                      Set attRec = attTest.DesignStepFactory.Item(Attachment.Field("CR_KEY_1"))
                 Case "CYCL_FOLD" 'Folder
                      Set attRec = TestSetTreeManager.NodeById(Attachment.Field("CR_KEY_1"))
                 Case "CYCLE" 'Test Set
                      Set attRec = TestSetFactory.Item(Attachment.Field("CR_KEY_1"))
                 Case "TESTCYCL" 'TestInstance
                      Set attRec = TSTestFactory.Item(Attachment.Field("CR_KEY_1"))
                 Case "RUN" 'Run
                      Set attRec = RunFactory.Item(Attachment.Field("CR_KEY_1"))
                 Case "STEP"  'Run Step
                      Set attRun = RunFactory.Item(Step_Fields("ST_RUN_ID").Value)
                      Set attRec = attRun.StepFactory.Item(Attachment.Field("CR_KEY_1"))
                 Case "BUG"
                      Set attRec = BugFactory.Item(Attachment.Field("CR_KEY_1"))
          End Select
          set AttFac = attRec.Attachments
          Msgbox "Please exit this record and refresh the QC page. The file: " & vbLF & _
                 Attachment.Name & vbLf & " has been removed as it exceeds the required file size of 3mb."
       End If
    End If

  On Error GoTo 0
End Sub


I have to restrict the attachment length to less than 50 characters.
Can you help, how to do this

Thanks, it worked fine for me.

Michael te Wierik

I found that when creating a Defect and adding an attachment QC does something novel.

In order to attach an item QC requires an entity to exist. In this case, on Bug_New we use “actAttachFile” and before actually populating the Defect with field values it creates a temporary bug entity and attaches the attachment. This temporary entity is inaccessable. Then when you hit post it updates the rest of the fields.

The script above cannot work with this as there is no visible ID is can use to check/change attachements.

I have not looked into what happens when you cancel after adding an attachment on bug new, but I assume there is a roll back function that gets called by QC.

The problem is the Attachment_New method does not get invoked when CREATING a defect and adding an attachment…. only when working with an existing defect and adding an attachment. HP has been notified of this BUG, but as of QC 9.2 they still have not fixed it.

Leave a Reply