logo

NJP

ServiceNow check duplicates on 2 columns using GlideAggregate

ServiceNow Blog · May 29, 2026 · article

 var sQuery = 'sys_created_onONThis month@javascript:gs.beginningOfThisMonth()@javascript:gs.endOfThisMonth()';

checkDuplQuestions(sQuery, 'table_sys_id', 'question', 'question_answer');

//checkDuplQuestions(sQuery, 'table_name', 'question', 'question_answer');

function checkDuplQuestions(customQuery, fieldParam, fieldParam2, table) {

  var agg = new GlideAggregate(table);

  agg.addEncodedQuery(customQuery);

  agg.groupBy(fieldParam);

  agg.groupBy(fieldParam2);

  agg.addHaving('COUNT', '>', 1);

  agg.query();

  while (agg.next()) {

    var val1 = agg.getValue(fieldParam);

    var val2 = agg.getValue(fieldParam2);

    var count = agg.getAggregate('COUNT');

    gs.info('Duplicate combination: ' + val1 + ' & ' + val2 + ' | Count: ' + count);

  }

}

View original source

http://www.cloudminus89.com/2026/05/servicenow-check-duplicates-on-2.html