When Yes/No field changes from Yes to No - Clear values selected in a list collector

SMuir
Mega Guru

I have a Catalog Item with a Yes/No Field

If Yes is selected

The UI policy will show a List collector, allowing them to select more than one role. This references a custom table I created to store the values of the roles.

If the customer changes the question from Yes to No

The List collector does not clear the selected entries.

The script I am using is not working and I wondered if someone can give me some assistance?

Steps to Reproduce

  1. Selected Related list > Catalog Client Scrips from the Catalog item form
  2. Selected onChange
  3. Selected Field sap_access_to_coe_ams { this is the Yes/No question}

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

          return;

    }

var sapAccessToCoe = g_form.getValue('sap_access_to_coe_ams');   //Create variable and get the current value

 

if (sapAccessToCoe == 'No '); {   //if If no has been selected for Sap access to Coe

 

g_form.setValue('sap_role_coe');   //This is the List collector field. To set Value to null

}

}

When I save this and then try the form the values are still showing and not clearing as I would expect it to.

1 ACCEPTED SOLUTION

SMuir
Mega Guru

ServiceNow support have got back to me with a working script. If anyone is trying to do the same thing. this might help.



if (isLoading || newValue == '') {


return;


}


var sapAccessToCoe = g_form.getValue('sap_access_to_coe_ams');   //Create a variable for the Yes No field



if (sapAccessToCoe == 'no'); {   //If the yest No question for COE AMS is No



var listCollectorName = 'sap_role_coe'; //Name of your list collector



var leftBucket = gel(listCollectorName + '_select_0');


var rightBucket = gel(listCollectorName + '_select_1');



var selectedOptions = rightBucket.options;


var selectedIDs = [];



for(var i = 0; i < selectedOptions.length; i++){



//Check for the item to add here based on sys_id


selectedIDs.push(i);


//var value = selectedOptions[i].value;


//rightBucket.remove(value);


// var lefBucket = gel(listCollectorName + '_select_0');


//leftBucket.add(value);



}



moveSelectedOptions(selectedIDs,rightBucket,leftBucket,'--None--');


sortSelect(leftBucket);



var leftOptions = leftBucket.options;



for(var j = 0; j<leftOptions.length; j++){


var value = leftOptions[j].innerHTML;


if(value == '--None--'){


leftBucket.remove(j);


}


}


}


View solution in original post

5 REPLIES 5

Chandu Telu
Tera Guru
Tera Guru

HI Sarah



Please find the below link it may help you


https://www.servicenowguru.com/scripting/client-scripts-scripting/move-list-collector-options/


List collector Clear Option



Please provide your feedback appropriately (Like, Helpful, Endorse AND/OR Correct) to help community.


To clear List Collector values on change of a variable in SR.


Please provide your feedback appropriately (Like, Helpful, Endorse AND/OR Correct) to help community.


SMuir
Mega Guru

Thanks for this,



I have a working script now but have another problem. If you can help?



Is there a way to add to the script to refresh the Available options.



Steps to reproduce



Created client script as follows



function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {


          return;


    }


var sapAccessToCoe = g_form.getValue('sap_access_to_coe_ams');   //Create a variable for the Yes No field


 


if (sapAccessToCoe == 'no'); {   //If the yest No question for COE AMS is No


 


var listCollectorName = 'sap_role_coe'; //Name of your list collector




var rightBucket = gel(listCollectorName + '_select_1');




var selectedOptions = rightBucket.options;




for(var i = 0; i < selectedOptions.length; i++){          




    //Check for the item to add here based on sys_id




    var value = selectedOptions[i].value;




        rightBucket.remove(value);


    }


}


}


  1. Selected Try it
  2. Selected Yes to Would you like to Add/Remove Permissions for COE/AMS?
  3. The List collector appears (showing all options)
  4. find_real_file.png
  5. Move items from Available to Selected
  6. find_real_file.png
  7. Change the Question Would you like to Add/Remove Permissions for COE/AMS? To No
  8. Change the Question Would you like to Add/Remove Permissions for COE/AMS? To Yes
  9. The Available list has not refreshed to show all the items or clearing the selection properly. It still says Sap Access role needed at the bottom.
  10. find_real_file.png