SlightlyLoony
Tera Contributor

Often when you're writing or modifying some JavaScript on the server, you'd like to know what's really inside a variable or object. Especially an object! You can write some special logging statements to do this, but that can be quite tedious — again, especially for an object. And then there's the special case of the global context (which is itself an object) — it would really be nice to take a look at what's in there.

Well, you can.

Let's take a simple object as our first example. Try this code:


var myObj = {};
myObj.x = 'bogonomics';
myObj.y = 'taxicide';
myObj.z = 1459392;
JSUtil.logObject(myObj, 'myObj');


If you run this code on Spring 2011 Preview 1 or later, you'll see this in the log:

*** Script: Log Object: myObj
Object
x: string = bogonomics
y: string = taxicide
z: number = 1459392

Nothing profound here, just a nicely formatted description of what you've got in that object. But that's a very handy thing! The last line, of course, is what produces the log entry. The logObject() method in JSUtil takes two arguments. The first is simply the object you want a description of. The second is optional; it's the name of that object, so you can identify it in the log (very useful if your script has several logObject() calls for different variables).

Now we'll do something slightly more complicated. Try this code:

var myObj = {};
myObj.w = {a:'elephant',b:'giraffe',c:'hyena',d:'kitten'};
myObj.x = 'bogonomics';
myObj.y = 'taxicide';
myObj.z = 1459392;
JSUtil.logObject(myObj, 'myObj');


Now you'll see this in the log:

*** Script: Log Object: myObj
Object
y: string = taxicide
z: number = 1459392
w: Object
c: string = hyena
d: string = kitten
a: string = elephant
b: string = giraffe
x: string = bogonomics


The property myObj.w is itself an object, containing the properties a, b, c, and d. The logObject() method walks through all of these and outputs them as well, indented so that you can see that they're all part of w. With logObject(), you can log objects with up to 25 levels of contained objects, all with a single line of code.

We've recently enhanced the logObject() method so that it will explicitly log GlideRecord and GlideElement instances, providing useful information for them in the output. Here's one last example, this time a business rule, and showing how you can use logObject() to see what's in the global context of any script. Here's the business rule I created (it doesn't matter what table you do this on):

JSUtil.logObject(this, 'Business Rule Global Context');


Here's what you'll get:

*** Script: Log Object: Business Rule Global Context
Object
cmdbCIChildren: function
incidentGetViewName: function
getRoleDelegatorGroupOptions: function
sys_meta_BUILDER: GlideElement (or child): sys_meta_BUILDER = System meta data
v_field_editor_typeGetChoices: function
isMandatoryNotification: function
sys_action_collection: string = sys_script_include
sys_action_table: Java Object: com.glide.script.GlideRecordBusinessRules
cti: function
doesEntryFloat: function
getNextObjNumberPadded: function
getRelatedRecords: function
system: Object
action: Java Object: com.glide.script.Action
populatedFilterRefQual: function
sc_req_itemGetViewName: function
g_warn_time: number = 100
getRelatedGroup: function
getCurrencyFilter: function
UserGetSysId: function
v_field_editor_lengthGetChoices: function
cmdb_rel_type_suggest_relationshipGetChoices: function
getUsersForRoleDelegation: function
sc_req_item_stageGetChoices: function
current: GlideRecord('sys_script_include') @ AbstractAjaxProcessor
sc_cat_itemGetViewName: function
global_simple_events: function
historyRecordPresent: function
answer: null = null
GetIDValue: function
deleteAllRecords: function
workflowTaskTemplateReferenceQualifier: function
getBannerText: function
kbGetText: function
sys_action: string = update
sys_include_map: Java Object: java.util.HashSet
addChoiceType: function
updateSetPreviewInstalled: function
google_getLatLong: function
getNextObjNumber: function
v_ws_field_editor_lengthGetChoices: function
getUserDashboards: function
recordHasUpdateXML: function
checkForUnscheduled: function
getRoledUsers: function
now: function
sys_userGetEmailAddress: function
homeGetViewName: function
__wf_engine_stack_count: number = 1
getGroupMembersGivenGroupID: function
getDelegatableRoles: function
nowDateTime: function
dynamicUserCreation: function
GetReasonForChange: function
sys_action_when: string = after
getMyApprovals: function
addWFVariables: function
getPrimaryCompany: function
closeRelatedTasks: function
getRelatedCI: function
closeProblem: function
recurseParents: function
sys_script_include: Java Object: com.glide.script.GlideRecordBusinessRules
Class: Object
create: function
sc_req_item_stage_DeliveryPlanChoices: function
sys_dictionary_elementCanWrite: function
getUserHomepages: function
addVarsForSet: function
getMyGroups: function
getLDAPSources: function
action: Java Object: com.glide.script.Action
cmn_notif_device_listGetViewName: function
getRoleListIds: function
getGroupQualifier: function
GlideArrayRecord: function
calculatePriority: function
addVariableSets: function
cmn_notif_deviceGetViewName: function
recurseImageParents: function
emailsToUsers: function
getMyRoleDelegationGroups: function
checkAllApprovers: function
global_events: function
gs: Object
action: Java Object: com.glide.script.Action
getMyAssignments: function
limitFieldType: function
previous: GlideRecord('sys_script_include') @ AbstractAjaxProcessor
ProposedChangesExist: function
closeIncident: function
globalListen: function
trim: function
incidentGetCaller: function
sys_meta: GlideElement (or child): sys_meta_BUILDER = System meta data
getDisplayValueOf: function
addVariables: function
getDefaultDeliveryPlan: function
getRoleDelegatorOptions: function
incident_listGetViewName: function
GetGroupFilter: function
closeChange: function
limitFieldLength: function
isApprovalMine: function
isAdvancedUI: function
notifDeviceRefQual: function
JSUtil: function
getBannerSrc: function
lastWeek: function
getGroupMembersGivenGroupName: function
getDictionaryEntry: function
getBaseFilter: function
getGroupsInHierarchy: function
getAllMembersOfMyGroups: function
sys_dictionary_nameCanWrite: function
kbWriteComment: function
createProblem: function
padObjNumber: function
zz_transaction_cache: Java Object: com.glide.sys.cache.LRUCache
wf_variables: function
getGroupMembers: function


There's a lot of stuff in a business rule's global context! But note in particular how functions, GlideRecords, and GlideElements are shown. You can figure out a lot just from this listing. The functions are from global business rules, and you can find them in there to see what they do. You can see "current", and what table and record that GlideRecord is on.

To get the global context in any script, all you need to do is put the code JSUtil.logObject(this, 'a name'); at the beginning of the script. When the script executes, that will log the the contents of "this", which at that moment is a reference to the global context (because the global context is itself an object).

3 Comments