Catalog Client Script Returning Undefined Values
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2023 10:21 AM
I'm having an issue with a field on a catalog item displaying a list of permissions based off of a table. I'm just getting "undefined" as the values.
I'm fairly new to JavaScript and ServiceNow and unsure if my issue is with the code or with the table/permissions/etc. so I'll try and give as much detail as I can (so sorry for the long post). The original catalog item, table, and scripts were created for our SN environment during our instance's initial setup (before I joined), and we wanted to add some extra fields to display when filling out the catalog item.
We're using a Multi Line Text variable that is filled in by a Catalog Client Script, that references a Script Include, that references a custom table of roles and additional referenced tables for applications and drives based off of those roles. I've added the new Lag table and copied the same permissions. I've also copied and modified the catalog client script that populates the variable, as well as copying and modifying sections of the script it references to include the new table.
Catalog Client Scripts
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ajax = new GlideAjax('UserAccessUtil');
ajax.addParam('sysparm_name','buildStandardDrivesList');
ajax.addParam('sysparm_role', g_form.getValue('role'));
ajax.getXML(setVariables);
}
function setVariables(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('standard_drives_list', answer);
}
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ajax = new GlideAjax('UserAccessUtil');
ajax.addParam('sysparm_name','buildLagGroupsList');
ajax.addParam('sysparm_role', g_form.getValue('role'));
ajax.getXML(setVariables);
}
function setVariables(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('lag_groups_list', answer);
}
Script Include
var UserAccessUtil = Class.create();
UserAccessUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getStandardDrives : function(pRole) {
role = pRole.toString() || this.getParameter('sysparm_role');
var driveList = [];
var onbDrive = new GlideRecord('u_m2m_drives_onboarding_roles');
onbDrive.addQuery('u_onboarding_roles', role);
onbDrive.query();
while(onbDrive.next()) {
driveList.push(onbDrive.u_drives.toString());
}
return 'sys_idIN' + driveList.join();
},
getDrivesToAdd : function(pRole) {
role = pRole.toString() || this.getParameter('sysparm_role');
var driveList = [];
var onbDrive = new GlideRecord('u_m2m_drives_onboarding_roles');
onbDrive.addQuery('u_onboarding_roles', role);
onbDrive.query();
while(onbDrive.next()) {
driveList.push(onbDrive.u_drives.toString());
}
return 'sys_idNOT IN' + driveList.join();
},
getStandardApplications : function(pRole) {
role = pRole.toString() || this.getParameter('sysparm_role');
var appList = [];
var onbApp = new GlideRecord('u_m2m_applications_onboarding_r');
onbApp.addQuery('u_onboarding_roles', role);
onbApp.query();
while(onbApp.next()) {
appList.push(onbApp.u_applications.toString());
}
return 'sys_idIN' + appList.join();
},
getApplicationsToAdd : function(pRole) {
role = pRole.toString() || this.getParameter('sysparm_role');
var appList = [];
var onbApp = new GlideRecord('u_m2m_applications_onboarding_r');
onbApp.addQuery('u_onboarding_roles', role);
onbApp.query();
while(onbApp.next()) {
appList.push(onbApp.u_applications.toString());
}
return 'sys_idNOT IN' + appList.join();
},
buildStandardDrivesList : function(pRole) {
role = pRole.toString() || this.getParameter('sysparm_role');
var driveList = '';
var onbDrive = new GlideRecord('u_m2m_drives_onboarding_roles');
onbDrive.addQuery('u_onboarding_roles', role);
onbDrive.query();
while(onbDrive.next()) {
if (driveList != '') {
driveList += '\n';
}
driveList += onbDrive.u_drives.u_drive;
}
return driveList;
},
buildStandardApplicationsList : function(pRole) {
role = pRole.toString() || this.getParameter('sysparm_role');
var appList = '';
var onbApp = new GlideRecord('u_m2m_applications_onboarding_r');
onbApp.addQuery('u_onboarding_roles', role);
onbApp.query();
while(onbApp.next()) {
if (appList != '') {
appList += '\n';
}
appList += onbApp.u_applications.u_application;
}
return appList;
},
//Lag Groups Test
getLagGroups : function(pRole) {
role = pRole.toString() || this.getParameter('sysparm_role');
var LaggList = [];
var onbLagG = new GlideRecord('u_m2m_lags_onboarding_r');
onbLagG.addQuery('u_onboarding_roles', role);
onbLagG.query();
while(onbLagG.next()) {
laggList.push(onbLagG.u_lag_groups.toString());
}
return 'sys_idIN' + laggList.join();
},
getLagGroupsToAdd : function(pRole) {
role = pRole.toString() || this.getParameter('sysparm_role');
var laggList = [];
var onbLagG = new GlideRecord('u_m2m_lags_onboarding_r');
onbLagG.addQuery('u_onboarding_roles', role);
onbLagG.query();
while(onbLagG.next()) {
laggList.push(onbLagG.u_drives.toString());
}
return 'sys_idNOT IN' + laggList.join();
},
buildLagGroupsList : function(pRole) {
role = pRole.toString() || this.getParameter('sysparm_role');
var laggList = '';
var onbLagg = new GlideRecord('u_m2m_lags_onboarding_r');
onbLagg.addQuery('u_onboarding_roles', role);
onbLagg.query();
while(onbLagg.next()) {
if (laggList != '') {
laggList += '\n';
}
laggList += onbLagg.u_lag_groups.u_lag_group;
}
return laggList;
},
//End Lag Groups test
type: 'UserAccessUtil'
});
I'm not sure where to check next. The scripts have to be partly working at least because I get the correct number of entries in the field when a role is selected, they all just show up as undefined. The original 2 (Drives & Applications) are both still populating correctly. I've come to my wits end trying to troubleshoot this. Hopefully someone has some insight they could share. I'm hoping my inexperience means it's just something simple I'm missing, but if any other info is needed just let me know.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2023 12:37 PM
I wonder if this is the issue:
laggList += onbLagg.u_lag_groups.u_lag_group;
Try changing that to a string by adding .toString() to the end of that line. There have been some cases where I needed to do that. This is especially true when I am working with a reference field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2023 12:53 PM
No, unfortunately that did not fix it. Changed the line to below and still getting undefined's.
laggList += onbLagg.u_lag_groups.u_lag_group.toString();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2023 01:28 PM
Ok. Considering the scripting is working to some degree, I want to confirm the correct table(s) and field(s) are being called in the script include for buildLagGroupsList.
Table - u_m2m_lags_onboarding_r
Field used to filter that table - u_onboarding_roles
Then, we loop through the filtered results. If we take a look at one of the records in this resulting list, there should be a field u_lag_groups that references another record. In that other record, there is a field u_lag_group that we are getting the value from.
When you look through these records, are all these references/dot-walking correct?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2023 01:25 PM
What happens to the undefined if you change this:
laggList += onbLagg.u_lag_groups.u_lag_group;
To this:
laggList += onbLagg.u_lag_groups.getDisplayValue();