Copy specific Attachment from Request Items to Approvals and vice versa
Tags: servicenow, javascript
Published: 2021-05-26 13:43:18

The requirement was to copy the attachment from a Request Item to an Approval record or vice versa. The OOTB API GlideSysAttachment copies all the attachments from one record to the other every time, this was not the desired outcome. 

Every attachment upload fires an event called 'attachment.uploaded' which we can hook on to via a script action. Following code was used, which is inspired by SNPros:

Script Action:
```javascript
// condition-> event.parm1.toString() == 'sc_req_item' || event.parm1.toString() == 'sysapproval_approver'


if (event.parm1.toString() == 'sc_req_item') {

    var reqItemGR = new GlideRecord('sc_req_item');
    reqItemGR.get(event.parm2.toString());

    var approvalGR = new GlideRecord('sysapproval_approver');
    var apprOr = approvalGR.addQuery('sysapproval', event.parm2.toString());
    apprOr.addOrCondition('sysapproval', reqItemGR.request.getValue());
    approvalGR.query()
    while (approvalGR.next()) {
        new attachmentUtils().copySpecificAttachment(current.sys_id.getValue(), 'sysapproval_approver', approvalGR.sys_id.getValue())
    }


} else {
    var approvalGR = new GlideRecord('sysapproval_approver');
    if (approvalGR.get(event.parm2.toString())) {

        if (approvalGR.sysapproval.sys_class_name == 'sc_request') {

            var reqItemGR = new GlideRecord('sc_req_item');
            reqItemGR.addQuery('request', approvalGR.sysapproval.getValue());
            reqItemGR.addQuery('active', true);
            reqItemGR.query();
            while (reqItemGR.next()) {
                new attachmentUtils().copySpecificAttachment(current.sys_id.getValue(), 'sc_req_item', reqItemGR.sys_id.getValue())

            }
        } else {
            new attachmentUtils().copySpecificAttachment(current.sys_id.getValue(), 'sc_req_item', approvalGR.sys_id.getValue())

        }
    }

}

```

Script Include 'attachmentUtils'
```javascript
var attachmentUtils = Class.create();
attachmentUtils.prototype = {
    initialize: function () {},

    copySpecificAttachment: function (attachment_id, targetTable, targetSysId) {
        var attRecord = new GlideRecord('sys_attachment');
        if (attRecord.get(attachment_id)) {
            var sourceAttId = attRecord.getValue('sys_id');

            var newAttRecord = this._copyRecord(attRecord);
            newAttRecord.setValue('table_name', targetTable);
            newAttRecord.setValue('table_sys_id', targetSysId);
            newAttRecord.update()

            var attDateRecord = new GlideRecord('sys_attachment_doc');
            attDateRecord.addQuery('sys_attachment', attRecord.getValue('sys_id'));
            attDateRecord.query();

            while (attDataRecord.next()) {
                newDocRecord = this._copyRecord(attDataRecord);
                newDocRecord.setValue('sys_attachment', newAttRecord.getValue('sys_id'));
                newDocRecord.update();
            }

        }

    }

    _copyRecord: function (record) {
        var recordElement;
        var recordElementName;
        var recordTable = record.getTableName();
        var recordFields = record.getFields();
        var newRecord = new GlideRecord(recordTable);
        newRecord.initialize();
        for (var i = 0; i < recordFields.size(); i++) {
            recordElement = recordFields.get(i);
            if (recordElement.getName() != 'sys_id' && recordElement.getName() != 'number') {
                recordElementName = recordElement.getName();
                newRecord.setValue(recordElementName, record.getValue(recordElementName));
            }
        }
        newRecord.insert();
        return newRecord;
    }
}
type: 'attachmentUtils'
};

```