- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 11:53 PM
Hi Team,
I have written below script in background script the purpose of the script is Group incidents by assigned to and list only grouped records where the assigned to have more than one record. The error i am getting is
invalid return
var agg = new GlideRecord('incident');
agg.addAggregate('COUNT','assigned_to');
agg.query();
var arr =[];
while(agg.next()){
var incidentCount = agg.getAggregate('COUNT','assigned_to');
if(incidentCount >1) {
gs.info('Display the count',[incidentCount])
var inc = new GlideRecord('incident');
inc.addQuery('assigned_to'+agg.assigned_to);
inc.query();
while(inc.next()){
arr.push(inc.sys_id.toString())
}
}
return arr;
gs.print([incidentCount]);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2024 01:38 AM - edited 05-14-2024 01:49 AM
The code which you have written will not satisfy your requirement, i have modified the entire code ,Please refer the working code as below,
var agg = new GlideAggregate('incident');
agg.addAggregate('COUNT', 'assigned_to');
agg.orderBy('assigned_to');
agg.query();
var incidentCount = 0;
while (agg.next()) {
var num = [];
incidentCount = agg.getAggregate('COUNT', 'assigned_to');
if (incidentCount > 1) {
var gr = new GlideRecord('incident');
gr.addQuery('assigned_to', agg.assigned_to);
gr.query();
while (gr.next()) {
num.push(gr.getValue('number'));
}
gs.info('count for ' + agg.assigned_to.name + ':' + incidentCount + " Incident Numbers : " + num );
}
}
Please mark this comment as Correct Answer/Helpful if it helped you.
Regards,
Swathi Sarang
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2024 12:22 AM - edited 05-14-2024 12:27 AM
Hi @Khalid9030 ,
There were few errors in the code i have corrected it please do give it a try ,
var agg = new GlideAggregate('incident');
agg.addAggregate('COUNT', 'assigned_to');
agg.query();
var arr = [];
var num = [];
if(agg.next()) {
var incidentCount = agg.getAggregate('COUNT', 'assigned_to');
if (incidentCount > 1) {
gs.info('Display the count: ' + incidentCount);
var inc = new GlideRecord('incident');
inc.addQuery('assigned_to' , agg.assigned_to);
inc.query();
while (inc.next()) {
arr.push(inc.sys_id.toString());
num.push(inc.getValue('number'));
}
}
gs.print('array : ' + arr);
gs.print('Number : ' + num);
}
Please mark this comment as Correct Answer/Helpful if it helped you.
Regards,
Swathi Sarang
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2024 12:33 AM
Hi @Khalid9030
try the
var incidentCount =0;
var arr=[];
var assginto=[];
var agg = new GlideAggregate('incident');
agg.addAggregate('COUNT','assigned_to');
agg.query();
while(agg.next()){
incidentCount = agg.getAggregate('COUNT','assigned_to');
assginto.push(agg.assigned_to.toString());
}
gs.print('Display the count'+incidentCount);
if (incidentCount > 1) {
var inc = new GlideRecord('incident');
inc.addQuery('assigned_to' , assginto);
inc.query();
while (inc.next()) {
arr.push(inc.sys_id.toString());
}
gs.print(arr);
return arr;
below code.
Thanks
dgarad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2024 01:27 AM - edited 05-14-2024 02:30 AM
@swathisarang98 yes you own the right , credits to you... 🙂
☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2024 01:35 AM
Hello @Sohail Khilji ,
Can you please let me know what is the difference in my below code and your code?
I can see only variable names and info texts are changed .And more over its not the working code yet as i re-tested it once again and was about to post the correct code
Regards
Swathi sarang