- 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
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
04-03-2024 05:58 AM
Thanks for your time and tips btw. 🙂
I do not have many experience with scripting but I'm trying 😄
Look I tried to run this simple array script on background script but that's what I've got... 😞
var arr1 = [1, 2, 3];
var arr2 = [2, 3, 4];
var arr = [];
for (var i=0; i<arr1.length; i++){
if (arr2.indexOf(arr1[i]===-1)){
arr.push(arr1[i]);
}
}
gs.print(arr);
:
*** Script: 1,2,3
soo it's not working as I though or I'm doing something wrong..
also the whole script from my action:
maybe you can find where is the issue:
// var arrayUtil = new ArrayUtil();
thisWeek = [];
lastWeek = [];
var thisWeekCount = 0;
var lastWeekCount = 0;
var grWeek = new GlideRecord('u_calendar_events');
grWeek.addEncodedQuery('sys_created_onONThis week@javascript:gs.beginningOfThisWeek()@javascript:gs.endOfThisWeek()');
grWeek.query();
while(grWeek.next()){
thisWeekCount += 1;
thisWeek.push(grWeek.u_user);
gs.print('Current week Assigned To: ' + grWeek.u_user.getDisplayValue());
}
gs.print('This Week Count: '+ thisWeek.length);
var grWeek2 = new GlideRecord('u_calendar_events');
grWeek2.addEncodedQuery('sys_created_onONLast week@javascript:gs.beginningOfLastWeek()@javascript:gs.endOfLastWeek()');
grWeek2.query();
while(grWeek2.next()){
lastWeekCount += 1;
lastWeek.push(grWeek2.u_user);
gs.print('Last week Assigned To: ' + grWeek2.u_user.getDisplayValue());
}
gs.print('Last Week Count: '+ lastWeek.length);
// gs.print(arrayUtil.diff(thisWeek, lastWeek) + ' My test');
var finalArr = [];
for (var i=0; i<thisWeek.length; i++){
if (finalArr.indexOf(thisWeek[i]) === -1){
finalArr.push(thisWeek[i]);
// gs.eventQueue('calendarEvents.reminder', gr, finalArr[i]);
}
}
for (var j=0; j<lastWeek.length; j++){
if (finalArr.indexOf(lastWeek[j]) === -1){
finalArr.push(lastWeek[j]);
}
}
var arr = [];
// for (var g=0; g<lastWeek.length; g++){
// if (thisWeek.indexOf(lastWeek[g]) === -1){
// arr.push(lastWeek[g]);
// // gs.eventQueue('calendarEvents.reminder', grWeek2, arr);
// }
// }
for (var i=0; i<arr.length; i++){
// gs.eventQueue('calendarEvents.reminder', grWeek, arr[i]);
}
gs.print('Unique/Difference: ' + finalArr.length);
gs.print('Final List of Assigned To Users without Duplicates: \n' + arr);
// gs.eventQueue('calendarEvents.reminder', grWeek, finalArr[1]);
gs.print(arr);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 08:07 AM
Hi @DIVI_IT,
Glad to see that you are following my answers and proposed solutions and happy to see that you are marking my answers and solutions as Helpful and Accepted. It really motivates me to post and propose my solutions in the community.
If you do not have many experience with scripting, no worries at all, you will be able to achieve this milestone soon 🙂
Now, let me correct your script that you pasted above. To make it check and correct, I tried to implement your code given in above comment in "Background Script" on my "Personal Developer Instance". And unfortunately, I am successfully able to find an error in your above script. Kindly check and refer below screenshots: -
Before Correct and Error Highlighting: -
After Correct: -
Hope you will be able to get it now and please try it again on your PDI or any other instance to get handy to the scripting and let me know if any other help needed.
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 11:52 PM
now it's working properly 🙂
my bad, Thank you so much for your support 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 11:59 PM
Glad to hear that @DIVI_IT. You really got it now 🙂 Thanks for marking my solution as Helpful and Happy to assist you every time.
Thanks 🙂
Aakash Garg
ServiceNow Developer