
- 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 having taken the class SSNF and has good intermediate level knowledge and/or familiarity of Scripting in ServiceNow. Know how to upload an XML file to given table in an instance.
The purpose of this lab is to demonstrate how to create then call an event that triggers a Script Action that calls a Script Include! Additionally, I wanted to show how you could simulate a Business Rule call in Scripts - Background.
CAVEAT: I did not go into extreme how-to detail, and there is a certain expectation of proficiency with the ServiceNow platform to successfully complete this lab.
1. Install the UsefulUtilities Script Include
This will provide the currentFactory function used later in this article.
NOTE: You can find out how to in this article: Community Code Snippets: Useful Utilities Library
2. Create a Script Include to do something!
- Navigate to System Definition -> Script Includes. You may still be there.
- Click New. The a blank Script Include form will be displayed.
- Fill in the following:
Name: MyIncidentUtils
Accessable From: All application scopes
Client callable: unchecked
Active: checked
Description:
Utilities Library for Incidents
addTenDaysToOpenedAt - method to add 10 days to the opened_at Date field on the Incident form
Protection Policy: -- None --
Script:
var MyIncidentUtils = Class.create();
MyIncidentUtils.prototype = {
initialize: function() {},
addTenDaysToOpenedAt : function(incident) {
var location = 'SI:' + this.type + '.AddTenDaysToOpenedAt';
try {
gs.info('---> [{2}] Incident number: {0} Opened At: {1}',
[incident.number, incident.opened_at, location]);
var openedAt = new GlideDateTime(incident.opened_at.getDisplayValue());
var opened_at = openedAt.getDate();
opened_at.addDays(10);
incident.opened_at = opened_at;
incident.update();
gs.info('---> [{1}] Date after: {0}', [incident.opened_at.getDisplayValue(), location]);
}
catch(err) {
gs.error('[{1}] ERROR:{0}', [err, location]);
}
},
type: 'MyIncidentUtils'
};
d. Click the Submit button.
NOTE: After filling in the name field then tabbing off the form a template with your Script Include name will be auto-filled into the Script field.
NOTE: Always check your work, and click the syntax to make sure there were no problems.
Your Script Include should look like this:
3. Create a new event - Register the Event name
- Navigate to System Policy -> Events -> Registry. The Event Registry list view will be displayed.
- Click on the New button. The Event Registration form will be displayed.
- Fill the following in:
Event name: incident.opened_at.add_ten_days
Table: Global (this would be Incident if you were firing the event from an Incident Business Rule)
Fired by: Scripts - Background
Description: Add ten days to the incident
Your event form should look like this:
d. Click the Submit button to save.
4. Create a Script Action to fire the Script Include
- Navigate to System Policy -> Events -> Script Actions. The Script Actions list view will be displayed.
- Click the New button. The new Script Action form will be displayed.
- Fill in the form with the following:
Name: AddTenDaysToOpenedAt
Event name: incident.opened_at.add_ten_days
Active: Checked
Script:
gs.info('---> ' + current.number + ' - Opened At: ' + current.opened_at, 'SA: AddTenDaysToOpenedAt');
// Try/Catch...just in case
try {
new MyIncidentUtils().addTenDaysToOpenedAt(current);
}
catch(err) {
gs.info('---> ERROR: ' + err, 'SA: AddTenDaysToOpenedAt');
}
d. Click the Submit button to save.
Your new Script Action should look like this:
5. Using UsefulUtilities.currentFactory fire the new event from Scripts - Background
a. Type the following script into your Run Script field:
var utils = global.UsefulUtilities;
var order = {
type:'Ascending',
field:'number'
};
var current = utils.currentFactory('incident', 1, order, 'active=true');
// did we get one back, and we want to know what the value was before it was changed
gs.info(current.number + ' - Opened At: ' + current.opened_at);
gs.eventQueue('incident.opened_at.add_ten_days', current);
b. Click the Run script button.
c. Note the Incident Number and the Date.
Example response on my PDI:
*** Script: INC0000002 - Opened At: 2018-01-30 00:00:00
6. Verify
a. Navigate to System Policy -> Events -> Event Log
b. Order the Events List View by Created descending.
i. Search the Name field for: incident.opened_at.add_ten_days
ii. Parm1 of the record should contain the Incident Number. Write this number down.
iii. Navigate to Incident -> Open.
iv. Search for the Incident number that was listed in the Event log.
c. Open that incident and see if the Opened At date has been advanced ten days.
i. Right click on the header of the incident to bring up the pop-up menu.
ii. Look for History > List. This will bring up the audit form.
iii. Filter for the Label Opened.
iv. Not that the Old and New columns should show a 10 day addition.
d. Navigate to System Logs -> System Log -> All
i. Order by Created descending
ii. In the messages field search for --->
iii. Observe the records written there.
Your result in the log should look something like this:
Everything should reflect that 10 days were added to the Opened At value.
And that is all there is to it! Have fun!
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-22-2015 07:02 AM
I updated the code and brought the article into alignment with my new formatting standard.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.