
- 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 of knowledge and/or familiarity with Scripting in ServiceNow.
Normally you cannot pass complex objects in the parameters of gs.eventQueue. It simply won't reconstitute them on the Script Action side. For example, let's create a simple Script Action.
Lab 1.1 - Events: Passing Object Problem
1. Navigate to System Policy -> Events -> Registry. The Registry list view will appear.
2. Click the New button. A blank Event Registration form will appear. Fill in the form with the following:
- Name: object.passing
- Table: Global [global]
- Fired by: Scripts background or fix script
- Description: Test of object passing
- Click the Submit button to save.
3. Navigate to System Policy -> Events -> Script Actions. The Script Actions list view will appear.
4. Click the New button. A blank Script Action form will appear. Fill in the form with the following:
- Name: ObjectPassing
- Event Name: object.passing
- Order: 100
- Active: checked
- Script:
var myObject = event.parm1;
var message = '---> \n';
message += 'Name: ' + myObject.name;
message += '\nNumber: ' + myObject.number;
gs.info('---> [{1}] {0}', [message, 'SA: ObjectPassing']);
Your Script Action should look like this:
f. Click the Submit button to save.
5. Navigate to System Definition -> Scripts - Background. The Run script screen will appear. Type in the following script (make sure you are in Global scope):
var myObject = {name:'test', number:'1234abc'};
gs.eventQueue('object.passing', null, myObject, '');
NOTE: If you are not passing a GlideRecord then the second parameter may be set to null. Normally this is where current would be placed.
NOTE: I suggest copying this script to notepad as we will be using it later.
6. Click the Run script button at the bottom of the form. A blank results screen will be displayed with the number seconds it took to execute the script.
7. Navigate to System Logs -> System Log -> All. A list view of all log records for today will appear.
- Sort by Created descending.
- Search for Message begins with --->
- It may take a few seconds for the log entry to appear. When it does the Message field should look something like this:
---> [SA: ObjectPassing] --->
Name: undefined
Number: undefined
As you can see our simple object was not passed correctly!
Now, to correct this we will need to use JSON.
Lab 1.2 - Events: Using JSON With an Event
1. Navigate to System Policy -> Events -> Script Actions.
2. Edit our ObjectPassing Script Action.
- Change line 1 of the script to add the JSON decode:
var myObject = new global.JSON().decode(event.parm1);
var message = '---> \n';
message += 'Name: ' + myObject.name;
message += '\nNumber: ' + myObject.number;
gs.info('---> [{1}] {0}', [message, 'SA: ObjectPassing']);
b. Click the Update button to save.
NOTE: The decode method will be used to reconstitute our object from binary to the original format that was passed.
3. Navigate to System Definition -> Scripts - Background. Enter in the following script; which now contains our JSON encode. Note the addition of a JSON encode. This changes the object into a simple variable (binary) that can now be passed.
var myObject = {name:'test',number:'1234abc'};
gs.eventQueue('object.passing', null, new global.JSON().encode(myObject), '');
4. Click the Run script button at the bottom of the form. A blank results screen will be displayed with the number seconds it took to execute the script.
5. Navigate to System Logs -> System Log -> All.
- Sort by Created descending.
- Search for Message begins with --->
- It may take a few seconds for the log entry to appear. When it does the Message field should look something like this:
---> [SA: ObjectPassing] --->
Name: test
Number: 1234abc
Now our object is being passed correctly!
This can be done with any complex object you want to pass to the event call (GlideRecord objects in addition to or other than current, object arrays, etc.). Interestingly you don't have to JSON arrays as they are passed as comma-delimited strings. However, my recommendation is to JSON them as well just to be safe.
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-10-2015 08:33 PM
I updated the code and brought the article into alignment with my new formatting standard.
- 3,683 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.