- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2024 06:45 AM
Hi,
How to get records created on the last week and current week, and after that compare then to get the assigned_to difference value from these records?
table: Incident
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2024 08:13 AM
Hi @DIVI_IT ,
Please have a look at the sample code below:
var lastweek = new GlideRecord('incident');
lastweek.addEncodedQuery("active=true^opened_atONLast week@javascript:gs.beginningOfLastWeek()@javascript:gs.endOfLastWeek()");
lastweek.query();
while(lastweek.next()){
gs.info('Incident No. '+ lastweek.number + ','+ 'Assigned To : '+ lastweek.assigned_to.name);
}
var lastWeekCount = lastweek.getRowCount();
gs.info("Last week's Total Count: "+ lastWeekCount);
var currentWeek = new GlideRecord('incident');
currentWeek.addEncodedQuery("active=true^opened_atONThis week@javascript:gs.beginningOfThisWeek()@javascript:gs.endOfThisWeek()");
currentWeek.query();
while(currentWeek.next()){
gs.info('Incident No. '+ currentWeek.number + ','+ 'Assigned To : '+ currentWeek.assigned_to.name);
}
var currentWeekCount = currentWeek.getRowCount();
gs.info("This week's Total Count: "+ currentWeekCount);
var getDiff = gs.info('Difference: '+ (parseInt(currentWeekCount) - parseInt(lastWeekCount)));
Output:
*** Script: Incident No. INC0009001,Assigned To : Bud Richman
*** Script: Incident No. INC0009005,Assigned To : Antony Alldis
*** Script: Last week's Total Count: 2
*** Script: Incident No. INC0007001,Assigned To : Arya Hajarha
*** Script: Incident No. INC0008001,Assigned To : Luke Wilson
*** Script: Incident No. INC0008111,Assigned To : Aqib Mushtaq
*** Script: This week's Total Count: 3
*** Script: Difference: 1
Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.
Mark this as Helpful / Accept the Solution if this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2024 08:47 AM
Hi @DIVI_IT,
Hope you are doing well.
Proposed Solution
In order to achieve this task, I personally tried it on my Personal Developer Instance and used "Incident" Table so that you can get the solution as soon as possible. For this, you just need to create a "Background Script" and write script as mentioned below to get the difference and unique records from the last week and this week data.
Background Script:-
thisWeekInc = [];
lastWeekInc = [];
var thisWeekCount = 0;
var lastWeekCount = 0;
var grInc = new GlideRecord('incident');
grInc.addEncodedQuery('sys_created_onONThis week@javascript:gs.beginningOfThisWeek()@javascript:gs.endOfThisWeek()^assigned_toISNOTEMPTY');
grInc.query();
while(grInc.next()){
thisWeekCount += 1;
thisWeekInc.push(grInc.assigned_to.getDisplayValue());
gs.print('Incident: ' + grInc.number + ' and ' + 'Assigned To: ' + grInc.assigned_to.getDisplayValue());
}
gs.print('This Week Inc Count: '+ thisWeekInc.length);
var grInc2 = new GlideRecord('incident');
grInc2.addEncodedQuery('sys_created_onONLast week@javascript:gs.beginningOfLastWeek()@javascript:gs.endOfLastWeek()^assigned_toISNOTEMPTY');
grInc2.query();
while(grInc2.next()){
lastWeekCount += 1;
lastWeekInc.push(grInc2.assigned_to.getDisplayValue());
gs.print('Incident: ' + grInc2.number + ' and ' + 'Assigned To: ' + grInc2.assigned_to.getDisplayValue());
}
gs.print('Last Week Inc Count: '+ lastWeekInc.length);
var finalArr = [];
for (var i=0; i<thisWeekInc.length; i++){
if (finalArr.indexOf(thisWeekInc[i])===-1){
finalArr.push(thisWeekInc[i]);
}
}
for (var j=0; j<lastWeekInc.length; j++){
if (finalArr.indexOf(lastWeekInc[j]) === -1){
finalArr.push(lastWeekInc[j]);
}
}
gs.print('Unique/Difference: ' + finalArr.length);
gs.print('Final List of Assigned To Users without Duplicates: \n' + finalArr);
For your reference, also attaching screenshots of the outputs that will give you better insights of how the scripts are working or the best thing will be to follow the solution and execute the scripts on your instance.
If you find this information/knowledge/solution helpful, Please don't forget to mark my solution and reply as helpful and accepted.
Thanks 🙂
Aakash Garg
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 04:39 AM
Hi @DIVI_IT,
Kindly follow these points down below and above proposed solution and re-validate it: -
- Refer to the screenshot down below and first try to check and validate your above requirement in this format (means in browser's console by taking an example of 2 arrays and insert the final data or outcome into a new array).
- Don't use "finalArr" now in eventQueue. You need to use the new array that you have just created as per my previous solution or code.
gs.print('Final List of Assigned To Users without Duplicates: \n' + finalArr); // Replace finalArr with new array that you have created as mentioned in the screenshot below.
If you find this information/knowledge/solution helpful, please don't forget to mark my solution and reply as helpful and accepted.
Thanks :)
Aakash Garg
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2024 08:13 AM
Hi @DIVI_IT ,
Please have a look at the sample code below:
var lastweek = new GlideRecord('incident');
lastweek.addEncodedQuery("active=true^opened_atONLast week@javascript:gs.beginningOfLastWeek()@javascript:gs.endOfLastWeek()");
lastweek.query();
while(lastweek.next()){
gs.info('Incident No. '+ lastweek.number + ','+ 'Assigned To : '+ lastweek.assigned_to.name);
}
var lastWeekCount = lastweek.getRowCount();
gs.info("Last week's Total Count: "+ lastWeekCount);
var currentWeek = new GlideRecord('incident');
currentWeek.addEncodedQuery("active=true^opened_atONThis week@javascript:gs.beginningOfThisWeek()@javascript:gs.endOfThisWeek()");
currentWeek.query();
while(currentWeek.next()){
gs.info('Incident No. '+ currentWeek.number + ','+ 'Assigned To : '+ currentWeek.assigned_to.name);
}
var currentWeekCount = currentWeek.getRowCount();
gs.info("This week's Total Count: "+ currentWeekCount);
var getDiff = gs.info('Difference: '+ (parseInt(currentWeekCount) - parseInt(lastWeekCount)));
Output:
*** Script: Incident No. INC0009001,Assigned To : Bud Richman
*** Script: Incident No. INC0009005,Assigned To : Antony Alldis
*** Script: Last week's Total Count: 2
*** Script: Incident No. INC0007001,Assigned To : Arya Hajarha
*** Script: Incident No. INC0008001,Assigned To : Luke Wilson
*** Script: Incident No. INC0008111,Assigned To : Aqib Mushtaq
*** Script: This week's Total Count: 3
*** Script: Difference: 1
Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.
Mark this as Helpful / Accept the Solution if this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 04:22 AM
That's a great solution, May I ask you to, how to sent an event to users from the difference?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2024 08:47 AM
Hi @DIVI_IT,
Hope you are doing well.
Proposed Solution
In order to achieve this task, I personally tried it on my Personal Developer Instance and used "Incident" Table so that you can get the solution as soon as possible. For this, you just need to create a "Background Script" and write script as mentioned below to get the difference and unique records from the last week and this week data.
Background Script:-
thisWeekInc = [];
lastWeekInc = [];
var thisWeekCount = 0;
var lastWeekCount = 0;
var grInc = new GlideRecord('incident');
grInc.addEncodedQuery('sys_created_onONThis week@javascript:gs.beginningOfThisWeek()@javascript:gs.endOfThisWeek()^assigned_toISNOTEMPTY');
grInc.query();
while(grInc.next()){
thisWeekCount += 1;
thisWeekInc.push(grInc.assigned_to.getDisplayValue());
gs.print('Incident: ' + grInc.number + ' and ' + 'Assigned To: ' + grInc.assigned_to.getDisplayValue());
}
gs.print('This Week Inc Count: '+ thisWeekInc.length);
var grInc2 = new GlideRecord('incident');
grInc2.addEncodedQuery('sys_created_onONLast week@javascript:gs.beginningOfLastWeek()@javascript:gs.endOfLastWeek()^assigned_toISNOTEMPTY');
grInc2.query();
while(grInc2.next()){
lastWeekCount += 1;
lastWeekInc.push(grInc2.assigned_to.getDisplayValue());
gs.print('Incident: ' + grInc2.number + ' and ' + 'Assigned To: ' + grInc2.assigned_to.getDisplayValue());
}
gs.print('Last Week Inc Count: '+ lastWeekInc.length);
var finalArr = [];
for (var i=0; i<thisWeekInc.length; i++){
if (finalArr.indexOf(thisWeekInc[i])===-1){
finalArr.push(thisWeekInc[i]);
}
}
for (var j=0; j<lastWeekInc.length; j++){
if (finalArr.indexOf(lastWeekInc[j]) === -1){
finalArr.push(lastWeekInc[j]);
}
}
gs.print('Unique/Difference: ' + finalArr.length);
gs.print('Final List of Assigned To Users without Duplicates: \n' + finalArr);
For your reference, also attaching screenshots of the outputs that will give you better insights of how the scripts are working or the best thing will be to follow the solution and execute the scripts on your instance.
If you find this information/knowledge/solution helpful, Please don't forget to mark my solution and reply as helpful and accepted.
Thanks 🙂
Aakash Garg
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 02:56 AM
That's a great solution, May I ask you to, how to sent an event to users from the difference?