Script Repository


Notify users on pending Approval Requests initiated by them

September 17, 2015
1328

This script can be used in Business Rules, Custom Commands or Scheduled Tasks to notify users on pending Approval Requests they initiated. For example, you can create a Custom Command that will notify a user on which it is executed.

Note: Pending Approval Requests are requests that have been neither approved, nor denied, nor canceled.
To add the script to a Custom Command or Scheduled Task, use the Run a program or PowerShell script action.

Parameters:

  • $to - specifies email address of the recipient of the notification;
  • $subject - specifies the email message subject;
  • $reportHeader - specifies the email message header;
  • $reportFooter - specifies the email message footer.
You can use value references (e.g. %mail%) to specify the recipient. When the script is executed, the value references will be replaced with property values of the user on whom it is executed.
Edit Remove
PowerShell
$to = "%mail%" # TODO: modify me
$subject = "Unapproved tasks that you initiated" # TODO: modify me
$reportHeader = @"
<b>List of unapproved tasks that you initiated</b><br/><br/>
<table border="1">
    <tr>
        <th>Request</th>
        <th>Approvers</th>
    </tr>
"@ # TODO: modify me
$reportFooter = "<hr /><p><i>Please do not reply to this e-mail, it has been sent to you for notification purposes only.</i></p>" # TODO: modify me

if ([System.String]::IsNullOrEmpty($to))
{
    # The user doesn't have an email address, exit
    return
}

# Get all requests initiated by the user
$requests = $Context.TargetObject.GetRequestsForApproval("ADM_APPROVALSTATE_PENDING")
if ($requests.Length -eq 0)
{
    return # No pending requests
}

# Get the Web Interface address registered in Adaxes service
$webInterfaceAddress = "%adm-WebInterfaceUrl%"
if ([System.String]::IsNullOrEmpty($webInterfaceAddress))
{
    $Context.LogMessage("Default web interface address not set for Adaxes service. For details, see http://www.adaxes.com/help/?HowDoI.ManageService.RegisterWebInterface.html", "Warning")
}

foreach ($requestGuidBytes in $requests)
{
    # Bind to the Approval Request
    $requestGuid = [Guid]$requestGuidBytes
    $request = $Context.BindToObject("Adaxes://<Guid=$requestGuid>")
    
    # Get request info
    $approversInfo = $request.GetApproversInfo()
    $approvers = $approversInfo.GetApproversEx($request.Requestor, $request.TargetObject)
    $requestOperation = $request.DescriptionOfOperationToApprove
    
    # Add request to e-mail notification
    $reportHeader += "<tr><td><a href='$webInterfaceAddress`ViewObject.aspx?guid=$requestGuid'>$requestOperation</a></td><td>"

    # Add information on approvers
    foreach ($approver in $approvers)
    {
        $approverGuid = (New-Object "System.Guid" (, $approver.Get("ObjectGuid"))).ToString()
        $approverName = $approver.Get("name")
        $reportHeader += "<a href='$webInterfaceAddress`ViewObject.aspx?guid=$approverGuid'>$approverName</a><br/>"
    }

    $reportHeader += "</td></tr>"
}

# Build report
$report = $reportHeader + "</table>" + $reportFooter

# Send mail
$Context.SendMail($to, $subject, $NULL, $report)


Comments ( 0 )
No results found.
Leave a comment