Cancel pending activities in a failed Service Request

Travis Wright has a couple of years ago, written a blog on how to cancel pending activities in a failed Change Request: https://blogs.technet.com/b/servicemanager/archive/2011/05/03/cancelling-pending-activities-when-a-change-request-fails.aspx

I often get the request to make a similar solution for Service Requests and thought it was about time to document it. 

Open the Authoring console and create a new Management Pack.

Right-click on Workflows and select Create

Give the workflow a name:

 

Choose to trigger the workflow based on a condition occurring in the database:

And subscribe to Service Requests being updated where the criteria is Status BEFORE Not Equal to Failed and Status AFTER Equal to Failed.

 

 

 Then finish out the wizard.

That will drop you into the workflow designer. Drag a single PowerShell script activity from the
toolbox onto the workflow designer and give it a name:

 

 

Then click the button on the Script Body attribute in the Details pane and insert the Powershell script.

 

 

Here is the script so you can copy it if you want to:

import-module smlets
$InProgress = Get-SCSMEnumeration -Name ActivityStatusEnum.Ready

$Cancelled = Get-SCSMEnumeration -Name ActivityStatusEnum.Cancelled

$ServiceRequest = Get-SCSMObject -Class (Get-SCSMClass -Name System.WorkItem.ServiceRequest$) -Filter "Id -eq $ServiceRequestID"

$ChildActivities = (Get-SCSMRelationshipObject -BySource $ServiceRequest | ?{$_.RelationshipID -eq "2da498be-0485-b2b2-d520-6ebd1698e61b"})

$ChildActivities | %{$Activity = Get-SCSMObject -ID $_.TargetObject.Id; if($Activity.Status -eq $InProgress){$Activity | Set-SCSMObject -Property Status -Value $Cancelled}}

 

Then you need to bind the ID of the Service request that triggers the workflow to the $ServiceRequestID variable. To do this switch to the Script Properties tab. Enter ‘ServiceRequestID’
in the Name column (so that it exactly matches the name of the $ServiceRequestID variable in the PowerShell script) and then click the … button and choose the ID property (not ID (Internal)).

 

 

 

 

Now save the management pack. That will produce a .dll file that contains the workflow code.

To deploy the slution:

Import the MP into SCSM and Copy the .dll to C:\Program Files\Microsoft System Center 2012\Service Manager  directory on the SCSM management server where the workflows are running.

First, here’s an SR waiting for an approval:

  

Let’s play along and deny the request. Normally the SR would fail and the remaining activites will stay in Pending mode.

 

Now, with the new Workflow in place, the remaining activites are changed from Pending to Cancelled.

 

So like with the CR activities, you can actually restart the Review Activity and all the Manual activites
will go back to pending, awaiting the RA to be approved.

 

Attached is the MP and DLL I've created as I wrote this blog.

Cancel Activites.zip