- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-05-2018 01:32 AM
How to send latest attachments when added to the incident without sending the old one ?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-05-2018 01:56 AM
1. First Create a after insert business rule on sys_attachment table which triggers an event.
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".
3. The event triggers a notification.
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
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();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2021 01:25 PM
Hi
Could you please help? How could I get this working with Paris version?
Thanks
G
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-17-2021 06:59 AM
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().
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-05-2018 04:00 AM
test

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-05-2018 04:41 AM
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/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-18-2018 02:39 AM
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(',');