How to test function in a script include?

othomas1
Kilo Guru

Does anyone know how i can test a function within an script include using a background script?

Function:

getLibrarians: function(className) {
// default return values
var librarians = null;
var users = [];
var groups = [];
this.users = null;
this.groups = null;
this.firstLibrarianUser = null;
this.firstLibrarianGroup = null;
this.firstLibrarianItilGroup = null;

// query for the CMDB ACL access parameter records and process them.
var cmdbCiAcl = new GlideRecord('u_cmdb_ci_table_access_parameters');
cmdbCiAcl.addQuery('u_active', true);
cmdbCiAcl.addQuery('u_class.name', className);
cmdbCiAcl.addQuery('u_class_librarian', true);
cmdbCiAcl.query();

// store all the found users and groups from the CMDB ACLs
while (cmdbCiAcl.next()) {
switch (cmdbCiAcl.u_access_entity.toString()) {
case '1': // user
users.push(cmdbCiAcl.u_user_all.toString());
break;
case '3': // group
groups.push(cmdbCiAcl.u_group_all.toString());
break;
}
}
if (users.length || groups.length) {
librarians = {};
librarians.users = users;
librarians.groups = groups;
this.users = users;
this.groups = groups;
if (users.length) {
this.firstLibrarianUser = users[0];
}
if (groups.length) {
this.firstLibrarianGroup = groups[0];

// iterate over each group and find the first ITIL group if one exists
for (var i = 0; i < groups.length; i++) {
var sysUserGroup = new GlideRecord('sys_user_group');
if (sysUserGroup.get(groups[i])) {
// determine if the group is an ITIL group
if (sysUserGroup.type.toString().indexOf('8bda6993dbd8ba402f11f456bf9619f5') != -1) {
this.firstLibrarianItilGroup = groups[i];
break;
}
}
}
}
}
return librarians;
},

3 REPLIES 3

stevensteichen
Kilo Expert

You can call it like any class or you can debug by copy the entire class into the background script and below the class, after it's end, call it and pass in the parms. I usually hard code in a glideRecord .get(sys id). the call will use the class in the script so you can add debug code if needed.

Better Yet, Install the free plugin Xplore and learn to use that to test code. it's really a great add in.

Michael Jones -
Giga Sage

In the background script window you can just declare the function and call it. 

gs.print(getLibrarians(className); //pass an appropriate value for className!

function getLibrarians(className) {
// default return values
var librarians = null;
var users = [];
var groups = [];
this.users = null;
this.groups = null;
this.firstLibrarianUser = null;
this.firstLibrarianGroup = null;
this.firstLibrarianItilGroup = null;

// query for the CMDB ACL access parameter records and process them.
var cmdbCiAcl = new GlideRecord('u_cmdb_ci_table_access_parameters');
cmdbCiAcl.addQuery('u_active', true);
cmdbCiAcl.addQuery('u_class.name', className);
cmdbCiAcl.addQuery('u_class_librarian', true);
cmdbCiAcl.query();

// store all the found users and groups from the CMDB ACLs
while (cmdbCiAcl.next()) {
switch (cmdbCiAcl.u_access_entity.toString()) {
case '1': // user
users.push(cmdbCiAcl.u_user_all.toString());
break;
case '3': // group
groups.push(cmdbCiAcl.u_group_all.toString());
break;
}
}
if (users.length || groups.length) {
librarians = {};
librarians.users = users;
librarians.groups = groups;
this.users = users;
this.groups = groups;
if (users.length) {
this.firstLibrarianUser = users[0];
}
if (groups.length) {
this.firstLibrarianGroup = groups[0];

// iterate over each group and find the first ITIL group if one exists
for (var i = 0; i < groups.length; i++) {
var sysUserGroup = new GlideRecord('sys_user_group');
if (sysUserGroup.get(groups[i])) {
// determine if the group is an ITIL group
if (sysUserGroup.type.toString().indexOf('8bda6993dbd8ba402f11f456bf9619f5') != -1) {
this.firstLibrarianItilGroup = groups[i];
break;
}
}
}
}
}
return librarians;
}

 

If this was helpful or correct, please be kind and remember to click appropriately! Michael Jones - Proud member of the CloudPires team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

Hi Michael,

Thanks a lot for the reply! I did have a follow up question...

Maybe its just my ignorance about what the function was supposed to return, but i got 'null' in some instances, which from looking at the block of code in the function seems right. I was expecting the function to return actual names though.

Below is the the full script include, would i need to test with the entire script include to get actual names to display?

 

var u_CmdbCiAcl = Class.create();
u_CmdbCiAcl.prototype = {
initialize: function() {
},

/*
* Description: Provides a true/false return value if the current user has write access to the field
* Parameters: root_rule is the class.field, current is the current CI record
* Returns: true to grant access, false to deny access
*/
checkFieldWrite: function(root_rule, current) {
// root_rule = class.field, i.e. cmdb_ci_win_server.name
var currentField = root_rule.replace(current.sys_class_name + '.', ''); // remove the class to get just the field (i.e. name)

// query for the CMDB ACL access parameter records and process them.
var cmdbCiAcl = new GlideRecord('u_cmdb_ci_table_access_parameters');
cmdbCiAcl.addQuery('u_active', true);
cmdbCiAcl.addQuery('u_class.name', current.sys_class_name);
cmdbCiAcl.addQuery('u_access_write', true);
cmdbCiAcl.query();
while (cmdbCiAcl.next()) {
// iterate over each attribute on the access parm record to see if there is a match with the current field
var attribute = cmdbCiAcl.u_attributes.split(',');
for (var i = 0; i < attribute.length; i++) {

// get the sys_dictionary entry for the attribute sys_id
var sysDictionary = GlideRecord('sys_dictionary');
if (sysDictionary.get(attribute[i])) {

// if the current field is this attribute entry then check to see the user is defined as the access entity
if (sysDictionary.element == currentField) {
if (this._isAccessEntity(current, cmdbCiAcl) == true) {
return true;
}
}
}
}
}
return false;
},

/*
* Description: Provides a true/false return value if the current user has create access to the class
* Parameters: Current is the current CI record
* Returns: true to grant access, false to deny access
*/
checkClassCreate: function(root_rule, current) {
// for fields we will check field write acess
// for table/class we will check class create access

// root_rule = class.field, i.e. cmdb_ci_win_server.name
if (root_rule.indexOf('.') != '-1') {
var currentField = root_rule.replace(current.sys_class_name + '.', ''); // remove the class to get just the field (i.e. name)

// if the user has field write access, we will give field create access.
return this.checkFieldWrite(root_rule, current);
}

// query for the CMDB ACL access parameter records and process them.
var cmdbCiAcl = new GlideRecord('u_cmdb_ci_table_access_parameters');
cmdbCiAcl.addQuery('u_active', true);
cmdbCiAcl.addQuery('u_class.name', root_rule);
cmdbCiAcl.addQuery('u_access_create', true);
cmdbCiAcl.query();
while (cmdbCiAcl.next()) {
if (this._isAccessEntity(current, cmdbCiAcl) == true) {
return true;
}
}
return false;
},

/*
* Description: Provides a true/false return value if the current user has delete access to the class
* Parameters: Current is the current CI record
* Returns: true to grant access, false to deny access
*/
checkClassDelete: function(root_rule, current) {
// query for the CMDB ACL access parameter records and process them.
var cmdbCiAcl = new GlideRecord('u_cmdb_ci_table_access_parameters');
cmdbCiAcl.addQuery('u_active', true);
cmdbCiAcl.addQuery('u_class.name', current.sys_class_name);
cmdbCiAcl.addQuery('u_access_delete', true);
cmdbCiAcl.query();
while (cmdbCiAcl.next()) {
if (this._isAccessEntity(current, cmdbCiAcl) == true) {
return true;
}
}
return false;
},

/*
* Description: Provides a list of CMDB Librarians from the CMDB ACLs.
* Parameters: The class to get the librarians.
* Returns: An object with two elements or null if no librarians for the indicated class.
* [object].users containing an array of user librarian sys_ids.
* [object].groups containing an array of group librarian sys_ids.
* Also sets properties for quick access after the initial call with a class
* - [object].users // only the array of users or null
* - [object].groups // only the array of groups or null
* - [object].firstLibrarianUser // only the first librarian user or null
* - [object].firstLibrarianGroup // only the first librarian group of any type or null
* - [object].firstLibrarianItilGroup // only the first librarian ITIL group or null
*/
getLibrarians: function(className) {
// default return values
var librarians = null;
var users = [];
var groups = [];
this.users = null;
this.groups = null;
this.firstLibrarianUser = null;
this.firstLibrarianGroup = null;
this.firstLibrarianItilGroup = null;

// query for the CMDB ACL access parameter records and process them.
var cmdbCiAcl = new GlideRecord('u_cmdb_ci_table_access_parameters');
cmdbCiAcl.addQuery('u_active', true);
cmdbCiAcl.addQuery('u_class.name', className);
cmdbCiAcl.addQuery('u_class_librarian', true);
cmdbCiAcl.query();

// store all the found users and groups from the CMDB ACLs
while (cmdbCiAcl.next()) {
switch (cmdbCiAcl.u_access_entity.toString()) {
case '1': // user
users.push(cmdbCiAcl.u_user_all.toString());
break;
case '3': // group
groups.push(cmdbCiAcl.u_group_all.toString());
break;
}
}
if (users.length || groups.length) {
librarians = {};
librarians.users = users;
librarians.groups = groups;
this.users = users;
this.groups = groups;
if (users.length) {
this.firstLibrarianUser = users[0];
}
if (groups.length) {
this.firstLibrarianGroup = groups[0];

// iterate over each group and find the first ITIL group if one exists
for (var i = 0; i < groups.length; i++) {
var sysUserGroup = new GlideRecord('sys_user_group');
if (sysUserGroup.get(groups[i])) {
// determine if the group is an ITIL group
if (sysUserGroup.type.toString().indexOf('8bda6993dbd8ba402f11f456bf9619f5') != -1) {
this.firstLibrarianItilGroup = groups[i];
break;
}
}
}
}
}
return librarians;
},

/*
* Description: Determines if a user is included in the access entity definition.
* Parameters: Current is the current CI record and cmdbCiAcl is the CMDB ACL to evaluate.
* Returns: true if the user is included in the access entity or false if not.
*/
_isAccessEntity: function(current, cmdbCiAcl) {
switch (cmdbCiAcl.u_access_entity.toString()) {
case '1': // User All
// grant access if the current user is the listed user
if (cmdbCiAcl.u_user_all == gs.getUserID()) {
return true;
}
break;
case '2': // User CI
// grant access if the current user is the user of the specified ci user reference field
if (current[cmdbCiAcl.u_user_ci.element] == gs.getUserID()) {
return true;
}
break;
case '3': // Group All
// grant access if the current user is in the listed group
if (gs.getUser().isMemberOf(cmdbCiAcl.u_group_all)) {
return true;
}
break;
case '4': // Group CI
// grant access if the current user is in the group of the specified ci group reference field
if (gs.getUser().isMemberOf(current[cmdbCiAcl.u_group_ci.element])) {
return true;
}
}
return false;
},

type: 'u_CmdbCiAcl'
};