
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
I'm often faced with providing customers solutions for complex requirements. Some of these requirements come at the end of long processes. For example, "in ourchange workflow, we want you to look up closure approves from these set of conditions…" I could put 10-20 lines of code in a workflow script activity, but then I'd have to go through 15-20 minutes of filling out a form, moving it through the change process, only to get to the closure to find out if my code worked or not. Read on for a better way.
By placing the main logic of the lookup process, in this case, in a script include one can "unit" test the code outside the change process and have confidence that it will work as expected when you do invest the 20 minutes to use the form.
Let's take the following simple script include as an example:
var SimpleUtil = Class.create();
SimpleUtil.prototype = {
initialize: function() {
this.debug = false;
},
/*
* addTwoNums - add two numbers together and return a result
*
* @param num1 - a number
* @param num2 - a number
* @return - the sum of num1 + num2
*
*/
addTwoNums : function(num1, num2) {
return num1 + num2;
},
/*
* _debug - send a string to the debug log
*
* @param str - string to output
* @return NA
*
*/
_debug : function(str) {
if (this.debug)
gs.print(str);
},
type: 'SimpleUtil'
}
Let's ensure that the function to add two numbers works before releasing it to the testers. To do this, use "System Definition> Scripts - Background". To see this module, you will need to first elevate your security privileges. Click on the gold lock next to your name on the title bar.
Next, check the security_admin checkbox on the pop up window and click OK.
You should now see the key "unlocked", indicating you have elevated privileges.
Finally, go to System Definition> Scripts - Background, or type in 'background' (no quotes) in the navbar search box to quickly locate the module, and click on it.
In the large text area presented, you can test specific components of your script include logic. In the case of the script above, I can use a simple test script like the following:
var su = new SimpleUtil();
var answer = su.addTwoNums(5, 3);
gs.print(answer);
Click the "Run Script" button to get a result:
*** Script: 8
If it did not work, quickly edit and save the script include then test again without the pain of going through the long change process again. Once it is working, add the lines of code to create a new object from the script include and call the same function in the workflow script activity with confidence that it works as you expect. In the future, someone may try to enter string information, or other 'invalid' data that cannot/should not be added. Updating and re-testing your changes is quite easy in this case without involving checking out and publishing the workflow, long unit testing cycles, etc. Scripts - Background makes testing very easy in such cases.
This is, of course, a very simple test, but you get the idea how you can create more complex tests to ensure your script gives you the expected results.
Another thing I like to use the Scripts - Background utility for is to trigger events, to send email notifications. The follow test script is an example of how to do this.
var incidentID = '46f67787a9fe198101e06dfcf3a78e99';
var inc = new GlideRecord('incident');
var incEvent = 'reset.password';
if (inc.get(incidentID)) {
gs.eventQueue(incEvent, inc, inc.number, gs.getUserDisplayName());
} else {
gs.print('Error: Cannot read record');
}
These are just some of the useful things you can do with Scripts - Background. I hope you find it as useful as I do.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.