
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
In this final Workflow Control Pattern article we will be using what we learned with Variable and Property control and building a hybrid that uses both. This has a bit of twist to it, and thus the reason for this particular pattern. BTW, this pattern is the one I use the most when dealing with Service Catalog items.
The other articles in this series:
- Pragmatic Patterns: Workflows - Using Data to Adjust the Flow
- Mini-Lab: Workflow Control Via Variable Input (Intermediate Skill Level)
- Mini-Lab: Workflow Control Via Property Input (Advanced Skill Level)
Prerequisites:
- Intermediate to Advanced experience with ServiceNow will smooth over places in the labs where I don't spell out exactly what should be done. Consider this an Advanced Skill Level article.
- You are going to want to work through the Property Input article (#3) first. We will be expanding on the Service Catalog and Workflow we constructed there.
- Also, if you have not done so, you might want to run through my other lab on how to do advanced work with the If Activity; as we will be using the techniques describe there in this series of labs:
Mini-Lab: Workflows - Playing With the If Activity
Requirements
- Create a Service Catalog Item / Workflow combination where properties changed by an application owner will affect what occurs in the workflow.
- Place this Service catalog Item under the Can We Help You? Service Catalog Category.
- The name of the Service Catalog Item will be: Workflow Control via Property Input
- The Service Catalog item will contain a single variable: State.
- State can have four choices: New, Active, Open, and Closed. The underlying values of these will be 0,1,2,3 respectively.
- Only the Order Now button will appear on the Service Catalog Item.
- There will be a new application named: Workflow Patterns
- There will be a section under Workflow Patterns labeled: Administration.
- In the Administration section there will be a module that will contain a properties page named: Properties.
- There will be three properties:
- Property 1 will be labeled: React on Active?, and will be a checkbox. Default will be checked.
- Property 2 will be labeled: React on Open?, and will be a checkbox. Default will be checked.
- Property 3 will be labeled: Possible Choices (New, Closed, Monkeys)
- The workflow will print a message in the log that indicates a particular state was chosen.
- The workflow will check to see if the React on Active is checked. If it is it will place a log statement saying so in the System Log.
- The workflow will check to see if the React on Open is checked. If it is it will place a log statement saying so in the System Log.
- The workflow will check to see if the New and/or Closed are listed. If either is it will place a log statement saying so in the System Log.
- If nothing is chosen or an unknown value is present then an unknown message will be printed in the log.
- If Monkeys is listed in the Possible Choices field then the Active, Closed, and Unknown log messages will change to reflect that Monkeys was chosen.
Design
1. Create a Service Catalog Item (Req #1)
- Name: Workflow Control via Property Input (Req #1,3)
- Category: How Can We Help You? (Req #2)
- Deactivate every shopping cart feature except Order Now. (Req #6)
2. Catalog Item Variables: (Req #4,5)
- Name: State
- Choice List
- Choices: New, Active, Open, Closed
- Values will be the strings: 0, 1, 2, 3
- Choices will be entered into the Choices table
3. Create a new Application (Req #7,8)
- Name: Workflow Patterns
- Create a section in the new application labeled: Administration.
4. Create three System Properties (Req #10)
a. Name: workflow.react_on_active
- Description: React on Active?
- Type: true/false
- Associated to Workflow Patterns -> Properties page
b. Name: workflow.react_on_active
- Description: React on Active?
- Type: true/false
- Associated to Workflow Patterns -> Properties page
c. Name: workflow:possible_choices
- Possible Choices (New,Closed)
- Type: String
- Associated to Workflow Patterns -> Properties page
5. Create a new System Property Category
- Name: Workflow Patterns
- Title: Workflow Patterns
- Associate this to the two system properties in Design#4.
6. Create a new Module under Workflow Patterns (Req #9)
- Name: Properties
- Type: Properties page.
- Link: Workflow Patterns System Property Category.
7. Workflow design will be as follows: (Req #1, 11,12,13,14,15,16)
Development
Our requirements and design are finished. Let us now create the solution!
We will be building off of our solution from the previous article. So, same properties; we will be adding one new one. Same properties page, and application. Same Service Catalog. However, we will be copying our previous workflow and expanding on it.
Lab 1.1: Adding Possible Choices to the Properties Page
1. In the navigation filter type sys_properties.list and press enter. This will open up the System Properties list view.
2. Click on the New button to open the New properties form.
3. Fill in the form with the following:
- Name: workflow.possible_choices
- Description (this will act as the question in the properties page): Possible Choices (New, Closed):
- Type: String
- Value: New, Monkeys
- Right-Click on the form header to display the form context menu, and choose Save to save your work.
- Scroll to the bottom of the form to view the System Property Category list view. Note that it is now empty.
- Click on the Edit button. This will display a Select List of System Property Categories.
- Filter for Workflow Patterns, and select this category.
- Click on Save to save your work. This will display the workflow.possible_choices property form. Scroll to the bottom to verify that the property is now associated to the Workflow Patterns System Property Category.
- Click on the Workflow Patterns link in the System Property Categories related list. This will display the Workflow Patterns System Property Categories form.
- Scroll to the bottom of the form. You will see the associated System Properties list view. Note the ordering.
- Make the workflow.possible_choices order to be 300.
- Click on the Update button to make sure you save your work. To make sure you have saved your work. This will return you to the workflow.possible_choices form.
- Click on the Update button to make sure to save your work. Just to make sure your work is saved. This will return you to the System Properties list view.
Lab 1.2: Creating the Workflow
1. Navigate to Workflow -> Workflow Editor. A new tab will open in your browser and the Workflow Editor will be displayed.
2. From the Workflows tab scroll down until you find the Workflow Control Via Property Input, which you created in the previous article. Open the workflow.
3. Click on the "hamburger" (Workflow Form Context) button in the upper left to display the Context menu.
4. Click on the Copy menu item. This will display the Workflow Name form
5. Fill out the form with the following:
- Name: Workflow Control Via Variable Examination
- Click on the OK Button to create the new workflow.
6. Right-Click on the Check Properties If Activity header to display the Activity Context menu, and choose Add Condition (be patient, for whatever reason this can take several seconds to display!). The new Condition Properties form will appear.
7. Fill out the form with the following:
- Name: isOpen
- Condition Type: standard
- Skip during generate: checked
- Condition: workflow.scratchpad.result.open == true
- Click on the Submit button to save your work.
8. Repeat the procedure. Fill out the form with the following:
- Name: isClosed
- Condition Type: standard
- Skip during generate: checked
- Condition: workflow.scratchpad.result.closed == true
- Click on the Submit button to save your work.
9. Open the Check Properties If Activity
a. Name: Check Properties (just making sure)
b. Change the Script to be the following:
var identifier = context.name + '.' + activity.name;
workflow.scratchpad.result = ifScript();
function ifScript() {
var result = {new1:false, active:false, open:false, closed:false, monkeys:false, unknown:false};
result.new1 = (workflow.variables.state == 0 && workflow.scratchpad.new1);
result.active = (workflow.variables.state == 1 && workflow.scratchpad.active);
result.open = (workflow.variables.state == 2 && workflow.scratchpad.open);
result.closed = (workflow.variables.state == 3 && workflow.scratchpad.closed);
result.monkeys = workflow.scratchpad.monkeys;
// Debug
//gs.info('---> [WF:{0}] {1} - {2} - {3} - {4}', identifier, result.new1, result.active, result.open, result.monkeys);
// make sure you have a catch all
result.unknown = (
!(result.new1
|| result.active
|| result.open
|| result.closed)
);
return result;
}
c. Click Save to save your work.
10. Your If Activity should look like this:
11. Open the Initialize Run Script Activity.
- Replace the Script with the following:
var identifier = context.name + '.' + activity.name;
workflow.scratchpad.active = gs.getProperty('workflow.react_on_active') == 'true';
workflow.scratchpad.open = gs.getProperty('workflow.react_on_open') == 'true';
// separating these out into individual ifs allows for parallel execution in the
// workflow. Be careful with this. It could introduce problems if you don't do
// it correctly.
workflow.scratchpad.possibleChoices = gs.getProperty('workflow.possible_choices').toLowerCase();
workflow.scratchpad.new1 = (workflow.scratchpad.possibleChoices.indexOf('new') > -1);
workflow.scratchpad.closed = (workflow.scratchpad.possibleChoices.indexOf('closed') > -1);
workflow.scratchpad.monkeys = (workflow.scratchpad.possibleChoices.indexOf('monkeys') > -1);
// Debug
//gs.info('---> [wf:{0}] {1} - {2} - {3}', identifier, workflow.scratchpad.new1, workflow.scratchpad.closed, workflow.scratchpad.monkeys);
b. Click on the Update button to save your work.
12. From the Core tab on the editor navigate to Utilities and drag a Run Script Activity onto the form. The Activity Properties form will appear.
13. Fill out the form with the following:
a. Name: Log Open
b. Script:
var identifier = context.name + '.' + activity.name;
gs.info('---> [WF:{0}] Open triggered!', identifier);
c. Click the Submit button to save your work.
14. From the Core tab on the editor navigate to Utilities and drag a Run Script Activity onto the form. The Activity Properties form will appear.
15. Fill out the form with the following (Req #16):
a. Name: Log Closed
b. Script:
var identifier = context.name + '.' + activity.name;
if (workflow.scratchpad.result.monkeys) {
gs.warn('---> [WF:{0}] There were monkeys present at closing!', identifier);
}
else {
gs.info('---> [WF:{0}] Closed triggered!', identifier);
}
c. Click the Submit button to save your work.
16. Open the Log Active Run Script and change the Script to the following (Req #16):
var identifier = context.name + '.' + activity.name;
if (workflow.scratchpad.result.monkeys) {
gs.warn('---> [WF:{0}] Monkeys were present when Active was triggered!', identifier);
}
else {
gs.info('---> [WF:{0}] Active triggered!', identifier);
}
17. Open the Log Failure Run Script and change the Script to the following (Req #16):
var identifier = context.name + '.' + activity.name;
if (workflow.scratchpad.result.monkeys) {
gs.error('---> [WF:{0}] Something bad happened probably due to monkeys!', identifier);
}
else {
gs.info('---> [WF:{0}] Nothing triggered!', identifier);
}
18. Wire up your workflow to look like the following diagram:
At this point you may want to Publish your workflow to save it down. However, that is not really necessary as you will be the only one testing it.
Lab 1.3: Building the Service Catalog Item
Now we are ready to create the user interface (UI).
1. Navigate to Service Catalog -> Catalog Definitions -> Maintain Items. The Catalog Items list view will be displayed.
2. Click the New button. The New Catalog Item form will be displayed.
3. Fill out the form with the following:
- Name: Workflow Control Via Variable Examination
- Active: checked
- Availability: Desktop Only
- Catalogs: Service Catalog
- Category: Can We Help You?
- Workflow: Workflow Control Via Variable Examination
- Short Description: Workflow Control Via Variable Examination
- Use Cart Layout: not checked
- Omit price in cart: checked
- No quantity: checked
- No proceed checkout: checked
- No cart: checked
- Right-click on the form header to bring up the context menu.
- Choose Save to save your work and remain on the form.
4. Scroll to the bottom of the Item form. You will see the tabs for the related lists.
5. Choose Variables, and click the New button. The New Variable Form will be displayed.
6. Fill out the form with the following:
- Type: Select Box
- Click on the Question tab.
- Question: State
- Name: state
- Click on the Type Specifications tab.
- Include None: checked
- Right-click at the top of the Variable form to display the context menu.
- Choose Save to save your work and remain on the form.
7. Scroll to the bottom of the Variable form. You will see the Question Choices related list displayed.
8. Click on the New button. The New Question Choice form will be displayed.
9. Fill in the form with the following:
- Text: New
- Value: 0
10. Right-click on the Question Choice form header to bring up the context menu.
11. Click on Save to save your work and remain on the form.
12. Change the form to the following:
- Name: Active
- Value: 1
13. Right-Click on the Question Choices form header and choose Insert and Stay. This will write down a new record with the new values, and keep you in the form.
14. Change the form to the following:
- Name: Open
- Value: 2
15. Right-Click on the Question Choices form header and choose Insert and Stay. This will write down a new record with the new values, and keep you in the form.
16. Change the form to the following:
- Name: Closed
- Value: 3
17. Right-Click on the Question Choices form header and choose Insert. This will write down a third choice record, and will then return you to the Item form.
18. Set the order of your choices from the list view. The Question Choices list view should now look something like this:
Unit Test
There are so many options with this design that unit testing has to be approached differently than in the previous two articles. So we will go through the exercise of listing out the various tests that need to be created, then creating a couple to demonstrate that things are actually working.
Unit Tests that need creating:
- Check that new Possible Choices property is available in the Properties page.
- Test if New property checked and New picked on Service Catalog that is New path is activated in the workflow.
- Test if Active property checked and Active picked on the Service Catalog that is Active path is activated in the workflow
- Test if Open listed in Possible Choices property and Open picked in the Service Catalog that is Open path is active in the workflow.
- Test if Closed listed in Possible Choices property and Closed picked in the Service Catalog that is Closed path is active in the workflow.
- Test if Closed and Monkeys listed in Possible Choices property and Closed picked in the Service Catalog that is Closed path is active and the "Monkeys" log message is placed in the System Log.
- Test if Monkeys and Hello listed in Possible Choices property and any value is chosen in the Service Catalog that is Unknown path is active and "Monkeys" log message is placed in the System Log.
- ...And…well, you get the idea. You need to be thorough when it comes to unit testing!
So let's test a couple of these to see if things really do work!
1. Navigate to Workflow Patterns -> Administration -> Properties. The Workflow Properties page will be displayed.
2. Validate that the three properties are present. Change the values to be the following:
- Active is checked
- Open is unchecked
- Possible Choices has the following: New, Monkeys.
- Click the Save button to save your changes.
3. Navigate to Self-Service -> Service Catalog. The Service Catalog home page will be displayed.
4. Click on the Can We Help You? Service Catalog item. The list of Help Service Catalog items will be displayed.
5. Scroll down until you find the Service Catalog Item labeled: Workflow Control Via Variable Examination, and click that link. The Workflow Control Via Variable Examination page will be displayed and should look something like this:
6. Choose the State value of Active, and click the Order Now button. The Order Status form will be displayed.
7. Click on the RITM number. The Requested Item form will be displayed.
8. From the Requested Item Form scroll down to related links.
9. Click on the Show Workflow link. This will display the current workflow.
10. The workflow should show that the Active path was executed.
11. Close the workflow browser tab.
12. Navigate to Self-Service -> Service Catalog. The Service Catalog home page will be displayed.
13. Click on the Can We Help You? Service Catalog item. The list of Help Service Catalog items will be displayed.
14. Scroll back down until you find the Service Catalog Item labeled: Workflow Control Via Variable Examination, and click that link. The Workflow Control Via Variable Examination page will be displayed.
15. Choose the State value of Closed, and click the Order Now button. The Order Status form will be displayed.
16. Click on the RITM number. The Requested Item form will be displayed.
17. From the Requested Item Form scroll down to related links.
18. Click on the Show Workflow link. This will display the current workflow.
19. The workflow should show that the Unknown path was executed. This is because Closed was not listed in the Possible Choices property and therefore the default fired.
20. Close the workflow browser tab.
21. Navigate to System Logs -> System Log -> All. The System Log list view will be displayed.
22. Filter for Message starts with "---> [". This will display the list of messages written by the workflow.
23. Note that the first entry should be for the Active branch of the workflow firing. The second should be for the Unknown branch of the workflow firing. Because we had Monkeys listed in the Possible Choices property we also were able to trigger the code that checked for Monkeys.
24. You should go ahead and now test the other possibilities. Ahhh, make-work, gotta love it!
The demonstration of this pattern was to show that it is possible to use a variety of variable and property combinations to control any workflow. This flexibility allows for a lot of complexity.
That concludes the third data controlled workflow pattern example.
If you have read my previous three articles on workflow patterns you are probably getting pretty much tired of workflows! Hang in there! This is the last one…um, for a bit. I actually have three or four more planned dealing with Orchestration Framework workflows!
Steven Bell
If you find this article helps you, don't forget to log in and "like" it!
Also, if you are not already, I would like to encourage you to become a member of our blog!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.