How to copy attachment from service request to sysapproval record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 03:50 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 03:59 AM
Hi,
This is the code i have used in sys_attachment table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 04:08 AM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 04:12 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 04:55 AM
You can change the trigger condition or filter by file name or something.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 04:07 AM
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