You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
vargrIncident=newGlideRecord('incident');grIncident.newRecord();grIncident.short_description='Hello from the cheat sheet!';grIncident.insert();
Get a record by ID
vargrIncident=newGlideRecord('incident');if(grIncident.get('incidentSysID')){// Do something!gs.log(grIncident.number+': '+grIncident.short_description);}
Count active records using GlideAggregate
vargaIncCount=newGlideAggregate('incident');gaIncCount.addQuery('active','true');gaIncCount.addAggregate('COUNT');gaIncCount.query();varcount=0;// This will hold the final countif(gaIncCount.next()){count=gaIncCount.getAggregate('COUNT');}
Convert JS Object to JSON
varjsonString=JSON.stringify(obj);
Convert a JSON string to JS Object
varobj=JSON.parse(jsonString);
Look up task records in new state
vargrTask=newGlideRecord('task');grTask.addQuery('state','1');grTask.query();while(grTask.next()){// Do something with each task}
Add OR conditions to a GlideRecord query
vargrIncident=newGlideRecord('incident');varqc=grIncident.addQuery('state','1');// A or B or Cqc.addOrCondition('state','2');qc.addOrCondition('state','3');grIncident.query();while(grIncident.next()){// Do something...}vargrIncident=newGlideRecord('incident');grIncident.addQuery('priority','1');// A and (B or C)varqc=grIncident.addQuery('state','1');gc.addOrCondition('state','2');grIncident.query();while(grIncident.next()){// Do something... }
Commmon GlideSystem methods
varuser=gs.getUser();// Get current user ObjectvaruserID=gs.getUserID();// Get current user's IDvaruserName=gs.getUserName();// Get current user's user_namevargroups=gs.getUser().getMyGroups();// Get current user's groupsvarisMember=gs.getUser().isMemberOf(current.getValue('assignment_group'));// Check by GroupIDvarisMember=gs.getUser().isMemberOf('Service Desk');// Check by Group NamevarhasRole=gs.hasRole('itil');// Determine if current user has 'itil' role
Common AngularJS Directives for using in the HTML Template
Directive
Purpose
ng-model
This directive binds an input, select, textarea (or custom form control) to the Angular Scope
ng-repeat
Iterate through a data array to e.g. build a list of records or a table
ng-show/ng-hide
Evaluate an expression to determine if an element is shown
ng-if
Evaluate an expression to determine if an element is shown (removes the element from the DOM, therefore more costly - BUT it will trigger a re-evaluation of the widget code, which can come in handy.)
ng-class
Equivalent to the CSS element 'class' - count e.g. be used to assign a CSS class conditionally
ng-change
Triggered when the according element changes (e.g. ng.change = "c.myChangeFunction()")
ng-include
Fetches, compiles and includes an external HTML fragment (e.g. Angular template)
watch
AngularJS record watcher - e.g. used to do something on the client when value changes on the server
Service Portal Client Side Utilities
API
Purpose
spUtil
Client side only utility. Can be used for example to add Info Messages to the page - also required for recrodWatch functionality. See documentation for more functions available
addErrorMessage/addInfoMessage
Displays a notification message of different types (requires spUtil to work - e.g. spUtil.addInfoMessage("Your order has been placed"))
recordWatch
Requires spUtil. Will watch a specific query on a defined table for updates, if an update happens you can respond to it in real-time. Usage: spUtil.recordWatch($scope, "incident", "active=true");
rootScope
The root scope element will e.g. let you broadcast and event to the rootScope of the page (allows communication between widgets). Usage: $rootScope.broadcastEvent("myEvent.name", parm1);
console (Browser)
Powerful utility available in both client and server. Can be used to "log" debug messages of, for exmaple, the data object
Service Portal Server Side Utilities
API
Purpose
$sp
Server side only utility. Can be used, for example, to get parameters from the URL and get the GlideRecord for the current Portal. See documentation for a list of all available functions.
console (Browser)
Powerful utility available in both client and server. Can be used to "log" debug messages.
References
Copied over from ServiceNow Developer Cheat Sheet + Quick Reference brochure. Slight edits have been made. The original document can be found here.