List collector value not removed from script

Ruchi Kumari1
Tera Expert

Hi,

I am trying to remove values from list collector through onsubmit client script.
I have a script include which returns what values are to be removed from the list collector on submit. I tried below script but no luck. I am getting the expected alerts. What am i missing ?

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.toString());
    }

    var userListCollector = g_form.getValue('requested_for');

    var listArr = userListCollector.split(',');
    alert('listArr  ' + listArr); //Getting expected alert

    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) {
         //returned value should be array of sysids of user to be removed from list
        var answer = response;
        var test = answer.split(',');
        alert('anser1 = ' + test); ///getting expected alert
        //if (answer.length > 0) {
        for (var a = 0; a < test.length; a++) {
            var valueToRemove = test[a];
            alert('Value to remove ' + valueToRemove);
            var index = listArr.indexOf(valueToRemove);
            alert('index ' + index); //getting expected index value
            if (index !== -1) { 
                listArr.splice(index, 1); //remove value
            }
        }
        alert('listArr100  ' + listArr); //getting expected value
        g_form.setValue('requested_for', listArr.join(','));
        return false; //This is false only for testing purpose.

 

There is no change in the selected values before / after clicking submit.

12 REPLIES 12

Hi @Community Alums , This is an onchange script on list field. I want to update list field on change of values in mrvs.
Since we have a limitation in MRVS (onchange doesnot work in mrvs) I am trying to update the list onsubmit.
I tried with xmlwait as well in my onsubmit, but no luck.

Ankur Bawiskar
Tera Patron
Tera Patron

@Ruchi Kumari1 

I will recommend not to use onSubmit but please use either of these options and then update the MRVS after RITM is generated

1) workflow run script -> if workflow is used

2) after insert business rule on RITM table

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Ruchi Kumari1 

remember these points:

1) Asynchronous GlideAjax won't work as by the time ajax function returns the value your form will get submitted

2) Synchronous GlideAjax is not allowed in portal and scoped application

Refer this link

How to limit the total quantity of a shopping cart for a specific service catalog item in New York v...

Refer these links for workaround/solution on how to use Synchronous GlideAjax in onSubmit

How To: Async GlideAjax in an onSubmit script

Asynchronous onSubmit Catalog/Client Scripts in ServiceNow

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

Hi @Ankur Bawiskar , I cannot add script in workflow/BR to update the list field as the number of RITMS that would be generated is based on the number of users in list.
The expectation is if user/row is removed from MRVS before submit, the list should be also updated with the users so that correct RITMs can be generated.
Because of business requirement, I cannot make the MRVS readonly to restrict user from deleting any row.
.

@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