Wednesday 8 October 2014

HP ALM Workflow with AuditRecord and AuditProperty to retrieve defect history with old value and new value

HP ALM Workflow with AuditRecord and AuditProperty to retrieve defect history with old value and new value

Why this blog entry? 

I need a solution that allow me to retrieve the latest history changes on defect STATUS (old and new value). Unfortunately, I'm not able to get a quick solution from the net.

The Solution

 Set AuditRecordFactory = TDConnection.AuditRecordFactory
Set AuditRecordFactoryFilter = AuditRecordFactory.Filter
AuditRecordFactoryFilter.Filter("AU_ENTITY_TYPE") = "BUG"
AuditRecordFactoryFilter.Filter("AU_ENTITY_ID") = "10327" 'Defect ID'
AuditRecordFactoryFilter.Order("AU_TIME") = 1
AuditRecordFactoryFilter.OrderDirection("AU_TIME") = 1 'Sort the list in decending order
Set AuditRecordList = AuditRecordFactory.NewList(AuditRecordFactoryFilter.Text)

If AuditRecordList.Count < 10 Then
    For Each audit In AuditRecordList
 
       'Below line will retrieve changes made against Defect Status
       Set AuditPropertyFactoryFilter = audit.AuditPropertyFactory.Filter
       AuditPropertyFactoryFilter.Filter("AP_FIELD_NAME") = "BG_STATUS"
       AuditPropertyFactoryFilter.Filter("AP_ACTION_ID") = audit.ActionID 'This line is needed. Else, you will be retrieving all the records
     
       Set ChangeList = audit.AuditPropertyFactory.NewList(AuditPropertyFactoryFilter.Text)
   
       If ChangeList.Count = 1 Then
           For Each Change In ChangeList
               MsgBox "User [" & audit.UserName & "] performed [" & audit.Action & "] at [" & audit.Time & "] with detail:"
               MsgBox "FieldName: [" & Change.FieldName & "] @ OldValue: [" & Change.OldValue & "] @ NewValue: [" & Change.NewValue & "]"
           Next
         
           Exit For
       ElseIf ChangeList.Count > 1 Then
           MsgBox "Warning!, Audit Property has more than 1 changes on BG_STATUS. Changes count is " & ChangeList.Count
       End If
    Next
Else
    MsgBox "Audit Record Count: " & AuditRecordList.Count
End If

Set tdConnection = Nothing

Happy testing!