Let’s say you have a workflow that you want to behave differently based on the selection in a checkbox on a form. If it is checked you want to do one thing. If it is not checked you want it to do another thing. To do this you want to use the If/Else Windows Workflow Foundation activity provided out of the box in the SCSM Authoring Tool. It’s not exactly obvious how to do this though so I’ll walk you through it end to end.
- First, start by launching the Service Manager Authoring Tool
- Create a new MP by clicking the New button in the toolbar or File –> New
- I’m going to name my MP ‘Windows8Tablets’ in honor of the really cool stuff that was demo’d recently re: Windows 8.
- Right click on Classes in the Management Pack explorer and choose ‘Create Configuration Item Class…’
- Give the Class the Name of Windows8Tablet and click Create.
- At the top of the class editor change the Class name from Windows8Tablet to Windows 8 Tablet and delete the description.
- Select the Property_4 that is automatically provided and delete it by clicking the red ‘X’. Click Yes on the confirmation dialog and OK on the warning dialog. The warning dialog is telling you that your new class doesn’t have a key property.
- Click Create property… above the property list.
- Give the property a name of ID and click Create and then click OK again on the warning dialog.
- In the Details pane change Auto Increment to True, Key to True, and set the Default Value to {0}.
- Click Create property… again.
- This time name the property ‘ForExecutive’.
- In the Details pane, choose Bool from the Data Type drop down. Set the Default Value to False
- Right click on Forms and choose Create.
- In the Base class dialog choose the ‘Windows 8 Tablet’ class and click OK.
- Give the form a name like Windows8TabletForm and click Create.
- Drag a label onto the form from the Form Customization Toolbox.
- Change the Content property to ID: in the Details pane.
- Drag another label onto the form and place it just below the ID: label.
- Change the Binding Path property in the Details pane by clicking the … button and in the dialog that comes up choose the ID property (not the ID (Internal) property) and click OK.
- It should look like this:
- Drag a Check Box control from the toolbox onto the designer below the ID labels.
- Change the Content property to ‘User is Executive’.
- Change the Width property to 150.
- Change the Binding Path property by clicking the … button and choosing the ForExecutive property and clicking OK.
- Right click on Workflows and choose Create.
- Give the workflow a name like ‘NewWindows8TabletWorkflow’ and click Next.
- Select the ‘Run only when a database object meets specified conditions’ since we want this workflow to be triggered each time a new Windows 8 Tablet object is created.
- Click Next.
- Click Browse… and choose the Windows 8 Tablet class and click OK.
- Click Next.
- Click Create.
- Click Close.
- Drag a PowerShell script activity onto the designer. This is the first part that is non-obvious but is the KEY to this working. We need to get the properties of the object that triggered the workflow passed into the workflow as parameters. One way to do this is to drop a meaningless PowerShell script activity into the workflow and bind it to some of the properties of the object.
- Select the PowerShell script activity.
- In the Details pane click the … button next to the Script Parameters property
- In the Configure a Script Activity dialog click the New button to create a new script parameter.
- Name the parameter ForExecutive.
- Click the … button the same row to select the class property to bind to.
- In the dialog that comes up choose the ‘Use a Class Property’ radio button the left.
- Select the ForExecutive property from the list and click OK.
- Click the Script Body tab.
- In the script body tab enter the following script which will take the passed in value and simply write it to a file so we can see it: $ForExecutive > C:\Input.txt
- Click OK.
- Drag an If/Else activity onto the designer from the toolbox.
- Drag a PowerShell script activity into each side of the If/Else activity. These two activities will just simply simulate what you could do differently in each branch of the workflow.
- It should look like this:
- Now select the ifElseBranchActivity1 (the one on the left). Over in the Details pane click the textbox next to the Condition property and choose Declarative Rule Condition from the drop down menu.
- Expand the plus sign to the left of the Condition property in the Details pane.
- Change the ConditionName property to IsTrue (this is just an arbitrary name, but I helps succinctly describe what the condition is so you don’t have to look inside at the formula).
- Click the … button in the Expression property to bring up the Rule Condition Editor. This is the second part that is non-obvious.
- Type this and then a period. That will bring up a list of options. Choose ‘windowsPowerShellScript1_ForExecutive. Type another period and it will bring up more options. Choose ToString then type an open and close paranthesis. Then type a space, then two equals signed and then a space and then '”true”.
- It should look like this:
- Now, select the PowerShell script activity on the left and click the Script Body … button in the details pane.
- Enter the following script: “This user is an executive” > C:\Result.txt
- It should look like this:
- Click OK
- Now repeat steps 48-57 for the right side except this time in the rule condition editor you will set the condition == “false” and you will set the script to say ‘This user is not an executive”.
- Save the MP using the toolbar button.
- Copy the .dll file from the folder where you created your MP to C:\Program Files\Microsoft System Center\Service Manager 2010 on the management server where your workflows are running.
- Import the MP.
- Go to the Configuration Items workspace in the console.
- Right click on Configuration Items and choose Create Folder.
- Give the Folder a name like Windows 8 Tablets and make sure the Windows8Tablets management pack is selected and click OK.
- Right click on the new Windows 8 Tablets folder that is created and choose Create View.
- Give the view a name like Windows 8 Tablets and then choose the Windows 8 Tablet class in the criteria section and click OK.
- In the new Windows 8 Tablets view click the task ‘Create Windows 8 Tablet’.
- The custom form should come up. Check the User Is an Executive checkbox and click OK.
- Open Windows Explorer on the management server where the workflows are running and navigate to the C: folder and wait for the .txt files to be created.
- Within less than a minute a new text file called C:\Input.txt will be created. It should have the value ‘true’ inside of it because the checkbox was checked.
- The file C:\Result.txt will have the value ‘This user is an executive’ in it.
- Now create another Windows 8 Tablet from the UI but this time make sure the checkbox is cleared and see what happens in the .txt files.