Arrayutil / Script include/For loop not working as expected

Ruchi Kumari1
Tera Expert

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 ?

 

10 REPLIES 10

harish41
Tera Guru

Hi,

In the onSubmit Client script, the last line "return false" is not enclosed in a condition. 
When ever this CS is called it will return false. Which means you wont be able to submit.



Mark as correct and helpful if it solved your query.

Regards,
Harish

Hi @harish41 , please ignore return false ..i had commented it in actual script as i am currently testing on submit logic with expected values and do not want to create any request.

Ruchi Kumari1
Tera Expert

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.

Any suggestion on this. ?

I would suggest to cross check the values pushed to array are string data type. 

mrvsSelectedUsers.push(val.toString());

Ankur Bawiskar
Tera Patron
Tera Patron

@Ruchi Kumari1 

you can determine if row was added/removed from MRVS and then manipulate the outside variable

check how it can be done using a widget and Variable of type Custom

How to calculate the total of variables into another variable from a multi-row variable set? 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader