Glide While loop only running once
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2022 11:56 AM
I have a script include and function:
getRoleDescription: function(roleName) {
var role_descr;
gs.info(" Role Name = " +roleName );
var roleGR = new GlideRecord('x_XXXX_hcm_roles');
roleGR.addQuery('role_name', roleName);
roleGR.query();
roleGR.getRowCount();
return roleGR.getRowCount();
while (roleGR.next()) {
role_descr= roleGR.getDisplayValue('role_descr');
}
return role_descr;
},
it get called by
getRoleFromList: function(selected_roles, requestID) {
var roleArr = [];
if (selected_roles != "") {
var roles = selected_roles.split(',');
for (var x = 0; x < roles.length; x++) {
var roleObj = {};
roleObj.requestIDVal = requestID;
roleObj.role_name = roles[x];
//gs.info("Run 1 " + " In getRolesInfo " + " Role " + roles[x]);
roleObj.role_descr = this.getRoleDescription (roles[x]);
roleArr.push(roleObj);
} //eof for loop
} //eof if
return roleArr;
},
I have created a console comment and the function getRoleDescription receives all the a role runs it for the first time but returns no values for any subsequent run. Am not sure why it only runs once when its being called multiple times. Does anyone know why
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2022 12:13 PM
Your problem is the line that states:
return roleGR.getRowCount();
That will exit the getRoleDescription function and just return the row count of your GlideRecord query result.
It's not entirely clear what you are trying to do, but also bear in mind if you are trying to get multiple role descriptios returned then the line:
role_descr= roleGR.getDisplayValue('role_descr');
will just overwrite the existing value of role_descr on every loop of the while. You need to add it to the existing variable NOT replace it on each iteration of the While loop.
Regards
Paul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2022 01:21 PM
i thought so, but when i replaced the code with:
getRoleFromList: function(selected_roles, requestID) {
var roleArr = [];
var role_descr
var roleGR = new GlideRecord('x_XXXX_hr_hcm_roles');
if (selected_roles != "") {
var roles = selected_roles.split(',');
for (var x = 0; x < roles.length; x++) {
var roleObj = {};
roleObj.requestIDVal = requestID;
roleObj.role_name = roles[x];
roleGR.addQuery('role_name', roles[x]);
roleGR.query();
while (roleGR.next()) {
roleObj.role_descr = roleGR.getDisplayValue('role_description');
}
roleArr.push(roleObj);
} //eof for loop
} //eof if
return roleArr;
},
it returns on the portal:
-
HCM ETRAC Creator<p>Grant access to allow user to create hiring/change job data transaction for students, staff, or faculty. User will only be able to create transactions for authorized departments. </p>
-
HCM TL AM Timekeeper
-
HCM Labor Cost Distribution
It only displays the first rows description only

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2022 03:41 PM
There's a syntax error in the script.
1. getDisplayValue() doesn't take argument. To return display value of column "role_descr", use roleGR.role_descr.getDisplayValue().
2. Looping isn't necessary to retrieve 1 record. Instead of using .query() and while(), use .get()
getRoleDescription: function(roleName) {
var role_descr;
gs.info(" Role Name = " + roleName);
var roleGR = new GlideRecord('x_XXXX_hcm_roles');
if (roleGR.get('role_name', roleName)) {
return roleGR.role_descr.getDisplayValue();
}
return;
},
getRoleFromList: function(selected_roles, requestID) {
var roleArr = [];
if (selected_roles != "") {
var roles = selected_roles.split(',');
for (var x = 0; x < roles.length; x++) {
var roleObj = {};
roleObj.requestIDVal = requestID;
roleObj.role_name = roles[x];
//gs.info("Run 1 " + " In getRolesInfo " + " Role " + roles[x]);
roleObj.role_descr = this.getRoleDescription(roles[x]);
roleArr.push(roleObj);
} //eof for loop
} //eof if
return roleArr;
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2022 05:44 PM
Still returns, (only runs once)
-
HCM ETRAC Creator<p>Grant access to allow user to create hiring/change job data transaction for students, staff, or faculty. User will only be able to create transactions for authorized departments. </p>
-
HCM TL AM Timekeeper
-
HCM Labor Cost Distribution