IBM Support

How to write a Perl hook in IBM Rational ClearQuest to get the record ID and write it into a new field with some prefix value

Question & Answer


Question

How do you write a Perl hook in IBM Rational ClearQuest to get the record ID and write it into a new field with some prefix?

Cause

You would like to design your schema in such way so that by seeing the ID itself, you should be able to identify if it is a Enhancement type or Defect type record.

Answer

You may achieve this with the following approach-

  • Create a new field which is SHORT_STRING type.
  • Get the value of id field, add some prefix to it
  • Write this new value to the newly create field

Detailed information on creating Attributes or fields is available in our product documentation guide.

Procedure:
  1. Login to ClearQuest Designer with the role admin

  2. Click on Record Types > Navigate to the Record Type (For example Defect) > States And Actions > Actions > Submit

  3. Double click on Submit action, this will show all the Actions in right side panel

  4. Select the row of Submit action and click on the cell under the Validation column. It will open a drop down button, click on it and select SCRIPTS -> Add Perl Script

  5. This will Open the Validation Subroutine in Perl Script Editor

    Example-

    sub Defect_Validation {
       my($actionname, $actiontype) = @_;
       my $result;
                  # $actionname as string scalar
                  # $actiontype as long scalar
                  # $result as string scalar
                  # action is Resolve
                  # record type name is Defect
                  # Start User Code
                  # Return a non-empty string explaining why the action cannot commit
                  # with its current values. If it is valid, return an empty string.
                        # Example:
                             #    my $value_info = $entity->GetFieldValue("some field");
                             #    if (length($value_info->GetValue()) < 10)                       {
                             #        $result = "Must be at least 10 chars long";
                             #    }
                             # End User Code
                             return $result;
                             }

  6. Place the following code within this subroutine before # End User Code

    #######################################################
    $sessionObj = $entity->GetSession();
          my $ctobj = $entity->GetFieldValue("ChangeType");
          my $changetype = $ctobj->GetValue();

                my $idobj = $entity->GetFieldValue("id");
                my $currid = $idobj->GetValue();

         if ($changetype && $currid)
     {
         if ($changetype eq "Defect")
               {
               $newcqid = "D-"."$currid";
               $entity->SetFieldValue("CQID", $newcqid);
               }
              else
          {
    $newcqid = "E-"."$currid";
    $entity->SetFieldValue("CQID", $newcqid);
          }
     }

    ########################################################
    NOTE: The below hook code will add a Prefix D- or E- to the ID field and will write into the new field CQ_ID

  7. Save the work and perform Validation and check for errors

  8. If no errors, perform a Test Work

9. Verify in the Test Database, if it is working as expected

10. Once verified in the Test Database, CheckIn the Schema and Upgrade the Database

[{"Product":{"code":"SSSH5A","label":"Rational ClearQuest"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"Designer - Hooks","Platform":[{"code":"PF033","label":"Windows"}],"Version":"8.0.1","Edition":"","Line of Business":{"code":"LOB45","label":"Automation"}}]

Document Information

Modified date:
16 June 2018

UID

swg21666415