How to use While loop with Else condition
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2023 01:28 AM
Hi All,
I have a requirement where I need to delete the inactive users from assignment groups. Also After deltion I need tocheck if the user is still present in grp member table then give error else it will give the records in excel sheet .
For this I have a below script after delete record , I am checking users in grp member table ut I am not sure how to use while and else condition
var grGrpMember1 = new GlideRecord('sys_user_grmember');
grGrpMember1.addEncodedQuery('group.nameLIKEJM.HR.^group.nameNOT LIKEsecure^user=' + group_member[i]);
grGrpMember1.query();
while (grGrpMember1.next()) {
//print error
gs.info('Parul error');
} else {
gs.info('Parul No error');

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2023 01:27 AM
Hi @Parul Sahu
This code first deletes all group member records that contain the deleted user. It then performs a second query to check if there are any remaining group member records for the deleted user. If there are, it prints an error message. If there aren't, it exports the record to Excel.
var grGrpMember = new GlideRecord('sys_user_grmember');
grGrpMember.addQuery('user', userToDelete);
grGrpMember.query();
while (grGrpMember.next()) {
// Delete the group member record
grGrpMember.deleteRecord();
}
// Check if there are any remaining group member records for the deleted user
var grGrpMemberCheck = new GlideRecord('sys_user_grmember');
grGrpMemberCheck.addQuery('user', userToDelete);
grGrpMemberCheck.query();
if (grGrpMemberCheck.hasNext()) {
// User is still a member of one or more groups - print an error message
gs.info('Error: User ' + userToDelete + ' is still a member of one or more groups.');
} else {
// User has been removed from all groups - export the record to Excel
var excel = new global.ExportToExcel();
excel.exportRecords([current], 'incident_export.xlsx');
}