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.

Perl


$SessionObj=$entity->GetSession();

$ThisID=$entity->GetDisplayName();

$ActionJustPerformed=$entity->GetActionName();

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

$StateStatus="";

$SameState=0;


# DevOps Plan has a message monitor to display 

# DevOps Plan 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 "")