Examples of pessimistic record locking hook code

You can use these sample scripts to help you enable and manage pessimistic record locking.

Lock record script for BASE action Action_Initialization hook

sub Defect_Initialization {
    my($actionname, $actiontype) = @_;
    # $actionname as string scalar
    # $actiontype as long scalar
    # action is LockRecord
    # record type name is Defect
    # Do any setup for the action here.
	  $entity->LockRecord(0);
}

Lock record script for RECORD_SCRIPT_ALIAS action

sub Defect_LockRecord {
    my($result);
    my($param) = @_;
    # record type name is Defect
	  $entity->LockRecord(0);
    return $result;
}

Unlock record script for RECORD_SCRIPT_ALIAS action

Perl

sub Defect_Unlock {
    my($result);
    my($param) = @_;
    # record type name is Defect

    $result = "";
    my $locked_by = $entity->GetLockOwner();
    if ($locked_by ne "") {
        my $do_unlock = $session->IsUserSuperUser();
        if (! $do_unlock) {
            # If the current user holds the lock, let them unlock it.
            my $username = $session->GetUserLoginName();
            if ($username =~ /^$locked_by$/i) {
                $do_unlock = 1;
            }
        }
    if (! $do_unlock) {
        # Additional options to "authorize" unlocking:
        # 1) allow if user is a member of an "unlock" group
        # get user's groups, check if member
        # 2) allow for privileged users, e.g. Security Administrator
        # check session for the chosen privilege
        # 3) many other possibilities
        #
        # if ( user-should-be-allowed-to-unlock-the-record ) {
            # $do_unlock = 1;
        # }
    }
    if ($do_unlock) {
        $entity->UnlockRecord();
    }
    else {
        $result = "You are not allowed to unlock this record.";
    }
    }
    return $result;
}

VBScript

Function Defect_Unlock(param)
  ' param As Variant
  ' record type name is Defect
    REM add your hook code here
    Dim result
    Dim session
    Dim locked_by
    ' Get the session
    set session = GetSession
    locked_by = GetLockOwner
    if (locked_by <> "") then
        Dim do_unlock
        do_unlock = session.IsUserSuperUser
        if (NOT do_unlock) then
            ' If the current user holds the lock, let them unlock it.
            Dim username
            username = session.GetUserLoginName
            if (username = locked_by) then
                do_unlock = true
            end if
        end if
        if (NOT do_unlock) then
            ' Additional options to "authorize" unlocking:
            '
            ' 1) allow if user is a member of an "unlock" group
            '    get user's groups, check if member
            '
            ' 2) allow for some privileged users, e.g. Security Administrator
            '    check session for the chosen privilege
            '
            ' 3) many other possibilities
        end if
        if (do_unlock) then
            UnlockRecord
        else
            result = "You are not allowed to unlock this record."
        end if
    end if
End Function