Field default value hook examples

When the user creates a record, HCL Compass software creates the record and initializes its fields to appropriate default values. If a particular field has a default-value hook associated with it, the hook is run to set the field value. If no hook is associated with the field, a default value that is appropriate for the field type is assigned. For example, integer fields are set to 0, string fields are set to an empty string, and list fields are set to an empty list. Date fields are not initialized to a default value; always provide a default-value hook for date fields.

Note: Because hooks run with administrator privileges, these examples work even if the behavior of the fields is read-only.

The following example shows how to initialize a date field to the current date and time:

VBScript


Sub submit_date_DefaultValue(fieldname)

    ' fieldname As String
    ' entityDef = defect
    call SetFieldValue(fieldname, now)

End Sub 

Perl


# Define a function for the current timestamp

sub GetCurrentDate {

    my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $time) =
      localtime();
    return sprintf("%4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", $year + 1900, 
      $mon + 1, $mday, $hour, $min, $sec);
}

# Define a routine to call the timestamp function

sub Submit_Date_DefaultValue {

   my($fieldname) = @_;
   # $fieldname as a string scalar and entityDef is Defect
   $entity->SetFieldValue($fieldname, GetCurrentDate());

} 

The following example initializes the "submitter" field to the login name of the person who is submitting the record:

VBScript


Sub submitter_DefaultValue(fieldname)

    ' fieldname As String
    ' entityDef = swbug

    SetFieldValue fieldname, GetSession().GetUserLoginName()

End Sub 

Perl


sub Submitter_DefaultValue {

    my($fieldname) = @_;

    # $fieldname as a string scalar

    # entityDef is Defect

    my $session;

    my $username;

    $session = $entity->GetSession();

    $username = $session->GetUserLoginName();

    $entity->SetFieldValue($fieldname, $username);

}