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

gabor42
Kilo Contributor

Hi

Could you please help? How could I get this working with Paris version?

Thanks

G

Please let me know how to get this logic working for Scoped Application like Customer Service (Case). This is working fine for Incident but not on the scope and is there any alternative solution available in place of GlideSysAttachment.copy().

Santhoshi Jaini
Kilo Contributor

test

Harsh Vardhan
Giga Patron

mail script

 

just add in your notification

 

<mail_script>
attachLinks();
function attachLinks() {

//Check for any attachments and add attachment links if they exist
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id',current.sys_id);
gr.orderByDesc('sys_created_on');
gr.query();
if(gr.next()){
var attachLink = '<a href="' + gs.getProperty("glide.servlet.uri") + gs.generateURL(gr.getTableName(),gr.sys_id) + '">' + gr.file_name + '</a>';
template.print(attachLink + '\n');

}
}
</mail_script>

 

https://www.servicenowguru.com/scripting/send-email-notification-attachments/

mcconnellsj
Kilo Sage

Hi - great solution - what is happening in these lines of your final business rule?

 

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