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

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);


}


}


}