
- 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.
One question I get a lot when consulting on Workflows is: How do you do parallel processing, or parallel threads?
Simply put: I use a Branch Activity, and a Join Activity. I will give a how-to example here with a couple of pitfalls to watch out for.
Lab 1.1: Workflows: Branch and Join Example
1. Navigate to Workflow > Workflow Editor. This will open the workflow editor
a. In the Workflow Navigation column click on the "+" symbol to create a new workflow
2. Create a new workflow
a. Name: Branch and Join Example
b. Table: Global
c. Description: Demonstrate how to do parallel processing with a workflow
d. Take all other defaults
e. Click the Submit button to create the workflow
3. Navigate to Core > Utilities
a. Drag out a Branch Activity on to the form.
i. Name: Run Parallel Processes
b. Drag out a Join Activity on to the form.
i. Name: Pull Together Processes
c. Drag out a Run Script Activity on to the form.
i. Name: Set the Check
ii. Script:
workflow.scratchpad.check = 'true';
4. Navigate to Core > Timers
a. Drag out a Timer Activity.
i. Name: Timer For Branch 1
ii. Timer Based On: A user specified duration
iii. Duration: 5 seconds
b. Drag out a Timer Activity.
i. Name: Timer For Branch 2
ii. Timer Based On: A user specified duration
iii. Duration: 10 seconds
c. Drag out a Timer Activity.
i. Name: Timer For Branch 3
ii. Timer Based On: A user specified duration
iii. Duration: 15 seconds
5. Navigate to Core > Utilities
a. Drag out a Run Script Activity
i. Name: Set the Check
ii. Script:
workflow.scratchpad.check = 'true';
6. Navigate to Core > Conditions
a. Drag out an If Activity.
i. Name: Something Happened
ii. Advanced: checked
iii. Script:
answer = ifScript();
var check = 'no';
function ifScript() {
if (workflow.scratchpad.check === 'true') {
check = 'yes';
}
return check;
}
7. Wire everything up to look like the following:
8. Now click the Run Workflow button on the top right. The Start Workflow form will appear.
9. Click the Start button to run the workflow.
10. Observe the workflow. The timers all fire at the same time, but don't allow flow to continue until each has completed. Then all end up at the Join until the last one is done, and then get released to the end.
However! If you look closely at the Join you will see that the Incomplete branch fired! What's up with that?!
Well there is a bug in our workflow. The Branch Activity has three branches. There has to be the same number of branches flowing into the Join. So look carefully at the If Activity and you will see there are two there, plus one for each of the other timers! So four! That causes the Join Activity to fire the Incomplete.
11. So let us adjust the workflow slightly by adding a Script Activity after the If Activity, but prior to the Join activity. It's purpose will be to bring the two If Activity branches back together and then there will be only one flowing into the Join.
12. Navigate to Core > Utilities
a. Drag out a Run Script Activity on to the form.
i. Name: Write to Logs
ii. Script:
gs.info('---> [{0}] There was a problem!', ['WF: Branch and Join Example']);
Your Workflow should look something like this after rewiring:
13. Re-run the workflow again. Now the Join Activity fires the Complete branch. Whew!
NOTE: The number of branches coming out of the Branch Activity must equal the number of branches going into the Join Activity.
14. So, let's play a little game with it and see if it likes things changed up a bit.
15. Rewire your Workflow
a. The Timer 2 is connected to the Write To Logs activity.
b. The If Activity "Yes" branch is still wired to the Write To Logs activity.
c. The If Activity "No" branch is now wired directly to the Join.
16. Your workflow should now look like this (I highlighted the rewired Activities):
17. Re-run your workflow, and observe what happens.
18. Notice that there are still only three branches coming into the Join? That meets the criteria and the Complete branch fires. Even though only two were actually utilized during runtime.
That is it! You have now wired up your first Branch and Join Workflow. Even though this had only three branches you could do more. I recommend keeping it as simple as possible; as you can see, it can get difficult to tell what is happening pretty quick!
Enjoy!
Steven Bell.
If you find this article helps you, don't forget to log in and mark it as "Helpful"!
Originally published on: 09-20-2015 04:36 PM
I updated the code, fixed broken links, and brought the article into alignment with my new formatting standard.
- 3,753 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.