Arrayutil / Script include/For loop not working as expected
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-09-2025 08:21 AM - edited ‎07-09-2025 10:18 AM
Hi,
I have a catalog where there are 2 fields :
1. List type > Multiple users can be selected
2. MRVS (having 3 variables : users,machine name,serial number) > This gets populated based on user selected in field 1.
If i remove any row from MRVS, then the list should also be updated. That is user removed from MRVS, should be removed from list as well.
To achieve this, i am writing an onsubmit client script which further calls a script include(passing values from list and MRVS).
My Client script is as below:
function onSubmit() {
var mrvsValues = g_form.getValue('list_the_existing_machine_name');
var mrvsSelectedUsers = [];
var rows = JSON.parse(mrvsValues);
for (var i = 0; i < rows.length; i++) {
var val = rows[i].req_for;
mrvsSelectedUsers.push(val);
}
var userListCollector = g_form.getValue('requested_for');
var listArr = userListCollector.split(',');
var gaGetUserDetails = new GlideAjax('GetUserDetails');
gaGetUserDetails.addParam('sysparm_name', 'getUserDetails');
gaGetUserDetails.addParam('sysparm_listusers', listArr);
gaGetUserDetails.addParam('sysparm_mrvsusers', mrvsSelectedUsers);
gaGetUserDetails.getXMLAnswer(parseAnswer);
function parseAnswer(response) {
var answer = JSON.parse(response); //returned value should be array of sysids of user to be removed from list
//if (answer.length > 0) {
for (var a = 0; a < answer.length; a++) {
var valueToRemove = answer[i];
var index = listArr.indexOf(valueToRemove);
if (index !== -1) { //valueToRemove was found in array
listArr.splice(index, 1); //remove array member from this array
}
}
g_form.setValue('requested_for', listArr.join(','));
//return false;
}
return false;
}
My script include is as below:
getUserDetails: function() {
var listUsers = this.getParameter('sysparm_listusers');
var mrvsUsers = this.getParameter('sysparm_mrvsusers');
gs.log('EMEA2 script include called values listUsers -- ' + listUsers + ' --mrvsUsers--- ' + mrvsUsers); //GETTING EXPECTED VALUES
var listUserName = [];
var gr = new GlideRecord('sys_user');
gr.addEncodedQuery('sys_idIN' + listUsers);
gr.query();
while (gr.next()) {
listUserName.push(gr.name.toString());
}
gs.log('EMEA2 script include listUserName -- ' + listUserName); //GETTING EXPECTED VALUE
var finalValuesList = [];
var removeValuesList = [];
gs.log('EMEA3 SI listUserName.length --' + listUserName.length); //GETTING EXPECTED VALUE
for (var i = 0; i < listUserName.length; i++) {
gs.log('EMEA3 SI listUserName[i] -- ' + listUserName[i]+' value of i '+i); //NOT COMING AS EXPECTED // THIS IS COMING ONLY ONCE FOR INDEX 0 ??
var arrayUtil1 = new arrayUtil();
if (arrayUtil1.contains(mrvsUsers, listUserName[i])) {
finalValuesList.push(listUserName[i].toString());
} else {
removeValuesList.push(listUserName[i].toString());
}
}
gs.log('EMEA3 SI finalValuesList -- ' + finalValuesList + ' removeValuesList ' + removeValuesList); //NOT COMING
var removeUserId = [];
var grUser = new GlideRecord('sys_user');
grUser.addEncodedQuery('nameIN' + removeValuesList);
grUser.query();
while (grUser.next()) {
removeUserId.push(grUser.getUniqueValue());
}
gs.log('emea script include ' + removeUserId); //NOT COMING
//return array removeUserId;
gs.log('EMEA script include 1 finalValuesList -- ' + finalValuesList + ' --removeValuesList--- ' + removeValuesList); //NOT COMING
},
I am not sure what is not working in script include after for loop. Why is for loop iterating only once and not for the entire length ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-09-2025 10:55 AM
Typo in script it's :
var arrayUtil1 = new ArrayUtil();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-09-2025 11:17 AM - edited ‎07-09-2025 11:18 AM
@Ruchi Kumari1 , there is one more error I could see in the client script:
var valueToRemove = answer[i];
//You're referencing i, which was declared in the outer onSubmit function, not in this loop. This will either cause an error or produce incorrect results.
for (var a = 0; a < answer.length; a++) {
var valueToRemove = answer[a]; // Corrrect one ...
If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.
Best regards
Mohammad Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-09-2025 11:46 AM
Hi @Mohammad Danis1 , yeah corrected it. Thanks. I have found the issue in my script include. It was with the values in mrvs. I tried to convert it to string and then split and it worked.
There's one more issue that is not working :
In my onsubmit client script,
listArr.splice(index, 1);
i tried alert for listArr after this and i got expected alert. However, it was expected to remove the value from list since i am using setvalue next .But the value is not getting removed from the list, instead an extra 'none' appears in the slushbucket. Can you suggest something on this. ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2025 12:40 AM
Hi @Ruchi Kumari1 ,
This usually happens when:
The array contains undefined, null, or empty strings.
The field expects a comma-separated string of valid sys_ids or values, and one of them is invalid.
The slushbucket interprets an empty or malformed value as 'none'.
You can clean the array before setting the value:
// Remove the item
listArr.splice(index, 1);
// Clean the array
listArr = listArr.filter(function(item) {
return item !== undefined && item !== null && item !== '';
});
// Set the cleaned value
g_form.setValue('your_field', listArr.join(','));
If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.
Best regards
Mohammad Danish
Regards,
Mohammad Danish