How to send latest attachments when added to the incident without sending the old one ?

Santhoshi Jaini
Kilo Contributor

How to send latest attachments when added to the incident without sending the old one ?

1 ACCEPTED SOLUTION

Sruthi25
Giga Contributor

1. First Create a after insert business rule on sys_attachment table which triggers an event.

find_real_file.png

And add a script in Advanced as shown below:

(function executeRule(current, previous /*null when async*/) {
var incObj = new GlideRecord('incident');
incObj.addQuery('sys_id',current.table_sys_id);//Queries only the attachments that are on the incident table
incObj.query();

if(incObj.next()) {
//gs.log("Hello");

gs.eventQueue('incident.attachment.add', incObj, current.file_name);// Triggers an event with the file name of the attchment
}



})(current, previous);

 

2. create a new event "incident.attachment.add".

find_real_file.png

3. The event triggers a notification.

find_real_file.png

and write the advanced script to check whether the attachments are added to the incident. If attchments are added it returns true else returns false so that this notification doesn't trigger.

run();

function run(){


var agg = new GlideAggregate('sys_attachment');
agg.addQuery('table_sys_id', current.sys_id);
agg.addQuery('u_sent',false);
agg.addAggregate("COUNT");//counts how many latest attachments are added 
agg.query();
var answer = false;
if (agg.next()) {

answer = agg.getAggregate("COUNT");

if (answer > 0)
answer = true;
else
answer = false;
}

return answer;
}

4. We need to write one more business rule on sys_email before insert 

find_real_file.png

var emails = "INC-Attachment Added";//mentioning the notification for which this business rule should execute
emails = "503d455adb582300aefffbefbf9619fe"; // INC-Attachment Added
emails = emails.split(',');
//var emailLogGR = new GlideRecord('syslog_email');

var parentMail = "false";
for(var i in emails){
parentMail = checkParentMail(emails[i]);
if( parentMail == true);
break;
}
gs.info("hello1: " + parentMail);
if(parentMail == true){
copyNewAttachments();
}

function checkParentMail(parentName){
if(current.headers.indexOf(parentName)>-1){
return true;
}

}

function copyNewAttachments(){
GlideSysAttachment.copy("incident",current.instance,'sys_email',current.sys_id); // Copy all attachemnts to email

var gr2 = new GlideRecord('sys_attachment'); // Delete attachemnts that were sent (u_sent = true)
gr2.addQuery('table_sys_id',current.sys_id);
gr2.addQuery('u_sent',true);
gr2.deleteMultiple();
markSent();
}


function markSent(){
var gr = new GlideRecord('sys_attachment'); //Mark the new attachments to be sent
gr.addQuery('table_sys_id',current.instance);
gr.addQuery('u_sent',false);
gr.query();
while(gr.next()) {
gr.u_sent = true; // mark so that we don't send again
gr.update();
}
}

View solution in original post

9 REPLIES 9

Rajesh Mushke
Mega Sage
Mega Sage
Santhoshi Jaini,
 
you want to add the latest attachment in the notification?
 
 
Thanks,
Rajashekhar Mushke
Community Leader-18

 



Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke

yes I need only latest attachments in the email.

you need to use mail script in your notification.

refer the thread below.

 

https://community.servicenow.com/community?id=community_question&sys_id=b35687e1db1cdbc01dcaf3231f96...

Sruthi25
Giga Contributor

1. First Create a after insert business rule on sys_attachment table which triggers an event.

find_real_file.png

And add a script in Advanced as shown below:

(function executeRule(current, previous /*null when async*/) {
var incObj = new GlideRecord('incident');
incObj.addQuery('sys_id',current.table_sys_id);//Queries only the attachments that are on the incident table
incObj.query();

if(incObj.next()) {
//gs.log("Hello");

gs.eventQueue('incident.attachment.add', incObj, current.file_name);// Triggers an event with the file name of the attchment
}



})(current, previous);

 

2. create a new event "incident.attachment.add".

find_real_file.png

3. The event triggers a notification.

find_real_file.png

and write the advanced script to check whether the attachments are added to the incident. If attchments are added it returns true else returns false so that this notification doesn't trigger.

run();

function run(){


var agg = new GlideAggregate('sys_attachment');
agg.addQuery('table_sys_id', current.sys_id);
agg.addQuery('u_sent',false);
agg.addAggregate("COUNT");//counts how many latest attachments are added 
agg.query();
var answer = false;
if (agg.next()) {

answer = agg.getAggregate("COUNT");

if (answer > 0)
answer = true;
else
answer = false;
}

return answer;
}

4. We need to write one more business rule on sys_email before insert 

find_real_file.png

var emails = "INC-Attachment Added";//mentioning the notification for which this business rule should execute
emails = "503d455adb582300aefffbefbf9619fe"; // INC-Attachment Added
emails = emails.split(',');
//var emailLogGR = new GlideRecord('syslog_email');

var parentMail = "false";
for(var i in emails){
parentMail = checkParentMail(emails[i]);
if( parentMail == true);
break;
}
gs.info("hello1: " + parentMail);
if(parentMail == true){
copyNewAttachments();
}

function checkParentMail(parentName){
if(current.headers.indexOf(parentName)>-1){
return true;
}

}

function copyNewAttachments(){
GlideSysAttachment.copy("incident",current.instance,'sys_email',current.sys_id); // Copy all attachemnts to email

var gr2 = new GlideRecord('sys_attachment'); // Delete attachemnts that were sent (u_sent = true)
gr2.addQuery('table_sys_id',current.sys_id);
gr2.addQuery('u_sent',true);
gr2.deleteMultiple();
markSent();
}


function markSent(){
var gr = new GlideRecord('sys_attachment'); //Mark the new attachments to be sent
gr.addQuery('table_sys_id',current.instance);
gr.addQuery('u_sent',false);
gr.query();
while(gr.next()) {
gr.u_sent = true; // mark so that we don't send again
gr.update();
}
}