Notes from working for Service Now

I've been working for Service Now since 2012. I've had to do things to the poor platform it was never intended to allow.
So, here's some of them. Maybe you'll find 'em useful.

Killing long running processes

Go to https://<my_instance_url>/cancel_my_transactions.do will cancel any script and DB queries associated with your current session.

Removing a Script Include from the current context

My usage:

//re-include qunit to allow for multiple runs
try{  
    sys_include_map.remove('QUnitLoader');
    sys_include_map.remove('QUnit-v1.23');
    GlideController.removeGlobal("QUnit");
    GlideController.removeGlobal("exports");
}catch(e){
    CidQUnit2.log("could not remove QUnitLoader")
}
gs.include('QUnitLoader');  

QUnitLoader creates a global exports object, to allow QUnit to include itself on, then copies exports.QUnit to the global namespace. This code removes the names from the record the system keeps of already included scripts (keeps from re-including scripts that have already been used).
Then it removes the global references the scripts create.
If you are using standard SNC Script includes, then it becomes a bit easier:

sys_include_map.remove('<name_of_script_include>');  
GlideController.removeGlobal('<name_of_script_include>');  
Logging tricks
  • gs.print() will log only to your session,(activate System Diagnostics -> Debug Log) gs.log() will log to your session, as well as system logs.
    Problem with gs.print is that it always prefixes the output with *** Script:. If you don't want this, use GlideLog.print()

  • GlideSysLog.info('QUnit', message); //same as gs.log()

  • GlideLog.info('QUnit: ' + message); //same as gs.print(), but without that *** Script: prefix
Inserting/Updating records without triggering business rules

Use gr.setWorkflow(false);
Example

var gr = new GlideRecord("table");  
gr.addQuery("column=value");  
gr.setWorkflow(false);  
gr.query();  
while(gr.next()){  
     //do things
}
Updating records without updating the sys_updated fields

Use gr.autoSysFields(false);
Example

var gr = new GlideRecord("table");  
gr.addQuery("column=value");  
gr.autoSysFields(false);  
gr.query();  
while(gr.next()){  
     //do things
}
Forcing record update, even if no fields have changed

Normally when a record does not have any data changed, a call to gr.update() has no effect.
If you really want to trigger an update anyways you can use gr.setForceUpdate(true);
Example

var gr = new GlideRecord("table");  
gr.addQuery("column=value");  
gr.setWorkflow(false);  
gr.query();  
while(gr.next()){  
     if(reason){
          //do things
          gr.setForceUpdate(true);
          gr.update();
     }
}
Preventing record changes from being stored in update sets

If you've got a table that's storing updates in update sets, you can temporarily this tracking:
gs.suppressUpdateSynch(true);

Example

var oldSync = gs.suppressUpdateSynch(true);

var gr = new GlideRecord("table");  
gr.addQuery("column=value");  
gr.setWorkflow(false);  
gr.query();  
while(gr.next()){  
//do things that you don't want stored
}

gs.suppressUpdateSynch(oldSync);