Email notification when new attachment is added or updated

tejumes
Kilo Guru

I'm trying to send an email notification to assignment group in incident task record when new/update attachment is added.

Any idea?

"Include Attachments" option sends only on the update in the incident record.

Any help on how to trigger an email notification when attachment alone added?

1 ACCEPTED SOLUTION

humblecommitted
Kilo Guru

Hello Teju,



I have used a similar method for onboarding new users.   Here is what I had to do.



I wanted new users on the project to upload required security documents.   Once they uploaded documents, based on the conditions that their attachments that were uploaded to their profile contained the key words "HIPAA" and "DHS" would trigger an event (notice the event name that is created is "onboard.attachment").



off the event, an email notification and template would be shot out.



BR - User Form Attach Notify


var userrec = new GlideRecord("sys_user");


userrec.get(current.table_sys_id);




var today = new GlideDate;


var pattern = "HIPAA";




var result = current.file_name.match(pattern);


if (JSUtil.has(result) ) {


  gs.log("filename " + current.file_name + " for user " + userrec.name + " contains " + pattern);


  gs.eventQueue('onboard.attachment', current, gs.getUserDisplayName(), userrec.name);


}


else {


  pattern = "DHS";


      result = current.file_name.match(pattern);


      if (JSUtil.has(result) ) {


      gs.log("filename " + current.file_name + " for user " + userrec.name + " contains " + pattern);


      gs.eventQueue('onboard.attachment', current, gs.getUserDisplayName(), userrec.name);


      }


  else gs.log("filename " + current.file_name + " for user " + userrec.name + " not matched to a pattern");


}


//gs.log("user " + userrec.name);


//if (userrec.sys_created_on > gs.daysAgo(30)) {


// gs.log(" new user " + userrec.name + ", created " + userrec.sys_created_on + " just attached file " + current.file_name   );




//Queue the email send event    


//Pass in event name, record(not applicable here), custom parm1, and custom parm2    


//Custom parm1 is the count of incidents queried and can be referenced from the email notification that responds to the queued event by using '${event.parm1}' in the email notification record.    


//gs.eventQueue('onboard.attachment', current, gs.getUserDisplayName(), userrec.name);


//gs.getUserName()


//}


//else gs.log(" old user attachment, created   " + userrec.sys_created_on + " just attached file " + current.file_name   );



1.1.jpg


1.2.jpg



The Email Notification that fires off the event named "onboard.attachment" and the email template associated with the email notification.


1.3.jpg


1.4.jpg



Now based on your request you were wanting to apply a similar situation to an incident.   simply change the code that references the "sys_user" table in the GlideRecord to the incident table and the file names that are being searched for.   You could also look at this old code we have when we were not searching attachments based on file names.



var userrec = new GlideRecord("sys_user");


  1. userrec.get(current.table_sys_id);


var today = new GlideDate;



  1. gs.log("user " + userrec.name);

if (userrec.sys_created_on > gs.daysAgo(14)) {


                              gs.log(" new user " + userrec.name + ", created " + userrec.sys_created_on + " just attached file " + current.file_name   );



//Queue the email send event    


//Pass in event name, record(not applicable here), custom parm1, and custom parm2    


//Custom parm1 is the count of incidents queried and can be referenced from the email notification that responds to the queued event by using '${event.parm1}' in the email notification record.    


  1. gs.eventQueue('onboard.attachment', current, gs.getUserDisplayName(), userrec.name);

//gs.getUserName()


}


else gs.log(" old user attachment, created   " + userrec.sys_created_on + " just attached file " + current.file_name );





I hope this helps.   Please let me know should you have any questions.



Sincerely,



Orlando Galindo


View solution in original post

25 REPLIES 25

This is what i did BUT notification is not sent i checked in outbox and sent , it is not.



find_real_file.png



find_real_file.png





find_real_file.png


I have never used this event, just remember reading about it.


I would open an incident with ServiceNow to see why its not working.


On another approach you could also make the Email Notification trigger with any record insert on the Attachments (sys_attachment) table where the table_name = incident.


But to get field info from the incident you would need to create a GlideRecord to get the incident record with the table_sys_id field.


Thanks a lot William for checking this thread and given me a path towards the solution.


As suggested i can setup email notification trigger with any record on sys_attachement table But regarding to whom to send i cannot set assignment group here for this can you help me on create a GlideRecord to get the fields if possible?


Hi Sun,



Email Notification is triggering finally only when attachment is added BUT attachment is not added into the email. Below are the one i tried


Created a Business Rule as helped by mach



Table : sys_attachment


Script:


function onAfter(current, previous) {


  //This function will be automatically called when this rule is processed.


  attachmentNotify();


  checkAttachment();



  function checkAttachment() {


  var logged_user = gs.getUserDisplayName();


  //gs.log(logged_user);


  var addedfileLink ="[code]<a class='linked formlink' href='sys_attachment.do?sys_id="+current.sys_id+"'>" + "<font color='Blue'><b> " +current.file_name+"</b></font></a><a target='_blank' class='linked formlink' href='sys_attachment.do?sys_id=" + current.sys_id + "&view=true'>" + " " + "[view]"+"</a>[/code]";


  //gs.log(addedfileLink);



  // if attach is deleted


  if (current.operation() == 'delete') {


  var task = new GlideRecord('u_request_task');


  task.addQuery('sys_id', current.table_sys_id);


  task.query();


  if (task.next()) {


  task.comments = "Attachment " + "[code]<font color='DarkRed'><b>" + current.file_name + "</b></font>[/code]" + " has been removed from this record by " + logged_user;


  task.update();


  }


  }



  // if attach is added


  else if (current.operation() == 'insert') {


  var task1 = new GlideRecord('u_request_task');


  task1.addQuery('sys_id', current.table_sys_id);


  task1.query();


  if (task1.next()) {


  task1.comments = "Attachment " + addedfileLink + " " + " has been inserted in this record by " + logged_user;


  task1.update();


  }


  }


  }


  function attachmentNotify(){


  //   if (current.operation() == 'insert') {


  var gr = new GlideRecord('u_request_task');


  gr.addQuery('sys_id', current.table_sys_id);


  gr.query();


  if (gr.next()) {


  var attach = new GlideRecord('sys_attachment');


  attach.addQuery('table_name','u_request_task');


  attach.addQuery('table_sys_id',current.table_sys_id);


  attach.query();


  if (attach.next()) {



  var message = "Attachment " + current.file_name + " Deleted in " + gr.number;


  GlideSysAttachment.copy('u_request_task', current.sys_id, 'sys_email', current.sys_id);


  gs.eventQueue("attachment.uploaded",current,gr.assigned_to.email,gr.assignment_group);



  }


  }


  }


  }



Above one triggers a notification when any attachment is added but it is not having the attachment in the email . I selected "Include Attachments" for attachment.uploaded event as shown below



find_real_file.png




find_real_file.png



I checked the sys_email, for the latest transaction when attachment is added, still i couldn't find any attachment there.


Can you please help me with this , might be some problem with GlideSysAttachment.copy i have written above.


Please help!!


Can anyone suggest me on this. Notification should go on when new attachment is added and fpr all other updates notification should trigger notification without attachment. Please find the above thread for details.