- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā11-29-2023 01:41 PM
Hello Experts,
We want to copy attachments from "sysapproval_approver" table to change request table.
i have created below GlideSysAttachment.copy code is working fine.
but issue is whenever record is inserted or updated copying all attachments every time.
Requirement1.If approver attachment should copy only one time.
Please support me on this
Table =sysapproval_approver
After insert or update BR.
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var chg = new GlideRecord('change_request');
if(chg.get(current.sysapproval)){
var attachment = new GlideSysAttachment();
attachment.copy("sysapproval_approver",current.sys_id,"change_request",chg.sys_id);
}
})(current, previous
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā11-30-2023 11:25 PM
@akin9 condition is correct. Please try with below code.
var sy = new GlideRecord('sysapproval_approver');
sy.get(current.table_sys_id);
var changeSysID = sy.sysapproval;
var ch = new GlideRecord('change_request');
ch.get(changeSysID);
var attachment = new GlideSysAttachment();
attachment.deleteAll(ch);
attachment.copy("sysapproval_approver", current.table_sys_id, "change_request", changeSysID);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā12-01-2023 09:57 AM
You sure, you want to delete the existing attachment from change request š®? Below script is doing that.
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā11-30-2023 06:50 PM
Hi @akin9 ,
I trust you are doing great.
Here's an enhanced version of your script:
(function executeRule(current, previous /*null when async*/) {
// Check if the attachment has already been copied
if (current.u_attachment_copied) {
return; // Exit if already copied
}
var chg = new GlideRecord('change_request');
if (chg.get(current.sysapproval)) {
var attachment = new GlideSysAttachment();
attachment.copy("sysapproval_approver", current.sys_id, "change_request", chg.sys_id);
// Set a flag to indicate the attachment has been copied
current.u_attachment_copied = true;
current.update();
}
})(current, previous);
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā12-01-2023 12:51 AM
Hello @Amit Gujarathi
Thanks for the Quick reply!
I have tried the above but no luck.
Table name -sysapproval_approver
After insert,
Its not copying the attchment.
After insert/update,
Its copying all the existing attchments once again.
pls check
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā12-01-2023 01:16 AM
@akin9 have you tried above code provided by me?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā12-01-2023 01:56 AM
business rule should be after insert on sys_attachment
Condition: current.table_name == 'sysapproval_approver'
Script:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
// delete all attachments to the source record
// then copy all
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader