Copy An attachment from sys_attachment table to HR approval table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2024 05:43 AM
I am trying to create a business rule that copies an attachment from sys_attachment to HR approval(sysapproval_approver) table. But the below BR is copying it to the activity stream of HR approvalrecord. How can we attach it to HR approval record:
Type: After BR on Approval
BR script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2024 05:53 AM
Hi @swaghosh ,
Update below line
GlideSysAttachment().copy('sys_attachment', rec.sys_id, 'sysapproval_approver', current.sys_id);
to
GlideSysAttachment.copy('sys_attachment', rec.sys_id, 'sysapproval_approver', current.sys_id);
Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2024 06:03 AM
Check below and try it.
https://www.youtube.com/watch?v=bA-lvIFrkcU
https://www.youtube.com/watch?v=7ttCKAruClk
Regards,
Musab
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2024 06:46 AM
Hi @swaghosh
There are a few issues with the script you've provided.
Here's the corrected script:
(function executeRule(current, previous /*null when async*/) {
// Query the sys_attachment table for attachments related to the current approval record
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_sys_id', current.sys_id);
attachmentGR.query();
while (attachmentGR.next()) {
// Use the GlideSysAttachment API to copy the attachment
var gsa = new GlideSysAttachment();
gsa.copy(attachmentGR.getValue('table_name'), attachmentGR.getUniqueValue(), 'sysapproval_approver', current.sysapproval.getUniqueValue());
}
})(current, previous);
Here's what was changed and why:
1. Removed the `current.update()` call: You should not call `current.update()` inside an After business rule as it can lead to recursion and performance issues. The current record is already being saved, and that's why the business rule is running.
2. Corrected the GlideSysAttachment instantiation: The `GlideSysAttachment` class should be instantiated using `new GlideSysAttachment()` rather than `GlideSysAttachment()`.
3. Changed the `get` method to `query`: Instead of using `get` to retrieve a single record, we use `query` to handle multiple attachments. The `get` method is typically used when you expect a single record and know the exact sys_id.
4. Loop through attachments: Since there could be multiple attachments, we loop through the result set of the `attachmentGR` GlideRecord.
5. Corrected the parameters for the `copy` method: The `copy` method takes the source table name, the source record's sys_id, the destination table name, and the destination record's sys_id. Make sure that `current.sysapproval` is the correct field that contains the sys_id of the HR approval record you want to copy the attachment to.
6. Removed the `rec` variable: The `rec` variable was unnecessary, and its usage was incorrect. We directly use the `attachmentGR` GlideRecord to access the attachment information.
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
01-15-2024 03:56 AM
Thanks for your response.
Its getting attached to the activity stream of the approval record. I need the attachment to be added in the approval record itself on the header menu of the record, not as an attachment in activity stream of the record.