copy attachment from RITM to approval

akin9
Tera Contributor

Hello Experts

We are trying to copy attachments from RITM to sysapproval table.

I have created below BR but unfortunately not working pls correct me,

 

Table  :sc_req_item

When : After and Insert 

Filter condition : 

Item is "X"

Script : 

 

(function executeRule(current, previous /*null when async*/ ) {
    attachApproval();

    function attachApproval() {
        var gr = new GlideRecord('sysapproval_approver');
        gr.addQuery('sys_id', current.sysapproval);
        gr.query();
        while (gr.next()) {
            GlideSysAttachment.copy("sysapproval_approver", gr.sys_id, "sc_req_item", current.sys_id);
        }
    }
})(current, previous);

 

8 REPLIES 8

Clara Lemos
Mega Sage
Mega Sage

Hi @akin9 ,

 

You can make the following changes to your code :

(function executeRule(current, previous /*null when async*/ ) {
    attachApproval();

    function attachApproval() {
        var gr = new GlideRecord('sysapproval_approver');
        gr.addQuery('sysapproval', current.sys_id);
        gr.query();
        while (gr.next()) {;
            GlideSysAttachment.copy("sc_req_item", current.sys_id, "sysapproval_approver", gr.sys_id);
        }
    }
})(current, previous);

 

But notice, that this business rule will only run if you update/insert a RITM record. If for example, you have a RITM and add an Attachment but doesn't make any modification on the RITM form the BR will not run. 

 

If this is an issue, you can create a Business Rule on the sys_attachment table.

 

If that helps please mark my answer as correct / helpful!
And if further help is needed please let me know

Cheers

 

 

Hello @Clara Lemos ,

Thank you for the quick reply!

Above code is working fine , but whenever RITM is updated .

Can you help me on if attachment is attached it needs to be copied.Thanks!

This is another reason you don't want to copy attachments.  You are creating more technical debt with now at least two Business Rules that don't need to exist.

Brad Bowman
Kilo Patron
Kilo Patron

First check the When to run - this Business Rule will only trigger when (after) the RITM record is created - so only if an attachment is added to the request form.  You could add a log statement inside the function to be certain it is triggering.  Your GlideSysAttachment parameters are backwards from your description - as written this would copy an attachment from and approval record to a requested item record

https://docs.servicenow.com/bundle/vancouver-api-reference/page/app-store/dev_portal/API_reference/G... 

 

Copying attachments should be avoided wherever possible as this unnecessarily bloats the database, and is not kept up to date when the attachment(s) change.  A better approach is to create a related list and add it to your Approval record so that the current attachments of the record being approved are always displayed.  Create a relationship that looks like this

BradBowman_0-1700051959867.png

 

BradBowman_1-1700052018239.png