- 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
‎10-05-2018 01:41 AM
Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-05-2018 04:28 AM
yes I need only latest attachments in the email.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-05-2018 04:32 AM
you need to use mail script in your notification.
refer the thread below.
- 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();
}
}