UnlockRecord

Description

Releases the lock on the record. Only the user that owns the lock, or a user with superuser privilege, or a user that a schema designer authorizes, can remove a lock. Throws an exception to indicate either the record is locked by another user, another session, or the record was updated.

Record locks are automatically released when the action is committed or reverted. You can use a RECORD_SCRIPT_ALIAS hook to manually remove a lock that has been abandoned.

To enable manual record unlocking, for each record type:
  • Create a new Record Script named Unlock.
  • Add a new action named Unlock of type RECORD_SCRIPT_ALIAS.
  • Set the action Record Script to the Unlock script.

Syntax

Perl


$entity->UnlockRecord(); 
Identifier
Description
entity
An Entity object representing a user data record.
Return value
None.

Example

Perl

sub Defect_Unlock {
    my($result);
    my($param) = @_;
    # record type name is Defect
    if (ref ($param) eq "CQEventObject") {
        # add your CQEventObject parameter handling code here
    } elsif (ref (\$param) eq "SCALAR") {
        # add your scalar parameter handling code here
        # The Web clients support scalar parameter type only,
        # so the hook code added in the above section, needs to be duplicated here
    } else {
        # add your handling code for other type parameters here, for example:
        # die("Unknown parameter type");
    }
    $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 some privileged users, e.g. Security Administrator
            #    check session for the chosen privilege
            #
            # 3) many other possibilities
        }
        if ($do_unlock) {
            $entity->UnlockRecord();
        }
        else {
            $result = "You are not allowed to unlock this record.";
        }
    }
    return $result;
}