Copy An attachment from sys_attachment table to HR approval table

swaghosh
Tera Expert

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: 

  var gr1 = new GlideRecord('sys_attachment');
    var rec = gr1.get('table_sys_id', current.sysapproval);
    if (rec) {
        GlideSysAttachment().copy('sys_attachment', rec.sys_id, 'sysapproval_approver', current.sys_id);
        current.update();
    }
Kindly correct me what is wrong in this script

 

4 REPLIES 4

Anand Kumar P
Giga Patron
Giga Patron

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

Musab Rasheed
Tera Sage
Tera Sage

Check below and try it.

https://www.youtube.com/watch?v=bA-lvIFrkcU

https://www.youtube.com/watch?v=7ttCKAruClk

Please hit like and mark my response as correct if that helps
Regards,
Musab
servicenow #servicenowdeveloper GlideSysAttachment | Copy Attachment | ServiceNow Today's Requirements: Create a UI action on task table calls Copy attachment to RITM record. var attach = new GlideSysAttachment(); var st = ...
In this video, I've explained that how to copy attachments from one record to another record in servicenow. If you like my effort please like this video and subscribe my channel.

Iraj Shaikh
Mega Sage
Mega Sage

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.

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.