Email notification and additional comments.

Maxwell3
Kilo Guru

Hello all,

Is there a way to trigger email notifications per the amount of Additional Comments?

So, if there is 1 additional comment the email notification would say Update 1 and if there is 2 Additional comments, the notification would say Update 2 and so on. These notifications will be triggered by the amount of additional comments in the current incident.

Right now, the notification is being triggered per update count but that is not efficient, the notification needs to be triggered per Additional comments count.

1 ACCEPTED SOLUTION

Hi,

Firstly, please mark any other reply of mine thus far as "Helpful", if it was.

Secondly, you could try something like this:

var setID;
var gr2 = new GlideRecord('sys_history_set');
gr2.addQuery('id', current.sys_id);
gr2.query();
if (gr2.next()) {
setID = gr2.sys_id;
}

var gr = new GlideRecord('sys_history_line');
gr.addQuery('set', setID);
gr.addQuery('field', 'comments');
gr.orderByDesc('sys_created_on');
gr.query();
if (gr.next()) {
template.print('gr.getDisplayValue('update'));
template.print('<br />');
}

So you have to grab the set for the history line "package". So you can get this by glide querying with the current.sys_id (so the current record this script is ran on), then we're tossing that over to a variable that we are going to use later in another glide query on the actual history line package, filtering those results for only records part of that set, then only comments, then only the latest comment, and then displaying that value update #.

However, if you're just trying to get the count of entries for "comments" then you can do: gr.getRowCount(); for example

https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=r_GlideRecord-getRowCount

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

12 REPLIES 12

Hello,

You wouldn't do the script there, I mentioned mail script, specifically.

So you would still set the notification to send when the additional comments changes, but then the mail script would query for which update/additional comment this is. Then in your notification, in the "what to send" tab, you would simply use your mail script like:

Update ${mail_script:comm_update_count}: Blah blah

Which in turn would result in (for example)...

Update 6: Blah Blah

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hello Allen,

The below query does print out a list of numbers, I think I need to add another condition so it queries the additional comments on the current Incident. I tried adding ('user',current.user_id); That did not work. When I take out the current.user_id condition, it works but I am not sure if the numbers are correct.

Below is what I have so far:

(function runMailScript(current, template, email, email_action, event) {

var hist = new GlideRecord('sys_history_line');
hist.addQuery('field','comments');
hist.orderByDesc('sys_created_on');
hist.setLimit(5);
hist.query();
while (hist.next())
{
template.print(hist.getDisplayValue('update'));
template.print('<br />');
}
})(current, template, email, email_action, event);

 

Hi,

Firstly, please mark any other reply of mine thus far as "Helpful", if it was.

Secondly, you could try something like this:

var setID;
var gr2 = new GlideRecord('sys_history_set');
gr2.addQuery('id', current.sys_id);
gr2.query();
if (gr2.next()) {
setID = gr2.sys_id;
}

var gr = new GlideRecord('sys_history_line');
gr.addQuery('set', setID);
gr.addQuery('field', 'comments');
gr.orderByDesc('sys_created_on');
gr.query();
if (gr.next()) {
template.print('gr.getDisplayValue('update'));
template.print('<br />');
}

So you have to grab the set for the history line "package". So you can get this by glide querying with the current.sys_id (so the current record this script is ran on), then we're tossing that over to a variable that we are going to use later in another glide query on the actual history line package, filtering those results for only records part of that set, then only comments, then only the latest comment, and then displaying that value update #.

However, if you're just trying to get the count of entries for "comments" then you can do: gr.getRowCount(); for example

https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=r_GlideRecord-getRowCount

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi,

If my reply above helped answer your question and guide you correctly, please mark it as Correct.

If you need further assistance, let me know.

Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Allen,

Thank you for your assistance. My task is to display the additional comments and manipulate the date/time field, display the custom priority levels next to them. The issue that you assisted me with worked but now I am trying to attach your solution to my requirements. Here what i have, I have posted the results below the code. It keeps printing out the same number, the number being printed out depends on the location of the brackets. 

var gre;
var setID;
var gr2 = new GlideRecord('sys_history_set');
gr2.addQuery('id', current.sys_id);
gr2.query();
while (gr2.next()){
setID = gr2.sys_id;
}

var gr = new GlideRecord('sys_journal_field');

gr.addQuery('element','comments');
gr.addQuery('element_id',current.sys_id.toString());
gr.orderByDesc('sys_created_on');
gr.setLimit(5);
gr.query();
gr.next();

while(gr.next()){

var gr3 = new GlideRecord('sys_history_line');
gr3.addQuery('field','comments');
gr3.addQuery('set', setID);
gr3.orderByDesc('sys_created_on');
gr3.query();
gr3.next;
while (gr3.next()){
gre = gr3;
}
var date = gr.sys_created_on;
var nowDateTime = new GlideDateTime(date);
var gd1 = new GlideDate();
gd1.setValue(nowDateTime.getLocalDate());
var gt = new GlideTime();
gt.setValue(nowDateTime.getLocalTime());
var str = gd1.getByFormat("MM/dd/yyyy");
var t = gt.getByFormat('hh:mm a');

template.print(current.u_ic_priority.getDisplayValue() +' UPDATE '+gre.getDisplayValue('update') + ' INC received an update on ' + str +' at '+ t +' ET: '+ gr.value.toString());
template.print('<br />');
template.print('<br />');

}

find_real_file.png

I ran the script separately, the results are below:

find_real_file.png