Search code examples
vbscripthp-quality-centerhp-alm

Check if object exists in VBScript


I am using the HP ALM API to fetch a field from the database. The problem is that it can happend, that this field does not exist on the project, and everytime that happens, I get the error bellow.

How can I check the field object properly to make sure that I don't get this "Invalid customization field name" anymore?

Code:

Set field = custFields.Field("TEST", "TS_USER_88") <-- crashes here

label = field.UserLabel
If label = Null Then
    Print "[" & Sysdate() & "] Project can NOT be migrated..."
    Print "[" & Sysdate() & "] FIELD TS_USER_88 NOT FOUND - PROJECT IS NOT SUPPORTED."
Else
    ...
End If

Error:

xy.vbs(126, 7) (null): Invalid customization field name

Solution

  • You'll want to wrap your code in "On Error Resume Next", then handle the error.

    On Error Resume Next
    Set field = custFields.Field("TEST", "TS_USER_88")
    
    If Err.Number <> 0 Then
      'Do Something to handle your error
      'stuff
    
      'Clear the error
      Err.Clear
    End If
    On Error Goto 0
    
    'more stuff down here
    

    Here's some more info on the Err object and some of it's properties:

    http://msdn.microsoft.com/en-us/library/sbf5ze0e(v=vs.84).aspx