Remove duplicates from choice list

jim_calhoun
Kilo Contributor

I am running the script below in both a record produce page and on a form.   The record producer page has a setting you can turn on or off to not show duplicates.   In the form you do not.   The values in the choice list are not duplicates when you look at the full record, but one value in the record can be duplicated when not in combination with the other values in the record.

I am trying to get rid of the duplicates in the "u_secondary_issue_category' field.   Does anyone have any idea how to do this??

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

if(newValue == oldValue){

return;

}

//remove all items from subcat drop down to start

// Used the g_form.clearOptions() function instead of g_form.removeOption() function

g_form.clearOptions('u_secondary_issue_category');

//build a new list of dependent options

var gp = new GlideRecord('u_novitex_issue_dictionary');

gp.addQuery('u_primary_system', newValue);

gp.query();

while(gp.next()){

g_form.addOption('u_secondary_issue_category', gp.u_sub_system, gp.u_sub_system);

}

  }

1 ACCEPTED SOLUTION

Gurpreet07
Mega Sage

Its seems like choice table already contains duplicates. You need to build your own logic to remove duplicates. Try below code.



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


if(newValue == oldValue){


return;


}


//remove all items from subcat drop down to start


// Used the g_form.clearOptions() function instead of g_form.removeOption() function


g_form.clearOptions('u_secondary_issue_category');



//build a new list of dependent options


var previousValue = '' ;


var gp = new GlideRecord('u_novitex_issue_dictionary');


gp.addQuery('u_primary_system', newValue);


gp.orderBy('u_sub_system');


gp.query();


while(gp.next()){


var currentValue = gp.u_sub_system ;


if(currentValue != previousValue){


g_form.addOption('u_secondary_issue_category', currentValue, currentValue);


}


previousValue = currentValue;


}


  }


View solution in original post

9 REPLIES 9

No Change. I already add that string starting on line 2. I added it again on line 12.






Jim Calhoun, Solutions Engineer, MCITP — Customer Focused Innovations


205 Michigan Ave<x-apple-data-detectors://1/> I ste. 300 I Chicago, IL I 60601


M: 708-285-5986<tel:708-285-5986>


Jim.Calhoun@novitex.com<mailto:Jim.Calhoun@novitex.com> |www.novitex.com<http://www.novitex.com/> | About Novitex<http://bit.ly/1j3tBOU>



For support please open an Incident ticket in ServiceNow<https://novitextest.service-now.com/>.


Gurpreet07
Mega Sage

Its seems like choice table already contains duplicates. You need to build your own logic to remove duplicates. Try below code.



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


if(newValue == oldValue){


return;


}


//remove all items from subcat drop down to start


// Used the g_form.clearOptions() function instead of g_form.removeOption() function


g_form.clearOptions('u_secondary_issue_category');



//build a new list of dependent options


var previousValue = '' ;


var gp = new GlideRecord('u_novitex_issue_dictionary');


gp.addQuery('u_primary_system', newValue);


gp.orderBy('u_sub_system');


gp.query();


while(gp.next()){


var currentValue = gp.u_sub_system ;


if(currentValue != previousValue){


g_form.addOption('u_secondary_issue_category', currentValue, currentValue);


}


previousValue = currentValue;


}


  }


That worked, thank you very much. I will also look at the client GlideRecord.





Jim Calhoun, Solutions Engineer, MCITP — Customer Focused Innovations


205 Michigan Ave<x-apple-data-detectors://1/> I ste. 300 I Chicago, IL I 60601


M: 708-285-5986<tel:708-285-5986>


Jim.Calhoun@novitex.com<mailto:Jim.Calhoun@novitex.com> |www.novitex.com<http://www.novitex.com/> | About Novitex<http://bit.ly/1j3tBOU>



For support please open an Incident ticket in ServiceNow<https://novitextest.service-now.com/>.


Gurpreet07
Mega Sage

Note: you are using GlideRecord in client script and it is making synchronous server call. Have a look at client GlideRecord.


http://wiki.servicenow.com/index.php?title=Client_Side_GlideRecord#gsc.tab=0


Gurpreet07
Mega Sage

Hi Jim,



Can you please close this thread by marking the replies Correct/Helpful.