How to use While loop with Else condition

Parul Sahu
Tera Contributor

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');

5 REPLIES 5

Shaikh Mzhar
Tera Guru

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');
}