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 04:18 AM
Duplicate attachment is added to the sys_attachment table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2024 04:22 AM
Hello @Arun91 ,
okay, also in the above thread you mentioned that "It is copying all the attachment which means duplicate attachment also you just need only one attachment." so how you are deciding among three exactly which attachment you want or if any one from those three attachment is fine for you?
can you clear this so that we can add the condition the code accordingly.
Thanks & Regards,
Aniket.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2024 04:27 AM
While creating a request I am attaching only one attachment. Once it is inserted into sys_attachment it is inserted as duplicate, which means same file name is getting duplicated n number of times. While copying all the files are copied to the approval record, which means same file is copied n number of times in approval record. I need only one attachment that need to copy from service request to sysapproval record.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2024 04:31 AM
Hello @Arun91 ,
Please give a try the below code and let me know how it works for you, so that we can make the changes in the code further accordingly,
var recordId = current.table_sys_id;
var tableName = current.table_name;
var targetGlideRecord = new GlideRecord('sc_request');
targetGlideRecord.addQuery('parent', recordId);
targetGlideRecord.query();
while (targetGlideRecord.next()) {
var targetId = targetGlideRecord.sys_id;
var attachment = new GlideSysAttachment();
// Select only the first attachment from the related RITM
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', 'sc_request');
attachmentGR.addQuery('table_sys_id', targetId);
attachmentGR.orderBy('sys_created_on'); // Order by creation time to get the first attachment
attachmentGR.setLimit(1); // Limit the result to one record
attachmentGR.query();
if (attachmentGR.next()) {
var sourceAttachmentSysId = attachmentGR.sys_id;
var fileName = attachmentGR.file_name;
var contentType = attachmentGR.content_type;
var sourceContent = attachment.getContentStream(sourceAttachmentSysId);
// Copy the selected attachment to the sysapproval record
attachment.writeContentStream('sysapproval_approver', recordId, 'sysapproval_approver', targetId, fileName, contentType, sourceContent);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2024 05:09 AM
Hi,
I tired creating the BR for sys_attachment table and used before method but attachments are not copying to approval record.