Mini-Lab: Workflow Script Includes (with current.variables auto-parsing)
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 thing I have gravitated to, over the years as a Workflow/Runbook/Orchestration developer, is thinning out my scripted Workflow Activities. What this means is that to better maintain the code base in any given workflow it is a best practice to move the code into a Script Include library, if possible. I usually end up creating a Workflow Utils named library of functions. There are several reasons to do this:
- Reusable Code
- Ease of Maintenance
- Easily searchable
So, the best practices?
- By definition a library of functions is for future reusability. So that Best Practice is obvious.
- Ease-of-Maintenance flows from that by making the code centralized, and generic (a reduced and refactored code-base with comments!).
- Maintenance is also improved by allowing the developer to make modifications to the code without having to check out the workflow.
- Easily Searchable is one that most developers don't realize is missing from Workflow activities. There is no easy-to-find searchable code base. It is hidden and hard to get at! This last one is a biggy, and a Script Include Library will instantly solve it. BTW, it is findable in sys_variable_value.
The question that comes up is: So how do I implement Script Includes with workflows?
Easy. Just follow this simple lab to get started!
Lab 1.1: Workflow Script Include Example
Pre-requisite
Work through the following Mini-Lab article: Mini-Lab: Printing off the Workflow Scratchpad object as we will be using the workflow developed there.
Creating The Workflow Script Include
- Navigate to System Definition > Script Includes.
- Click the New button.
- Fill in the form with the following:
Name: ML_WorkflowUtils
Accessible from: All Application Scopes
Description:
Utilities to be used from Workflows/OrchestrationUsage example:var utils = new global.ML_WorkflowUtils();
utils.loadScratchpadObject(current, workflow.scratchpad);
Script:
var ML_WorkflowUtils = Class.create();
ML_WorkflowUtils.prototype = {
initialize: function(location) {
this.location = location;
},
// convert the current.variables object into the
// workflow scratchpad object.
// btw, this is the first step to making a workflow Service Catalog variable agnostic.
loadScratchpadObject : function(current, scratchpad) {
var message = 'current.variables Object: \n';
var variables = current.variables;
for (var item in variables) {
message += item + ': ' + variables[item] + '\n';
var name = item.replace('v_','');
scratchpad[name] = variables[item];
}
gs.info('---> [{1}] {0}', [message, this.location]);
},
// now loop through the workflow scratchpad object and write all the values
// to the System Log.
writeScratchpadObjectToLog : function(scratchpad) {
var message = 'Scratchpad Object: \n';
for (var item in scratchpad) {
message += item + ': ' + scratchpad[item] + '\n';
}
gs.info('---> [{1}] {0}', [message, this.location]);
},
type: 'ML_WorkflowUtils'
};
4. Save your Script Include. We are done with the library.
Calling The Workflow Script Include
1. Navigate to Workflow > Workflow Editor
2. Edit up the workflow: ML - Print Scratchpad Object (from the previous article)
3. Open up the Run Script Activity and change the code to the following:
var location = context.name + '.' + activity.name;
// The activity location is passed in at instantiation of the object
var utils = new ML_WorkflowUtils(location);
// load the scratchpad object
utils.loadScratchpadObject(current, workflow.scratchpad);
// print of the filled scratchpad object
utils.writeScratchpadObjectToLog(workflow.scratchpad);
Note: We have replaced the previous code with calls to the Workflow Utilities Script Include.
4. Click the Update button to save the activity.
NOTE: We have now pushed the code down to a Script Include library. "Thinned" out the code in the Activity so that it really does not require an maintenance in the workflow. In the process this has now achieved our goals of Maintainability, Reusability, and Searchability! Any future modifications to the code will be made to the Script Include and will not require editing the Workflow.
NOTE: Since workflow.scratchpad is a Javascript object it is passed by reference (pointer), and therefore any modifications made to the object in the Script Include are reflected immediately in the original object. Thus when we go to print out the workflow.scratchpad object we find that it is now populated.
Testing The Workflow Script Include
1. Navigate to Service Catalog > Catalog Definitions > Maintain Items
2. Search for and open the item: Scratchpad Variable Sender
3. Click on the Try It button. This will bring up the new Service Catalog Request form.
4. Fill out the form with any random text and click the Order Now button.
5. After the Order info form appears navigate to System Logs > System Log > All.
6. Search for '--->' in the messages column.
7. You should see something like this in the logs:
Look familiar? It should be exactly the same as in the previous article only you are executing code in the Script Include instead of the Workflow Activity.
Congratulations you now have created your very first Workflow Utility Script Include library! Keep adding functions to it and I am sure you will continue to find ways of improving your own code to make it more useful!
Enjoy!
Steven Bell.
If you find this article helps you, don't forget to log in and mark it as "Helpful"!
Originally published on: 07-30-2018 05:13 PM
I updated the code, fixed broken links, and brought the article into alignment with my new formatting standard.
Labels:
https://www.servicenow.com/community/developer-articles/mini-lab-workflow-script-includes-with-current-variables-auto/ta-p/2330339
