Blog

JavaScript Class Functions Lookup

The ServiceNow wiki has some great documentation about the available javascript in ServiceNow.  However I wanted another way to look up all the functions of a JS class. This would help specifically when I was looking at custom Javascript classes in ServiceNow.

I researched a little on the internet and wrote a quick Script Include that can do this.

Script Include

Name: getSNEMethods
Client Callable: true
Script:
function getSNEMethods(obj) {
var result = [];
for (var id in obj) {
if (typeof(obj[id]) == "function") {
result[result.length] = id;
}
}
return result;
}

Example 1: Find ArrayUtil class functions

From Scripts - Background run this statement

var arrayUtil = new ArrayUtil();
gs.print(getSNEMethods(arrayUtil).join("\n"));

Results

*** Script: ensureArray
diff
indexOf
concat
type
convertArray
union
initialize
unique
contains
intersect

Example 2: Find CIUtils class functions

From Scripts - Background run this statement

var CIUtils = new CIUtils();
gs.print(getSNEMethods(CIUtils).join("\n"));

Results

*** Script: maxDepth
maxSize
currentDepth
added
services
parents
initialize
servicesAffectedByTask
_addParentServices
type
_addService
servicesAffectedByCI

Example 3: Find TableUtils class functions

From Scripts - Background run this statement

var TableUtils = new TableUtils();
gs.print(getSNEMethods(TableUtils).join("\n"));

Results

*** Script: tableName
getValidTableName
dropAndClean
dropTableAndExtensions
sanitizeTableName
_z
isValidField
getHierarchy
isSoloClass
getAbsoluteBase
drop
getTables
getAllExtensions
canStage
setUpModuleForDynamicReferenceTableCreate
_dropFromDatabase
createTableFromDynamicReference
isBaseClass
tableExists
initialize
getTableExtensions
hasExtensions

Now this is just one way of using this function.  There are probably other ideas out there too, let me know in the comments!

Thanks,
Mike