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

Yes jake, It will not copy attachments to sys_email table. To copy I ran other business rule on sys_email table and used other BR to send parameters to Event.



Before - Insert business rule on sys_email:



var gr = new GlideRecord('incident');


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


  gr.query();


  if(gr.next()){


  gs.log('hi2');


GlideSysAttachment.copy('incident', gr.sys_id, 'sys_email',current.sys_id);


            }



I am able to successfully copy attachments from incident table to sys_email table and send email notification with all attachments by using attachment.uploaded Event. But the problem is I want to send only newly attached attachment. Email notification is triggered when new attachment is uploaded. So I that only that attachment to be sent with email.


Note: Also tried include attachment checkbox.



Is there any way to select the required attachment by using mail script? Any help with this would be appreciated.



Thank you.


Hi John,



One solution here would be to store the sent Attachment record sys_id values in a comma separated String field on the related record (e.g Incident). Every time you send an email with 1 or more attachments, you would append those sys_ids to this field. You will not be able to use the same "GlideSysAttachment" Class as before, because this copies all attachments. Instead, you would need to write a new script using GlideRecord Class to copy the applicable attachments from the source record to the sys_email record. The logic would go to add each attachment one by one, but skip any that are found in the comma separated list of previously sent attachments.



Regards,


Jake