How to copy attachment from service request to sysapproval record

Arun91
Tera Contributor

Hi,

I have order guide and using variable type as attachment. After submitting the request based on number of RITM attachment is also attaching in the sys_attachment table. Now I want to copy only one attachment from service request to approval record. How we can do this. Could anyone help me on this.

14 REPLIES 14

Hi,

This is the code i have used in sys_attachment table

var recordId = current.table_sys_id;
var tableName = current.table_name;

var source=new GlideSysAttachment();
var source_content=source.getContentStream(current.sys_id);

var targetGlideRecord = new GlideRecord('sc_request');
targetGlideRecord.addQuery('parent',recordId);
targetGlideRecord.query();
while (targetGlideRecord.next()){
var fileName = current.file_name;
var contentType = current.content_type;
var sourceAttachmentSysId = current.getValue('sys_id');
var targetId=targetGlideRecord.sys_id;
var targetTbl=targetGlideRecord.getTableName();

var attachment = new GlideSysAttachment();
attachment.writeContentStream(targetGlideRecord,fileName,contentType,source_content);
}

You need to copy attachment , try this

var recordId = current.table_sys_id;
var tableName = current.table_name;
var source=new GlideSysAttachment();
var targetGlideRecord = new GlideRecord('sc_request');
targetGlideRecord.addQuery('parent',recordId);
targetGlideRecord.query();
while (targetGlideRecord.next()){
var attachment = new GlideSysAttachment();
attachment.copy(tableName , recordId , 'sc_request', targetGlideRecord.sys_id);
}
-Anurag

Hi,

It is copying all the attachment which means duplicate attachment also I just need only one attachment.

For example:

When I am uploading the attachment 1 Service request is created and 3 RITM's are created. Now 3 attachments are added to the sys_attachment table. But I need to copy only one attachment among the 3. 

You can change the trigger condition or filter by file name or something. 

-Anurag

Aniket Chavan
Tera Sage
Tera Sage

Hello @Arun91 ,

Please give a try to the updated code below and see how it works for you.

var sourceAttachmentSysId = current.sys_id;
var fileName = current.file_name;
var contentType = current.content_type;

var targetGlideRecord = new GlideRecord('sc_request');
targetGlideRecord.addQuery('parent', sourceAttachmentSysId); // Assuming 'parent' is the correct field for the relationship
targetGlideRecord.query();

while (targetGlideRecord.next()) {
    var targetId = targetGlideRecord.sys_id;
    var targetTbl = targetGlideRecord.getTableName();

    var source = new GlideSysAttachment();
    var sourceContent = source.getContentStream(sourceAttachmentSysId);

    var attachment = new GlideSysAttachment();
    attachment.writeContentStream(targetGlideRecord, fileName, contentType, sourceContent);
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket