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

Harish KM
Kilo Patron
Kilo Patron

Ca you share the screen shot of duplicate values?


Regards
Harish

See attached





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/>.


Attached is a screen shot from the record producer page with the setting to not allow dups. I need this to work in a form.





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/>.


Add this line before you query glide record


  if(newValue == ''){


  g_form.clearOptions('fieldname');


  g_form.addOption('fieldname', '', '-- None --');


  }


  if(newValue == oldValue){


  return;


  }


Regards
Harish