- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2016 11:21 AM
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());
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2016 11:38 AM
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());
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2016 11:38 AM
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());
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2016 11:50 AM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2016 12:23 PM
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
- gr.addQuery('sysapproval.sys_class_name','sc_request'); to
- gr.addQuery('sysapproval.source_table','sc_request');
to get it to work in Helsinki.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2022 03:25 AM