Copy attachment from "sys_attachment" to scoped app table.

devashishku
Tera Contributor

Hi all,
I am stuck with a problem from past few days that, my all the attachments are stored in "sys_attachment" table and i want to add the same attachment to the record of "x_dmcso_oneview_question_answer" table. 

As, i have to integrate the feature in Ui Builder, so first testing on background script. But the attachment is not getting copied.

var sourceTable = 'sys_attachment';
        var sourceSysId = ''fa1702412bb0f250dcf1f865ce91bfdb";
        var targetTable = 'x_dmcso_oneview_question_answer';
        var targetSysId = '3305ff953b953610079a8c9aa4e45a2c';

        var gsa = new GlideSysAttachment();
        var recordId = gsa.copy(sourceTable, sourceSysId, targetTable, targetSysId);

When I run the script, it is saying 
devashishku_0-1764276474064.png

also checked with switching the scope to global, same message is getting displayed and record is not attached to the other table.

If there will be any solution related to this, please let me know. 

Thanks,
Devashish

 

2 ACCEPTED SOLUTIONS

Deepak Shaerma
Mega Sage
Mega Sage

Hi @devashishku 

 

GlideSysAttachment.copy('source_table', 'source_sys_id', ...) function is designed to copy all attachments from a specific record (like an Incident or a Task) to another record.

 

​also by setting sourceTable = 'sys_attachment', you are telling ServiceNow: "Go find a record inside the sys_attachment table with this ID, and copy any attachments that are attached TO that attachment." Since attachments don't usually have their own attachments, it finds nothing to copy.

If you have the specific sysid of the attachment then try the below script once:

 

var attachmentSysId = 'fa1702412bb0f250dcf1f865ce91bfdb'; // The ID of the file in sys_attachment

var targetTable = 'x_dmcso_oneview_question_answer';

var targetSysId = '3305ff953b953610079a8c9aa4e45a2c';

var grSourceAtt = new GlideRecord('sys_attachment');

if (grSourceAtt.get(attachmentSysId)) {

 

    //Get the target record where you want to put the file

    var grTarget = new GlideRecord(targetTable);

    if (grTarget.get(targetSysId)) {

        

        var gsa = new GlideSysAttachment();

        

        var content = gsa.getContent(grSourceAtt);

        

        //New attachment to the target table

        var newAttId = gsa.write(grTarget, grSourceAtt.file_name, grSourceAtt.content_type, content);

        

        gs.info('Success! New Attachment created with ID: ' + newAttId);

    } else {

        gs.error('Target record not found');

    }

} else {

    gs.error('Source attachment not found');

}

 

 

 

 

Happy to help! If this resolved your issue, kindly mark it as the correct answer and Helpful and close the thread 🔒 so others can benefit too.

Warm Regards,

Deepak Sharma

Community Rising Star 2025

View solution in original post

Sarthak Kashyap
Mega Sage

Hi @devashishku ,

 

Try to check below script once 

  GlideSysAttachment.copy() does NOT accept sys_attachment as the source table.
It expects the table of the record to which the attachment belongs — not the attachment table.

var attachmentSysId = "fa1702412bb0f250dcf1f865ce91bfdb";

    // Step 1: Get the attachment info
    var att = new GlideRecord("sys_attachment");
    if (!att.get(attachmentSysId)) {
        gs.error("Attachment not found");
        return;
    }

    var sourceTable = att.table_name + "";
    var sourceSysId = att.table_sys_id + "";

    var gsa = new GlideSysAttachment();
    var newAttachment = gsa.copy(
        sourceTable,
        sourceSysId,
        "x_dmcso_oneview_question_answer",
        "3305ff953b953610079a8c9aa4e45a2c"
    );

    gs.info("Copied attachment Sys ID: " + newAttachment);

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,
Sarthak

View solution in original post

2 REPLIES 2

Deepak Shaerma
Mega Sage
Mega Sage

Hi @devashishku 

 

GlideSysAttachment.copy('source_table', 'source_sys_id', ...) function is designed to copy all attachments from a specific record (like an Incident or a Task) to another record.

 

​also by setting sourceTable = 'sys_attachment', you are telling ServiceNow: "Go find a record inside the sys_attachment table with this ID, and copy any attachments that are attached TO that attachment." Since attachments don't usually have their own attachments, it finds nothing to copy.

If you have the specific sysid of the attachment then try the below script once:

 

var attachmentSysId = 'fa1702412bb0f250dcf1f865ce91bfdb'; // The ID of the file in sys_attachment

var targetTable = 'x_dmcso_oneview_question_answer';

var targetSysId = '3305ff953b953610079a8c9aa4e45a2c';

var grSourceAtt = new GlideRecord('sys_attachment');

if (grSourceAtt.get(attachmentSysId)) {

 

    //Get the target record where you want to put the file

    var grTarget = new GlideRecord(targetTable);

    if (grTarget.get(targetSysId)) {

        

        var gsa = new GlideSysAttachment();

        

        var content = gsa.getContent(grSourceAtt);

        

        //New attachment to the target table

        var newAttId = gsa.write(grTarget, grSourceAtt.file_name, grSourceAtt.content_type, content);

        

        gs.info('Success! New Attachment created with ID: ' + newAttId);

    } else {

        gs.error('Target record not found');

    }

} else {

    gs.error('Source attachment not found');

}

 

 

 

 

Happy to help! If this resolved your issue, kindly mark it as the correct answer and Helpful and close the thread 🔒 so others can benefit too.

Warm Regards,

Deepak Sharma

Community Rising Star 2025

Sarthak Kashyap
Mega Sage

Hi @devashishku ,

 

Try to check below script once 

  GlideSysAttachment.copy() does NOT accept sys_attachment as the source table.
It expects the table of the record to which the attachment belongs — not the attachment table.

var attachmentSysId = "fa1702412bb0f250dcf1f865ce91bfdb";

    // Step 1: Get the attachment info
    var att = new GlideRecord("sys_attachment");
    if (!att.get(attachmentSysId)) {
        gs.error("Attachment not found");
        return;
    }

    var sourceTable = att.table_name + "";
    var sourceSysId = att.table_sys_id + "";

    var gsa = new GlideSysAttachment();
    var newAttachment = gsa.copy(
        sourceTable,
        sourceSysId,
        "x_dmcso_oneview_question_answer",
        "3305ff953b953610079a8c9aa4e45a2c"
    );

    gs.info("Copied attachment Sys ID: " + newAttachment);

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,
Sarthak