Need to remove duplicate values under list collector field "Can Read" in knowledge table

Harsha M V
Tera Contributor

Hi All,

How do we remove duplicate values under list collector field "Can Read" in knowledge table.
There are 2000+ articles where a field "Can Read" has multiple duplicate values added somehow.

How to write a script and cleanup the duplicate values for the field "Can Read" and have only unique values.
Sample picture attached

Please help with the script.

5 REPLIES 5

Dan Ostler
Tera Guru

This script should work. Take note of the fieldName variable and ensure that is the correct field name for the field you would like to update.

 

(function() {
  var fieldName = 'can_read_user_criteria'; // Ensure that this is the correct field name for the "Can Read" field you are working with
  var gr = new GlideRecord('kb_knowledge');
  gr.addNotNullQuery(fieldName);
  gr.query();

  while (gr.next()) {
    var valuesArray = (gr.getValue(fieldName) || '').split(',');

    if (Array.isArray(valuesArray)) {
      var uniqueValuesArray = valuesArray.reduce(function(acc, item) {
        if (acc.indexOf(item) === -1) {
          return acc.concat(item);
        }
        return acc;
      }, []);

      if (valuesArray.length > uniqueValuesArray.length) {
        gr.setValue(fieldName, uniqueValuesArray.toString());
        gr.update();
      }
    }
  }
})();

 

Thanks for this Dan.  I took what your code above and used it as the basis for a custom flow action to remove the duplicate values on a list collector field as the last step in my flow.  Works well, much appreciated.

Paul

Thanks for the feedback Paul, I'm glad I could help!

sujata sakhare
Tera Contributor

hi,

 List Collector type variable is displaying the records on a table, the right way would be to remove the duplicate record and enforce uniqueness on the display column.  If this is not feasible in this situation, next try to add to the Reference qualifier the attribute that makes these needed duplicates necessary, something like this.

active=true

Otherwise you would need to change the Reference qualifier to call a Script Include which queries the table and builds an array of unique values on the display column, then returns a comma-separated list of sys_ids of those records.

javascript: new CatalogUtil().getCountries();

 Thanks.