
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.
DIFFICULTY LEVEL: INTERMEDIATE
Assumes good knowledge and/or familiarity of Orchestration, Workflows, and Scripting in ServiceNow. Assumes having taken the class SSNF and has good intermediate level of knowledge and/or familiarity with Scripting in ServiceNow.
If you have ever tried to use the Switch Activity you will notice that it does not take a scratchpad variable (sic. workflow.scratchpad). This shortcoming can be overcome by using a little-known feature of the If Activity: the Advanced checkbox. When checked this displays the Script field where you can create code using a JavaScript switch or if/then/else if/else coding structure. Another Activity function that most people are unaware of is the Activity Context menu. For the If Activity this allows for the addition of more conditions. These two features will be used in this example to give us the capability of programmable Switch Activity.
In this example we will be exploring the Advanced functionality available with the out-of-the-box (OOB) If Activity.
NOTE: For this example I will be using an if/then/else if/else code structure, but I could have just as easily used a switch (see: Community Code Snippets - Javascript Switch Statement).
Lab 1.1: Using an Advanced If Activity
- In your ServiceNow instance Navigate to Workflow > Workflow Editor to bring up the Workflow Editor form.
- Click the "+" to create a new workflow. This will display the New Workflow form. Fill in the form with the following:
- Name: Playing With the If Activity
- Table: Global
- Description: Toying with various methods to implement the If Activity.
- Click the Submit button to create the new workflow.
3. Navigate to Core Activities > Utilities and drag out a Run Script activity
a. Name: Initialize
b. Script:
// This variable will determine which path the If Activity will take
workflow.scratchpad.trigger = 'red';
//workflow.scratchpad.trigger = 'blue';
//workflow.scratchpad.trigger = 'green';
//workflow.scratchpad.trigger = 'orange'; // fires the none branch
// This line will cause two paths to fire simultaneously
//workflow.scratchpad.trigger = 'red | green';
c. Click the Submit button to save your work.
NOTE: The commented out code represents the various test conditions we will be applying to our finished workflow.
4. Navigate to Core Activities Tab > Conditions and drag out an If activity.
a. Name: Validate Trigger
b. Advanced: Checked
c. Script (replace the template code):
workflow.scratchpad.result = ifScript();
function ifScript() {
var result = {
red: false,
blue: false,
green: false,
none: false
};
if (JSUtil.notNil(workflow.scratchpad.trigger)) {
if (workflow.scratchpad.trigger.indexOf('red') > -1) {
result.red = true;
}
if (workflow.scratchpad.trigger.indexOf('blue') > -1) {
result.blue = true;
}
if (workflow.scratchpad.trigger.indexOf('green') > -1) {
result.green = true;
}
}
if (!(result.red || result.blue || result.green)) {
result.none = true;
}
gs.info('---> [{0}]: {1}, {2}, {3}, {4}',
['Playing With the If Activity.Validate Trigger',
result.red,
result.blue,
result.green,
result.none]
);
return result;
}
d. Click the Submit button to save your work.
5. From the Workflow double-click on the If Activity "Yes" Condition. This will display the Condition form. Change the condition to the following:
- Name: is Red
- Skip During Generate: checked
- Condition: workflow.scratchpad.result.red == true
- Click the Update button to save your work.
6. From the Workflow double-click on the If Activity "No" Condition. This will display the Condition form. Change the condition to the following:
- Name: is Blue
- Skip During Generate: checked
- Condition: workflow.scratchpad.result.blue == true
- Click the Update button to save your work.
7. Right click on the If activity and choose Add Condition. This will display a blank Condition form. Fill in the form with the following:
- Name: is Green
- Skip During Generate: checked
- Condition: workflow.scratchpad.result.green == true
- Click the Update button to save your work.
8. Right click on the If activity again and choose Add Condition. This will display a blank Condition form. Fill in the form with the following:
- Name: is None
- Skip During Generate: checked
- Condition: workflow.scratchpad.result.none == true
- Click the Update button to save your work.
9. For now drag and connect all four conditions to the End Activity.
Done! We are now ready to test the new If activity!
Lab 1.2 — Testing the Workflow
1. Click the Play button in the upper right of the Workflow editor screen to run our If Activity workflow.
Note: Since we are manipulating the If activity with a variable in the Initialize Run Script activity the first branch of the If activity (Red) will trigger. The resultant context should look like this:
4. Now edit the Run Script "Initialize" activity
- Comment out the "red" line.
- Uncomment the "blue" line.
- Click the submit button to save your work.
- Click the Run Workflow button and observe the "blue" branch is triggered.
5. Edit the Run Script "Initialize" activity
- Comment out the "blue" line.
- Uncomment the "orange" line.
- Click the submit button to save your work.
- Click the Run Workflow button and observe the "none" branch is triggered.
6. Edit the Run Script "Initialize" activity
- Make sure the "green" line is commented out, and comment out the "Orange" line.
- Uncomment the "red | green" line.
- Click the submit button to save your work.
- Click the Run Workflow button and observe both the "red" and the "green" branches are triggered. This will demonstrate the firing of two branches simultaneously.
Note that the workflow was cancelled automatically. That is because we have parallel paths that are not yet merged, and ServiceNow does not know what to do with the two paths. More in a future article on Workflows and parallel streams.
Now we are done testing. That is all there is to it! Cool, huh?!
BTW, a large number of OOB activities have scripting capability which allows for flexibility to adapt to a variety of needs.
Enjoy!
Steven Bell.
If you find this article helps you, don't forget to log in and mark it as "Helpful"!
Originally published on: 10-18-2015 02:57 PM
I updated the code, fixed broken links, and brought the article into alignment with my new formatting standard.
- 6,928 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.