Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

copy attachment variable from catalog item to any table fields

sanjeet1245
Tera Guru

I have a catalog item in ServiceNow with an attachment field named image. I also have a target table with an attachment field named picture. How can I automatically copy any attachment added to the image field of the catalog item to the picture field of the corresponding record in the target table using a workflow and GlideRecord in ServiceNow
how can i achieve this requirement please help ?

3 REPLIES 3

Slava Savitsky
Giga Sage

Copying attachments is possible with GlideSysAttachment API. Here is an example:

var attachment = new GlideSysAttachment();
attachment.copy('source_table_name', 'source_record_id', 'target_table_name', 'target_record_id');

 

Note that it copies all attachments from the source record to the target record. Unfortunately, GlideSysAttachment has no method for copying a single attachment.

Dnyaneshwaree
Mega Sage

Hello @sanjeet1245 ,

Create a new After Insert and Update Business Rule on your required catalog item table:

 

 

(function executeRule(current, previous /*null when async*/) {
    // Define the target table and its attachment field
    var targetTable = 'target_table'; // Replace with your target table name
    var targetField = 'picture'; // Replace with your target field name

    // Retrieve attachments from the catalog item
    var attachmentGR = new GlideRecord('sys_attachment');
    attachmentGR.addQuery('table_sys_id', current.sys_id);
    attachmentGR.addQuery('table_name', current.getTableName());
    attachmentGR.query();

    while (attachmentGR.next()) {
        // Read the attachment data
        var attachmentData = new GlideSysAttachment().getBytes(attachmentGR);

        // Create a new attachment for the target record
        var targetRecordGR = new GlideRecord(targetTable);
        targetRecordGR.get(current.target_record_sys_id); // Adjust this line to fetch your target record

        new GlideSysAttachment().write(targetTable, targetRecordGR.sys_id, attachmentGR.file_name, attachmentData);
    }
})(current, previous);



 

Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

Dnyaneshwaree Satpute
Tera Guru

sanjeet1245
Tera Guru

I have created a catalog item with an attachment field variable. When I upload an attachment, I want it to be added to an image type field in a table. I've written a workflow run script for this, but when records are inserted, the attachment isn't being added to the image field of that table."

 

 

    // Querying sys_attachment records related to the current item
    var gr = new GlideRecord("sys_attachment");
    gr.addQuery("table_name", "ZZ_YY" + 'sc_req_item');
    gr.addQuery("table_sys_id", current.sys_id);
    gr.query();

  
    if (gr.next()) {
        
        gr.table_name = "sc_req_item";
        gr.update();

        // Copy the attachment to the 'picture' field in abcd table
        new GlideSysAttachment().copy( "sc_req_item", gr.sys_id, 'abcdtable', 'picture');
    }

 

 

@Ankur Bawiskar