Creating a mail script that pulls over an attachment on a record

Wayne Richmond
Tera Guru

I have an Email Client Template that populates the Email Client with text from records on a table [u_stationary_emails]. However, I've realised that the attachments on the records aren't being pulled across to the email client with the text. This is going to pose a huge problem if I can't get the attachment on the record to the email client because I'm using this as a sort of 'stationary/template reply'. Here's what happens in practice:

 

On my table [u_inbound_mail] I select a 'Stationary Reply' from a Reference field [referencing u_stationary_replies]

Press a button [UI Action] called 'Reply' which opens the Email Client in ServiceNow

The Email Client opens a Client Template

Within the template is the following mail script:

 

<mail_script>       
//get the sys id of the referring task   
var sys_id = current.sys_id;
//create a glide record to query the sys_email table for emails sent about this job   
var emailQuery = new GlideRecord("sys_email");   
emailQuery.addQuery("instance",sys_id);   
emailQuery.orderByDesc("sys_created_on");   
emailQuery.query();   
   
var qwer = current.u_stationary_reply.u_body; \\ THIS IS WHATS PULLING ACROSS THE TEXT
qwer += "\n";   
qwer += current.assignment_group.u_group_signature;
qwer += "\n";   
qwer += "\n _____________________________________________________";   
   
//Build the originating mail   
while(emailQuery.next());
{   
qwer += "\n To:";   
qwer += emailQuery.recipients;   
qwer += "\n From:";   
//qwer += emailQuery.user;   
qwer += current.u_sender;   
qwer += "\n Sent:";   
qwer += emailQuery.sys_created_on;   
qwer += "\n Subject:";   
qwer += emailQuery.subject;   
qwer += "\n _____________________________________________________";       
qwer += "\n\n"
qwer += emailQuery.body_text;   
qwer += "\n";   
}       
template.print(qwer);   
</mail_script>

 

So, does anyone know how I can pull the attachment(s) from the u_stationary_email records into this template?

Thanks

1 ACCEPTED SOLUTION

Hi Wayne,



Here's a potential way forward for you.



I don't have your configuration but have mocked up something similar by sending a mail directly from a Problem record (in your case it would be your u_inbound_mail table) and the mail pulling in attachment/s from a related Change record (could be your u_stationary_replies table).



Step 1. Setup a Business Rule (run before insert) for the sys_email table to fire when a new mail record is created.


Step 2. In the Business Rule Script window, insert a script like below



// Get Task Table and Record variables that fired the email - this is the Problem Table/Record in my mockup


var map = gs.action.getGlideURI().getMap();


var table = map.get('sysparm_table');


var sid = map.get('sysparm_sys_id');


     


// Query Task Table to locate Record that fired the email


var task = new GlideRecord(table);


task.addQuery('sys_id',sid);


task.query();


//   Set related variable to record which contains attachment - in my mockup this is the related Change Record 'rfc' field  


while(task.next()) {


related = task.rfc;


// Copy attachment/s from source record to sys_mail record - in my mockup, my source table is the change_request table


  GlideSysAttachment.copy('change_request', related, current.getTableName(), current.sys_id);


}


View solution in original post

11 REPLIES 11

marcguy
ServiceNow Employee
ServiceNow Employee

would links to the attachment work in the email? if so you could use the code from this thread:



Email Client - do you want to add the attachments that are on this record?


marcguy
ServiceNow Employee
ServiceNow Employee

The other way you could do it is to build a before BR on the sys_email table and add the attachments to that record by using the copyAttachment functionality, that would actually add the attachments as opposed to having links to them. you would of course need a way to identify and condition the BR to only run on these mails and be able to find the attachments, I'm sure you could bury some identifier in there though with your mail script.



Copy Attachments from Record to Record - ServiceNow Wiki


Thanks but it can't be a link.


Hi Wayne,



Here's a potential way forward for you.



I don't have your configuration but have mocked up something similar by sending a mail directly from a Problem record (in your case it would be your u_inbound_mail table) and the mail pulling in attachment/s from a related Change record (could be your u_stationary_replies table).



Step 1. Setup a Business Rule (run before insert) for the sys_email table to fire when a new mail record is created.


Step 2. In the Business Rule Script window, insert a script like below



// Get Task Table and Record variables that fired the email - this is the Problem Table/Record in my mockup


var map = gs.action.getGlideURI().getMap();


var table = map.get('sysparm_table');


var sid = map.get('sysparm_sys_id');


     


// Query Task Table to locate Record that fired the email


var task = new GlideRecord(table);


task.addQuery('sys_id',sid);


task.query();


//   Set related variable to record which contains attachment - in my mockup this is the related Change Record 'rfc' field  


while(task.next()) {


related = task.rfc;


// Copy attachment/s from source record to sys_mail record - in my mockup, my source table is the change_request table


  GlideSysAttachment.copy('change_request', related, current.getTableName(), current.sys_id);


}