Action hook that sets the value of a parent record

Use the following action hook along with parent/child linking to create a state-based relationship between a parent record and its children. This hook moves the parent record to the next state if all the children are in that state.

Note: This code assumes certain field names and record types. If you use this code in your own environment, you must changes some of the field names and record types used in this example. See Action commit hook example for more error handling details when using the Validate and Commit methods.

VBScript


Dim SessionObj
Dim ParentID 

' Dimension variables that hold objects
Dim ParentObj 
Dim ChildRefList 
Dim ChildArray 
Dim ChildID 
Dim DefectChildEntityObj 
Dim StateStatus 
Dim SameState 
Dim CurrentState 
Dim ActionJustPerformed 

Dim RetValue 

set SessionObj = GetSession 

ThisID = GetDisplayName 

ActionJustPerformed = GetActionName 

SessionObj.OutputDebugString "action is: "& ActionJustPerformed & vbCrLf

StateStatus = "" 

SameState = 0 

SessionObj.OutputDebugString "current db id is: " & ThisID & vbCrLf 

ParentID = GetFieldValue("parent").GetValue() 

SessionObj.OutputDebugString "parent id is: " & ParentID & vbCrLf 

if ParentID <> "" then 

  set ParentObj=SessionObj.GetEntity("defect", parent_id) 

' you can also call the GetValueAsList method instead of 
' calling GetValue and using the split utility
  ChildRefList=ParentObj.GetFieldValue("children").GetValue 
  ChildArray= split (ChildRefList, vbLf) 

  For Each ChildID In ChildArray 

    set DefectChildEntityObj=SessionObj.GetEntity("Defect", ChildID)

    CurrentState=DefectChildEntityObj.GetFieldValue ("State").GetValue 

    SessionObj.OutputDebugString "StateStatus is: " & StateStatus & vbCrLf 

    SessionObj.OutputDebugString "CurrentState is: " & CurrentState &vbCrLf

    SessionObj.OutputDebugString "SameState is: " & SameState & vbCrLf 

    if StateStatus = "" then 

      StateStatus = CurrentState 

      SameState = 1

      SessionObj.OutputDebugString "coming to statestatus is null" & vbCrLf 


    elseif StateStatus = CurrentState then 

      SessionObj.OutputDebugString "coming to same state" & vbCrLf 

      SameState = 1 

    else 

      SessionObj.OutputDebugString "states are different" & vbCrLf 

      SameState = 0 

    end if 
  Next 

  if SameState = 1 then 

    SessionObj.OutputDebugString "samestate=1, setting parent state" 

          & vbCrLf

    SessionObj.EditEntity ParentObj, ActionJustPerformed 

    status = ParentObj.Validate 

    if (status <> "") then 
    SessionObj.OutputDebugString "error when updating parent state: "_
     & status & vbCrLf 
      ParentObj.Revert

      exit sub

    end if 

    ParentObj.Commit 

  end if 

end if  

Perl


$SessionObj=$entity->GetSession();

$ThisID=$entity->GetDisplayName();

$ActionJustPerformed=$entity->GetActionName();

$ParentID=$entity->GetFieldValue("parent1")->GetValue();

$StateStatus="";

$SameState=0;


# HCL Compass has a message monitor to display 

# HCL Compass messages and your messages

$SessionObj->OutputDebugString ("perl current db id is: $ThisID\n");

$SessionObj->OutputDebugString ("perl parent id is:  $parent_id\n");


if ($ParentID ne "")  {                    

    $ParentObj = $SessionObj->GetEntity("defect", $ParentID);

# you can also call the GetValueAsList method instead of 
# calling GetValue and using the split utility

    $ChildRefList=$ParentObj->GetFieldValue("children")->GetValue();

    $SessionObj->OutputDebugString ("children are: $ChildRefList\n");

    @ChildArray = split (/\n/,$ChildRefList);   

foreach $ChildID (@ChildArray) {

    $DefectChildEntityObj = $SessionObj->GetEntity("defect", $ChildID);

    $CurrentState=$DefectChildEntityObj->GetFieldValue("State")->
          GetValue();

    $SessionObj->OutputDebugString("perl StateStatus is: $StateStatus\n");

    $SessionObj->OutputDebugString("perl Current Status is: 

          $CurrentStatus\n");

    $SessionObj->OutputDebugString("perl SameState is: $SameState\n");

    if ($StateStatus eq "") {

        $StateStatus = $CurrentState;

        $SameState = 1;

        $SessionObj->OutputDebugString("coming to statestatus is null\n");

    } elsif ($StateStatus eq $CurrentState)  {

        SessionObj->OutputDebugString ("coming to same state\n");

        $SameState = 1;

    } else   {

        $SessionObj->OutputDebugString("states are different\n");

        $SameState = 0;

    }    #nested if statements

} #End foreach Loop


if ($SameState == 1) {

    $SessionObj->OutputDebugString("samestate = 1, setting parent
         state \n");

    $SessionObj->EditEntity($ParentObj, $ActionJustPerformed);

    $status = $ParentObj->Validate();

    if ($status ne "")  {

        $SessionObj->OutputDebugString ("error when updating parent state:
           $status\n");
    $ParentObj->Revert();

    return -1;  # Exit

    }

    $ParentObj->Commit();

}    #end if for ($SameState == 1)

}    #end if for ($ParentID ne "")