I want to add attachment from email to target table (directly to paper-clip) using inbound or script include?

chsandi
Kilo Contributor

I want to add attachment from email to target table (directly to paper-clip) using inbound or script include? can yoy plz suggest on this. Its an urgent requirement

7 REPLIES 7

Gurpreet07
Mega Sage

OOB It should copy the attachments to target record if you have sleeted the table. If its not working then use script.

GlideSysAttachment.copy('sourcetable','sys_id','destinationtable','sys_id');

https://docs.servicenow.com/bundle/kingston-servicenow-platform/page/script/useful-scripts/reference/r_UsefulAttachmentScripts.html

Hi Gurpreet,

Thanks for the reply.

Requirement is not to attach from record to record.

need to attach from mail to record.

Thanks in advance

 

currently i am using this code

Script include

var SERReadingEmail = Class.create();
SERReadingEmail.prototype = {
checkMail: function(){
var gr = new GlideRecord('incident');
gr.description = email.body_text;
gr.short_description = email.subject;
gr.contact_type = "email";
gr.caller_id= gs.getUserID();
gr.u_affected_contact = gs.getUserID();
gr.opened_by= email.user_id;
gr.no_attachment=email.attach;

var supportKeywords = ["support", "assistance", "access"];
//var incidentKeywords = ["incident", "assistance", "ticket"];
var emailBody = email.body_text.toLowerCase();
var emailSubject = email.subject.toLowerCase();

for (var j=0; j< supportKeywords.length; j++) {

if((emailBody.indexOf(supportKeywords[j]) > -1 )|| (emailSubject.indexOf(supportKeywords[j]) > -1)){
//gr.insert();
Recipients = "";
}


}
},
type: 'SERReadingEmail'
};

 

 

Inbound action

 

//var keywords = new SERReadingEmail(current).checkMail();


var gr2 = new GlideRecord('sys_user');
//gr2.addQuery('user_name',gs.getUserID());
gr2.query();
if(gr2.next())
{
var diocese = gr2.department.name.getDisplayValue();
if(gs.getUserID().isMemberOf(diocese="Customer Support"))
{
var keywords = new SERReadingEmail(current).checkMail();
}
else
{
gs.log('state ignored');
//email.state = "ignored";
var gr = new GlideRecord('sys_email');
gr.query();
if (gr.next()) {
gs.log('state entered');
//gr.mailbox.name = "skipped";
// gr.setDisplayValue('state')= "ignored";
gr.setValue('state','ignored');
// gr.type = "received-ignored";
//gr.state = "ignored";
gs.log(" state>>>" + gr.state);
gs.log(" type>>>" + gr.type);
gs.log(" mailbox>>>" + gr.mailbox);
gr.update();

}
}
}

/*if (sys_email.hasAttachments()){
var att = new GlideRecord("sys_attachment");
att.addEncodedQuery("table_name=sys_email^table_sys_id=" + sys_email.getValue("sys_id"));
att.query();
while (att.next()){
att.table_name = "incident";
att.table_sys_id = gr.getValue("sys_id");
att.update();
}
}*/

 

here i have to add the code for attachments either in inbound or script include

Hi,

The fact is email itself a record in servicenow and you need to copy attachment form email record to target incident record. So write below statement in inbound action right after statement     var keywords = new SERReadingEmail(current).checkMail();

GlideSysAttachment.copy('sys_email',email,'incident',current);

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

 

We can use available api from service now i.e. 

GlideSysAttachment.copy('sourcetable','sys_id','destinationtable','sys_id');


Here source table will be emails table and its record sys id and destination table will be Record which you are creating along with its sys id.

Thanks,
Ashutosh Munot

Mark answer as correct or helpful.