- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2025 01:23 AM
Hi,
I have attached system log, the business rule fetching CI sys id and current user id(from user table).
event triggered, script action. but the script execution failed into the line indexof. index value printing -1 still though it has value. kindly let me know the correction in the script:
Business Rule:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2025 05:26 AM
function deactivateuser(id, username) {
var gr = new GlideRecord("cmdb_ci");
gr.addQuery('sys_id', id);
gr.query();
while (gr.next()) {
var approver = gr.getValue('u_level_1_approver');
if (!approver) continue; // Safeguard for empty fields
var approverarray = approver.split(',').map(function(item) {
return item.trim().toLowerCase();
});
username = username.trim().toLowerCase();
gs.log("Approver Array: " + approverarray);
gs.log("Username: " + username);
var index = approverarray.indexOf(username);
gs.log("Index: " + index);
if (index !== -1) {
approverarray.splice(index, 1);
gr.setValue('u_level_1_approver', approverarray.join(',')); // Convert back to string
gr.update();
gs.log("Updated Approver Array: " + approverarray);
}
}
}
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2025 02:11 AM
What happens if you replace 'if(index != -1)' with 'if(index !== -1)'?
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2025 03:03 AM
Yeah, i tried but it didnt work..also i tried index > -1
its printing index value = -1.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2025 05:26 AM
function deactivateuser(id, username) {
var gr = new GlideRecord("cmdb_ci");
gr.addQuery('sys_id', id);
gr.query();
while (gr.next()) {
var approver = gr.getValue('u_level_1_approver');
if (!approver) continue; // Safeguard for empty fields
var approverarray = approver.split(',').map(function(item) {
return item.trim().toLowerCase();
});
username = username.trim().toLowerCase();
gs.log("Approver Array: " + approverarray);
gs.log("Username: " + username);
var index = approverarray.indexOf(username);
gs.log("Index: " + index);
if (index !== -1) {
approverarray.splice(index, 1);
gr.setValue('u_level_1_approver', approverarray.join(',')); // Convert back to string
gr.update();
gs.log("Updated Approver Array: " + approverarray);
}
}
}
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2025 06:43 AM
can you try this?
deactivateuser(event.parm1, event.parm2);
function deactivateuser(id, username) {
var approverarray = [];
var gr = new GlideRecord("cmdb_ci");
gr.addQuery('sys_id', id);
gr.query();
if (gr.next()) {
var approver = gr.getValue('u_level_1_approver');
approverarray = approver.split(',');
gs.info("the approver array=" + approverarray);
gs.info("the username=" + username);
// Trim and lowercase for comparison
var trimmedUsername = username.trim().toLowerCase();
var index = -1;
for (var i = 0; i < approverarray.length; i++) {
if (approverarray[i].trim().toLowerCase() === trimmedUsername) {
index = i;
break;
}
}
gs.info("index=" + index);
if (index != -1) {
approverarray.splice(index, 1);
gs.info("the array after removal=" + approverarray);
gr.u_level_1_approver = approverarray.join(',');
gr.update();
}
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader