The scripts return true if there is at least one pending approval request for the target object created by the same initiator. The 1st script checks for the existence of any pending request while the 2nd script checks for requests to execute a specific custom command. Both scripts can be used in the If PowerShell script returns true condition in business rules, custom commands, and scheduled tasks.
Check for any requests
PowerShell
# Get GUIDs of initiator and target object.
$targetObjectGuid = $Context.TargetObject.Get("objectGuid")
$initiatorGuid = $Context.Initiator.UserAdsObject.Get("objectGuid")
# Build search criteria.
$criteria = New-AdmCriteria -Type "adm-ApprovalRequest" `
-Expression {adm-ApprovalState -eq 0 -and adm-TargetObjectGuid -eq $targetObjectGuid -and adm-ApprovalRequestorGuid -eq $initiatorGuid}
# Search approval requests.
$approvalsPath = $Context.GetWellKnownContainerPath("ApprovalRequests")
$searcher = $Context.BindToObject($approvalsPath)
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.Criteria = $criteria
$searcher.SizeLimit = 1
try
{
$searchResultIterator = $searcher.ExecuteSearch()
$searchResults = $searchResultIterator.FetchAll()
$Context.ConditionIsMet = $searchResults.Length -gt 0
}
finally
{
# Release resources.
if ($searchResultIterator){ $searchResultIterator.Dispose() }
}
Check for requests to execute a custom command
Parameter:
- $commandID - specifies the identifier of the custom command to search the approval requests for. For details on how to get the custom command indentifier, see Get custom command identifier.
PowerShell
$commandID = "{ac209b5d-042f-4165-b9f6-f73393fcba1c}" # TODO: modify me
# Get GUIDs of initiator and target object.
$targetObjectGuid = $Context.TargetObject.Get("objectGuid")
$initiatorGuid = $Context.Initiator.UserAdsObject.Get("objectGuid")
# Build search criteria.
$criteria = New-AdmCriteria -Type "adm-ApprovalRequest" `
-Expression {adm-ApprovalState -eq 0 -and adm-TargetObjectGuid -eq $targetObjectGuid -and adm-ApprovalRequestorGuid -eq $initiatorGuid}
# Search approval requests.
$approvalsPath = $Context.GetWellKnownContainerPath("ApprovalRequests")
$searcher = $Context.BindToObject($approvalsPath)
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.Criteria = $criteria
try
{
$searchResultIterator = $searcher.ExecuteSearch()
$searchResults = $searchResultIterator.FetchAll()
}
finally
{
# Release resources.
if ($searchResultIterator){ $searchResultIterator.Dispose() }
}
# Check each approval request if search results are not empty.
$Context.ConditionIsMet = $false
foreach ($result in $searchResults)
{
# Bind to the approval request.
$request = $Context.BindToObjectBySearchResult($result)
if ($request.ActionToApprove.CustomCommandId -eq $commandID)
{
$Context.ConditionIsMet = $true
return
}
}