Script repository

Add target user as approver for pending requests created during X days

Updated on: Jan 18, 2026, Views: 12138

Approval requests

The script adds the target user as approver for pending requests created during the specified number of days. To execute the script, create a business rule, custom command or scheduled task configured for the User object type.

Parameters

  • $requestExpirationDays - the number of days an approval request needs to remain pending to be updated.
  • $subject - the subject of email notifications that will be sent to new approver.
  • $messageTemplate - the email notification template. In the template, the {0} placeholder will be replaced with the operation description and {1} with the date when the request was submitted.
$requestExpirationDays = 30 # TODO: modify me
$subject = "New request awaiting your approval" # TODO: modify me
$messageTemplate = @"
Dear %name%,

New request awaiting your approval

Operation: {0}
Requested on: {1}
"@ # TODO: modify me

# Bind to the 'Approval Requests' container.
$approvalsPath = $Context.GetWellKnownContainerPath("ApprovalRequests")
$aprovalsContainer = $Context.BindToObject($approvalsPath)

# Get all pending approval requests.
$requests = $aprovalsContainer.GetApprovalRequests("ADM_APPROVALSTATE_PENDING")

$newApprover = $Context.TargetObject

foreach ($requestID in $requests)
{
    # Bind to the approval request.
    $guid = [Guid]$requestID
    $request = $Context.BindToObject("Adaxes://<GUID=$guid>")

    # Check whether request has expired.
    $requestExpDate = $request.CreationDate.AddDays($requestExpirationDays)
    
    if ([System.DateTime]::Now -lt $requestExpDate)
    {
        continue
    }
    
    # Get information about possible approvers.
    $approversInfo = $request.GetApproversInfo()
    if ($approversInfo.IsApproverEx($newApprover, $request.Requestor, $request.TargetObject))
    {
        continue
    }
    
    # Add user as a possible approver.
    $approverTrustees = $approversInfo.ApproverTrustees
    $approverTrustees.Add($newApprover)
    
    # Update the list of possible approvers.
    $request.SetApproversInfo($approversInfo)
    $request.SetInfo()
    
    # Build email notification.
    $operation = $request.DescriptionOfOperationToApprove
    $creationDate = $request.CreationDate
    $message = [System.String]::Format($messageTemplate, @($operation, $creationDate))
    
    # Send notification to the new approver.
    $Context.SendMail("%mail%", $subject, $message, $NULL)
}

Comments 0

You must be signed in to comment.

    Got questions?

    Support Questions & Answers

    We use cookies to improve your experience.
    By your continued use of this site you accept such use.
    For more details please see our privacy policy and cookies policy.