- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-12-2020 10:37 AM
Hi All,
We have created an email notification script to create a report and i am not able to see all the records.
notification script:
(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
baseUrl = gs.getProperty("glide.servlet.uri");
email.setSubject("User- User ticket details : " + gs.now());
var arr = [1,2,3,6,7,8]; //New,In Progress, On Hold, Resolved,CLosed,Canceled
var storeCount = [];
var listCount = {};
var x = 0;
var getGroup,getCountInProg, getCountOnHold, getCountNew,getState,cc, getStateVal,getIncCount;
while(x < arr.length)
{
gs.log("User-Inside while");
var gr = new GlideRecord('incident');
gr.addQuery('active',true);
gr.addQuery('assigned_to','46d44a23a9fe19810012d100cca80666' );
gr.addQuery('state',arr[x]);
gr.orderByDesc('state');
gr.chooseWindow(0, 1);
gr.query();
while(gr.next())
{
gs.log("User-inside if");
getState = gr.getValue('state');
if(getState == arr[x])
{
getGroup = gr.getDisplayValue('assignment_group');
getStateVal = gr.getDisplayValue('state');
cc = parseInt(gr.getRowCount());
listCount = {Groups: getGroup,State: getStateVal, DataVal : cc};
storeCount.push(listCount);
gs.info('Count Values is: ' + JSON.stringify(listCount));
}
}
x++;
}
gs.info('StoreVal is: ' + JSON.stringify(storeVal));
template.print("<table border=2>");
template.print("<tr><td colspan=5 align =center>List Of Tickets</td></tr>");
template.print("<tr><th>Assignment group</th><th>State</th><th>Count</th></tr>");
storeCount.forEach(mySortingFunction);
function mySortingFunction(item, index)
{
gs.log("User- Inside function");
template.print('<tr><td>' + item.Groups + '</td><td>'+ item.State + '</td><td>'+ item.DataVal + '</td></tr>');
}
})(current, template, email, email_action, event);
I am seeing the output as below:
Expected output needs to be based on state and assignment group in the above output i am not able to see the assignment groups "Hardware" and "Network" Instead i am able to see only 2 group and the count based on state :
I need to get individual state count based on individual groups.
Can anyone please help me out on this?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2020 08:32 AM
I rechecked and the code works absolutely fine.
May be on your instance someone is createing tickets or reassigning tickets.
How are you triggering this email? If its via ticket creation then that might be the reason why you see a difference.
-Tanaji
Please mark reply correct/helpful if applicable

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2020 02:58 AM
I assume Generic Request is a custom field and name of the field is u_generic_request. If not then you need to replace u_gereric_request in the following code.
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
var finalJSONArray = [];
baseUrl = gs.getProperty("glide.servlet.uri");
email.setSubject("User- User ticket details : " + gs.now());
var count = new GlideAggregate('incident');
count.addQuery('active', 'true');
count.addQuery('assigned_to', '46d44a23a9fe19810012d100cca80666');
count.addAggregate('COUNT');
count.groupBy('assignment_group');
count.groupBy('state');
count.groupBy('u_generic_request');
count.query();
if (count.hasNext()) {
template.print("<table border=2>");
template.print("<tr><td colspan=5 align =center>List Of Tickets</td></tr>");
template.print("<tr><th>Assignment group</th><th>State</th><th>Count</th><th>INC Count</th><th>GREQ Count</th></tr>");
var tempJSON = {};
while (count.next()) {
var assignmentGroup = count.assignment_group.getDisplayValue();
var stateValue = count.state.getDisplayValue();
var genericRequestValue = count.u_generic_request.getDisplayValue();
var groupCount = count.getAggregate('COUNT');
//template.print("<tr><td>" + assignmentGroup + "</td><td>" + stateValue + "</td><td>" + groupCount + "</td></tr>");
if (JSON.stringify(tempJSON) == "{}") {
tempJSON.assignment_group = assignmentGroup;
tempJSON.state = stateValue;
tempJSON.u_generic_request = {};
tempJSON.u_generic_request[genericRequestValue] = groupCount;
tempJSON.count = groupCount;
} else if (tempJSON.assignment_group == assignmentGroup && tempJSON.state == stateValue) {
tempJSON.u_generic_request[genericRequestValue] = groupCount;
tempJSON.count = parseInt(tempJSON.count) + parseInt(groupCount);
} else {
finalJSONArray.push(tempJSON);
tempJSON = {};
tempJSON.assignment_group = assignmentGroup;
tempJSON.state = stateValue;
tempJSON.u_generic_request = {};
tempJSON.u_generic_request[genericRequestValue] = groupCount;
tempJSON.count = groupCount;
}
}
finalJSONArray.push(tempJSON);
for (var i in finalJSONArray) {
template.print("<tr><td>" + finalJSONArray[i].assignment_group + "</td><td>" + finalJSONArray[i].state + "</td><td>" + finalJSONArray[i].count + "</td><td>" + (finalJSONArray[i].u_generic_request["false"] ? finalJSONArray[i].u_generic_request["false"] : 0) + "</td><td>" + (finalJSONArray[i].u_generic_request["true"] ? finalJSONArray[i].u_generic_request["true"] : 0) + "</td></tr>");
}
template.print("</table>");
}
})(current, template, email, email_action, event);
-Tanaji
Please mark reply correct/helpful if applicable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2020 07:01 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2020 08:32 AM
I rechecked and the code works absolutely fine.
May be on your instance someone is createing tickets or reassigning tickets.
How are you triggering this email? If its via ticket creation then that might be the reason why you see a difference.
-Tanaji
Please mark reply correct/helpful if applicable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2020 10:58 PM