Service Manager: Creating Workflows that Run when a Relationship is Created

The SCSM console allows you to quickly construct a workflow for an Incident or Change Request that triggers when a property is changed such as when a string or enumeration (list) property is updated.  But can a workflow be triggered when a relationship is created such as when an Incident is assigned to an Analyst?  Fortunately yes it can by editing the unsealed management pack that contains the workflow.

If you aren’t familiar with editing unsealed management packs (managementpackname.xml) then the easiest way to get started is to create a workflow using the Console or Authoring Tool to trigger when a property such as title is changed in the desired class.  In this example a workflow has been created using the console to apply a template when the Incident status changes from Active.   This will be changed to apply the template when the Affected User relationship is changed.  The workflow has been stored in Demo.Workflow.xml.

Using your XML editor tool of choice and locate the Subscription section:

<DataSources>
  <DataSource ID="DS" TypeID="SystemCenter!Microsoft.SystemCenter.CmdbInstanceSubscription.DataSourceModule">
    <Subscription>
      <InstanceSubscription Type="a604b942-4c7b-2fb2-28dc-61dc6f465c68">
        <UpdateInstance>
          <Criteria>
            <Expression>
              <And>
                <Expression>
                  <SimpleExpression>
                    <ValueExpression>
                      <Property State="Pre">$Context/Property[Type='CustomSystem_WorkItem_Incident_Library!System.WorkItem.Incident']/Status$</Property>
                    </ValueExpression>
                    <Operator>Equal</Operator>
                    <ValueExpression>
                      <Value>{5e2d3932-ca6d-1515-7310-6f58584df73e}</Value>
                    </ValueExpression>
                  </SimpleExpression>
                </Expression>
                <Expression>
                  <SimpleExpression>
                    <ValueExpression>
                      <Property State="Post">$Context/Property[Type='CustomSystem_WorkItem_Incident_Library!System.WorkItem.Incident']/Status$</Property>
                    </ValueExpression>
                    <Operator>NotEqual</Operator>
                    <ValueExpression>
                      <Value>{5e2d3932-ca6d-1515-7310-6f58584df73e}</Value>
                    </ValueExpression>
                  </SimpleExpression>
                </Expression>
              </And>
            </Expression>
          </Criteria>
        </UpdateInstance>
      </InstanceSubscription>
      <PollingIntervalInSeconds>100</PollingIntervalInSeconds>
      <BatchSize>50</BatchSize>
    </Subscription>
  </DataSource>
</DataSources>

 This instance subscription needs to be replaced with a relationship subscription:

 <RelationshipSubscription RelType="$MPElement[Name='System_WorkItem_Library!System.WorkItemAffectedUser']$" SourceType="$MPElement[Name='System_WorkItem_Library!System.WorkItem']$" TargetType="$MPElement[Name='System!System.User']$">
  <AddRelationship>
  </AddRelationship>
</RelationshipSubscription>

 The subscription is made up of three parts, the relationship type, the source, and the target.  If you don’t want the Affected User relationship, or even the Incident class, then you can find the properties for your relationship by using PowerShell, or viewing the Work Item classes using the Authoring Tool.

 One of the key parts is to ensure that you include the relevant reference for your relationship.

 Name='System_WorkItem_Library!System.WorkItemAffectedUser'

 We can see that in this example the relationship property ‘System.WorkItemAffectedUser’ is located in the ‘System_WorkItem_Library’, so a reference to this library must be included in the references section at the beginning of the Management Pack.

 <Reference Alias="System_WorkItem_Library">
  <ID>System.WorkItem.Library</ID>
  <Version>7.5.3079.0</Version>
  <PublicKeyToken>31bf3856ad364e35</PublicKeyToken>
</Reference>

 Note – ensure you use the correct versioning depending on whether you are working in Microsoft System Center 2012 R2 or Microsoft System Center 2016.

 If you are unfamiliar with the Work Item classes in Service Manager then the SourceType ‘$MPElement[Name=’System_WorkItem_Library!System.WorkItem’]$’ may be a little confusing as it doesn’t mention the Incident class.  This is because the relationship Affected User doesn’t actually exist in the Incident class.  It exists in the System.WorkItem class.  The Incident class inherits the relationship as do all Work Item classes such as Service Request.

 The same principle applies to the TargetType as it specifies System.User rather than the Active Directory user class for the same reason.

 The end result is the subscription should look like this:

 <DataSources>
   <DataSource ID="DS" TypeID="SystemCenter1!Microsoft.SystemCenter.CmdbInstanceSubscription.DataSourceModule">
     <Subscription>
       <RelationshipSubscription RelType="$MPElement[Name='System_WorkItem_Library!System.WorkItemAffectedUser']$" SourceType="$MPElement[Name='System_WorkItem_Library!System.WorkItem']$" TargetType="$MPElement[Name='System!System.User']$">
         <AddRelationship>
         </AddRelationship>
       </RelationshipSubscription>
       <PollingIntervalInSeconds>60</PollingIntervalInSeconds>
       <BatchSize>100</BatchSize>
     </Subscription>
   </DataSource>
 </DataSources>
Enterprise Mobility Workshops - 24th November 2015 - London | 9:00am – 3:00pm

Leave a Reply

Your email address will not be published. Required fields are marked *