Clear the latest value in the list collector field

Amit Rao
Giga Expert

Hi Experts,

Could you help me in clearing out the latest value in the List collector field of a catalog item.
Example: If user enters some IDS in the field 'Funding Source and Structure (AIM ID(s))' and if the ID was already used it throws an alert on the form i.e 'The last id selected was already used by some other product id, please select other id'.
I was using g_form.clearValue('cat_funding_sources_and_structure'); in an onChange catalog client script to clear the value from the List collector field which is clearing out all the values from the list, only the latest value should be cleared out after the alert is populated.
 In the below screenshot, it should remove only the value '31165' (latest)

Screenshot 2022-10-25 105312.png

The screenshot of the alert is below
Screenshot 2022-10-25 105529.png

Please  help me in this requirement.

1 ACCEPTED SOLUTION

kamlesh kjmar
Mega Sage
Mega Sage

Hi @Amit Rao ,

 

You can use the below script to acheive what you need :

 

Below your alert() method write the below lines of code:

 

 

var selectedValues = g_form.getValue('your_field_name').split(',');  //Retreives values selected in the form of array
selectedValues.pop();  // Removes the values selected most recently
g_form.clearValue('your_field_name'); // Clears the field
g_form.setValue('your_field_name',selectedValues.join()); // Repopultes the field with last selected value removed 

 

 

I Hope this helps.

 

Please mark this helpful if this helps and Accept the solution if this solves your issue.

 

Regards,

Kamlesh

View solution in original post

5 REPLIES 5

Sagar Pagar
Tera Patron

Hi @Amit Rao,

 

Try by setting the oldValue for list collector field.

 

g_form.setValue("list_collector_field", oldValue);

 

if not works, pls share your complete scripts to check ID is already used or not.

 

Thanks,

Sagra Pagar

The world works with ServiceNow

OlaN
Giga Sage
Giga Sage

Hi,

Instead of allowing the user to select a "wrong" value, and clearing it, I would suggest that you change the reference qualifier, so you remove choices that are invalid.

Pavankumar_1
Mega Patron

Hi @Amit Rao ,

I think you can't clear value only the particular value on list collector because on clearValue('variable name'); 

on this we will give variable name not any particular value as parameter.

Instead of clearing the value after selecting you can restrict before selecting.

1. Create onchange catalog client script on list collector variable and give your variables.

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var nameslist = g_form.getValue('listcollector variable'); //your list collector variable name
    var names = nameslist.split(',');
    var requested = g_form.getValue('Funding Source variable'); // use Funding Source and Structure (AIM ID(s)) variable name
    
    for (var i = 0; i < names.length; i++) {
        if (requested == names[i]) { //comparing with Funding Source and Structure (AIM ID(s)) value with list collector value
            alert('You canot select value already exst in in the Funding Source and Structure (AIM ID(s))');
            g_form.setValue("listcollector variable", oldValue); 
        }
    }
}

 

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

kamlesh kjmar
Mega Sage
Mega Sage

Hi @Amit Rao ,

 

You can use the below script to acheive what you need :

 

Below your alert() method write the below lines of code:

 

 

var selectedValues = g_form.getValue('your_field_name').split(',');  //Retreives values selected in the form of array
selectedValues.pop();  // Removes the values selected most recently
g_form.clearValue('your_field_name'); // Clears the field
g_form.setValue('your_field_name',selectedValues.join()); // Repopultes the field with last selected value removed 

 

 

I Hope this helps.

 

Please mark this helpful if this helps and Accept the solution if this solves your issue.

 

Regards,

Kamlesh