How to copy attachment from one record to another record

mark141230
Tera Expert

Hi Everyone,

I have a requirement, the scenario is that upon insert the attachment in the Customer Service application that I have created will automatically copied to the incident record.

the case is that if an agent is about to submit a ticket from the customer service application, once he indentified that this is an incident it will automatically create an incident ticket, I was successful on that part but the problem is that the attachment is not included once the incident record is created. I have this code but it is not working, hope someone can help me.

Business Rule

Table: Customer Service

When to Run: After / Insert

Script:

(function executeRule(current, previous /*null when async*/) {

var ritm = new GlideRecord('x_glti_incident_ma_incident_management_table');

ritm.addQuery('x_glti_customer_se_customer_service_ticket',current.sys_id);

ritm.query();

while(ritm.next()){

GlideSysAttachment.copy('x_glti_customer_se_customer_service', current.sys_id, 'x_glti_incident_ma_incident_management_table', ritm.sys_id);

}

})(current, previous);

Many thanks in advance

10 REPLIES 10

Ujjawal Vishnoi
Mega Sage
Mega Sage

Hi Mark,



Replace the below lines with



while(ritm.next()){


GlideSysAttachment.copy('x_glti_customer_se_customer_service', current.sys_id, 'x_glti_incident_ma_incident_management_table', ritm.sys_id);


}



with



if(ritm.next()){


GlideSysAttachment.copy('x_glti_customer_se_customer_service', current.sys_id, 'x_glti_incident_ma_incident_management_table', ritm.sys_id);


}



and change the order of the BR to 5000.



Hope this helps



Regards


Ujjawal


Hi Ujjawal,




I tried it also but to no avail. many thanks


Rohini Peraval1
Tera Contributor

Hi,


The attachments of any application will get stored in sys_attachment table.


I think we need to glide sys_attachment table.



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


var sysAttachment = new GlideRecord('sys_attachment');


sysAttachment.addQuery('file_name',source.u_attachment);


sysAttachment.query();



if(sysAttachment.next()){


log.info("copy attachment " + sysAttachment.sys_id + " on knowledge article " + target.number + " - " + target.sys_id);


GlideSysAttachment.copy('sys_attachment', sysAttachment.sys_id, 'kb_knowledge', target.sys_id);


}



target.update();



PS - Please Mark as Helpful, if the reply does.


Hi Rohini,



Small correction: Attachment is not stored in sys_attachment table,,only the reference to the attachment and record is saved there, actual attachment is saved in sys_attachment_doc table, therefore just copying/duplicating the entry in sys_attachment is not the way to do it, use SN provided method to copy attachment.


-Anurag