The Zurich release has arrived! Interested in new features and functionalities? Click here for more

attach only single attachment based on the name

Shobha N
Tera Contributor

Hi All,

im trying to retrive the attachment through emai script. im able to do it as a link but when i try attaching it , it will retrive all attchment  in the record instead of the one which i quuired for.

 

Here is the mail script please let me what's wrong

 

function printattachments(){
 
var mcase1 = new GlideRecord('u_mservice_case');
mcase1.query('sys_id',current.sys_id);
mcase1.query();
if(mcase1.next()){
mcase1.u_broken_status="Scrap approval requested";
mcase1.u_followup_status="0";
mcase1.update();
 
 
var sap = mcase1.u_sap_notification;
var gr =new GlideRecord('sys_attachment');
  gr.addEncodedQuery('file_nameLIKEScrapRpt^file_nameLIKE'+sap);
 gr.query();
if(gr.next()){
 
//this works gives the link
template.print('Attachment: <a href="http://'+gs.getProperty("instance_name")+'.service-now.com/sys_attachment.do?sys_id='+ gr.sys_id+'">'+ gr.file_name+'</a>');
 
//but i need it as attachment so tried 1 and 2 both are giving me all attchment in the record not the 1 record which i quired for
1) var attachment = new GlideSysAttachment();
var attachcontent = attachment.getContent(gr.sys_id);
attachment.write(current, name, gr.getValue('content_type'),attachcontent);
 
2)var content = new GlideSysAttachment().getContentStream(gr.sys_id);

                new GlideSysAttachment().writeContentStream(current, gr.getValue('file_name'), gr.getValue('content_type'), content);

 
 
 
 
1 ACCEPTED SOLUTION

@Shobha N 

then you need to use BR on sys_email table

script something like this so that it includes only 1 file. This has worked for me in past

 

AnkurBawiskar_0-1687838649206.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Shobha N 

email notification is on which table?

is it on u_mservice_case table?

if yes then you can use Include attachment checkbox and it will send the file on email and no scripting is required

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Shobha N
Tera Contributor

@Ankur Bawiskar , thanks for the reply, but i don't want all the attachment which is present on record. example i have 2 files on record file 1 and file 2, i would like to add only the file1 in my mail which contains sap notification number in it.

@Shobha N 

then you need to use BR on sys_email table

script something like this so that it includes only 1 file. This has worked for me in past

 

AnkurBawiskar_0-1687838649206.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Shobha N
Tera Contributor

@Ankur Bawiskar thank you for the help..