Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Inbound Action email copy attachment from current email to child task

Dario_C
Mega Sage

Hi everyone ! 

I need for my inbound action email,

In practice i have a RITM where with a UI action  i create a Task on sc_task with parent a id of RITM , when an operator send a email from this TASK to a seller, this seller usually answer this email with a PDF attachments , when this email reach my inboud, this one create another Task with parent a Main Task. I NEED to copy a PDF attachment on new task when this one will create.

So everytime a guest answer to email come from MAIN Task, the inbound create a CHILD TASK and on this one i need to pass a pdf comes from the email.  But my code doesn't work ! Thank in advance 

 

 

 

 

current.caller_id = gs.getUserID();
current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
//current.short_description = email.subject;	
GlideSysAttachment.copy('sys_email','sys_email.sys_id','sc_task','sc_task.sys_id');


current.caller_id=gs.getUserID();
current.comments = "Ricevuto preventivo da Indirizzo mail : " + email.origemail;
/*******************************************************************/
/***HERE I CREATE A CHILD TASK  *****/
var gr = new GlideRecord('sc_task');
gr.initialize();	
//gr.get(current.sc_task);	
gr.requested_for = email.origemail;
gr.short_description = email.subject;
gr.parent = current.sys_id;   // give the new task a id of MAIN TASK
gr.insert();

var attachment = new GlideSysAttachment();

//GlideSysAttachment.copy('sc_task', current.sys_id, 'sc_task', gr.sys_id); IF I USE THIS, I COPY ALL ATTACHMENT ON CHILD TASK

var emailSysId = '';//sys id of the email received
var attachmentName = '';
var gr1 = new GlideRecord('sys_attachment');
gr1.addQuery('table_name','sys_email');
gr1.addQuery('table_sys_id',emailSysId);
gr1.query();

if(gr1.next()){
GlideSysAttachment.copy('sc_task', current.sys_id, 'sc_task', gr.sys_id);
}	

 

 

 

 

1 ACCEPTED SOLUTION

Dario_C
Mega Sage

I solved! If you need to copy the attachment to a child task created by the parent task via inbound, you need to do a Glide Record on the email table.

 

var attachment = new GlideSysAttachment();
var emailSysId = sys_email.getUniqueValue();
var attachmentName = '';
var gr1 = new GlideRecord('sys_email');
gr1.addQuery('istance',current.sys_id);
gr1.query();
var grSysId = gr1.sys_id;
GlideSysAttachment.copy('sys_email',emailSysId,'sc_task', gr.sys_id);

 

View solution in original post

3 REPLIES 3

Muhammad Khan
Mega Sage

As far as I know GlideSysAttachment.copy() will copy all attachments from source record to target record. So, try using writeContentStream() function. Something like below should work.

var gsa = new GlideSysAttachment();
gsa.writeContentStream(  // Write/Create new attachment record in Attachments table.
gr,  // Child task glide record object to which this attachment will be attached.
gr1.file_name,  // File name of the attachment to be attached.
gr1.content_type, // Content type of the attachment to be attached.
gsa.getContentStream(gr1.sys_id) // Write the content of the attachment.
);

Dear Muhammad,

Thanks for your reply !

I have tested it but doesn't work, from log i see the inbound doesnt take current id of email, i dont know how this is possibile...

Dario_C
Mega Sage

I solved! If you need to copy the attachment to a child task created by the parent task via inbound, you need to do a Glide Record on the email table.

 

var attachment = new GlideSysAttachment();
var emailSysId = sys_email.getUniqueValue();
var attachmentName = '';
var gr1 = new GlideRecord('sys_email');
gr1.addQuery('istance',current.sys_id);
gr1.query();
var grSysId = gr1.sys_id;
GlideSysAttachment.copy('sys_email',emailSysId,'sc_task', gr.sys_id);