need to remove duplicates or get unique values from script

mikechristianse
Kilo Contributor

Hello,   I found the following script   that does what I want it to do with one exception.       It is looking through the Approvals table to find all outstanding approvals so we can send a notification out based on the results.     The only problem is,   it is pulling back all values instead of distinct values,     so when the event fires it will be sending out multiple emails to each approver when they only need one.     How do I constrain/filter   it down to only unique or distinct values so it only pulls one row per approver?

script:

var gr = new GlideRecord('sysapproval_approver');

var approver = gr.approver.getRefRecord();

      gr.addQuery('state', 'requested');

      gr.addQuery('sysapproval.sys_class_name','sc_request');

      gr.addQuery(approver.notification,'2');

      gr.query();

      while (gr.next()) {

          gs.eventQueue("catalog_task_approval_reminder",gr, gs.getUserID(), gs.userName());

      }  

1 ACCEPTED SOLUTION

jake_mckenna
ServiceNow Employee
ServiceNow Employee

The array util functions is where i would take this, and more importantly the contains function. http://wiki.servicenow.com/index.php?title=ArrayUtil#gsc.tab=0



It could allow you to do the unique filter and then send out the event. I haven't tested this, but here is what i was thinking.



var gr = new GlideRecord('sysapproval_approver');


var approver = gr.approver.getRefRecord();


      gr.addQuery('state', 'requested');


      gr.addQuery('sysapproval.sys_class_name','sc_request');


      gr.addQuery(approver.notification,'2');


      gr.query();


      var userArray = [];


      var arrayUtil = new ArrayUtil();


      while (gr.next()) {


      if(arrayUtil.contains(userArray,gr.approver.toString())) {


              continue;


      }


          gs.eventQueue("catalog_task_approval_reminder",gr, gs.getUserID(), gs.userName());


          userArray.push(gr.approver.toString());


      }


View solution in original post

4 REPLIES 4

jake_mckenna
ServiceNow Employee
ServiceNow Employee

The array util functions is where i would take this, and more importantly the contains function. http://wiki.servicenow.com/index.php?title=ArrayUtil#gsc.tab=0



It could allow you to do the unique filter and then send out the event. I haven't tested this, but here is what i was thinking.



var gr = new GlideRecord('sysapproval_approver');


var approver = gr.approver.getRefRecord();


      gr.addQuery('state', 'requested');


      gr.addQuery('sysapproval.sys_class_name','sc_request');


      gr.addQuery(approver.notification,'2');


      gr.query();


      var userArray = [];


      var arrayUtil = new ArrayUtil();


      while (gr.next()) {


      if(arrayUtil.contains(userArray,gr.approver.toString())) {


              continue;


      }


          gs.eventQueue("catalog_task_approval_reminder",gr, gs.getUserID(), gs.userName());


          userArray.push(gr.approver.toString());


      }


Dave Aiken
ServiceNow Employee
ServiceNow Employee

Try using GlideAggregate and the groupBy method.   http://wiki.servicenow.com/?title=GlideAggregate#gsc.tab=0



Example:


var count = new GlideAggregate('sysapproval_approver');


count.addQuery('state', 'requested');


count.addAggregate('COUNT');


count.groupBy('approver');


count.query();




while (count.next()) {


  gs.log("approver:   " + count.approver);


}




mikechristianse
Kilo Contributor

Jacob,     your array idea worked well for me,       Dave I didn't try yours yet as I was already going down the array path and was pretty close to what Jacob replied with.       to anyone else that looks at this,       I did need to change


  1.       gr.addQuery('sysapproval.sys_class_name','sc_request');     to
  2.       gr.addQuery('sysapproval.source_table','sc_request');  

to get it to work in Helsinki.  


ServiceNerd
Giga Guru
Giga Guru