Copy currently inserted attachment

Petr Pastuszek1
Tera Contributor

Hello Team

When attachment is added to sc task we need to copy it to ritm.

but only currently inserted file/files,not those inserted yesterday and so on

 

any advice on script in BR on attachment table.

1 ACCEPTED SOLUTION

Hello @Petr Pastuszek1 ,

Please try with the modified code below,

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

    // Get the current task record
    var grTask = new GlideRecord('sc_task');
    if (grTask.get(current.table_sys_id)) {

        // Create a new sys_attachment record
        var grAttachment = new GlideRecord('sys_attachment');
        grAttachment.initialize();

        // Set values for the new attachment record
        grAttachment.setValue('file_name', current.file_name);
        grAttachment.setValue('content_type', current.content_type);
        grAttachment.setValue('size_bytes', current.size_bytes);
        grAttachment.setValue('table_name', 'sc_req_item');
        grAttachment.setValue('table_sys_id', grTask.request_item);

        // Insert the new attachment record
        var newAttachmentSysId = grAttachment.insert();

        // Copy the content stream from the original attachment to the new attachment
        var gsa = new GlideSysAttachment();
        gsa.writeContentStream(
            grAttachment,
            current.file_name,
            current.content_type,
            gsa.getContentStream(current.sys_id)
        );
        
        // Optionally, log the new attachment sys_id for debugging
        gs.info('New Attachment Sys ID: ' + newAttachmentSysId);
    }

})(current, previous);

View solution in original post

12 REPLIES 12

Siddhesh Wani1
Tera Guru

Hi Petr,

There is one ServiceNow function for the copy attachment you can try this 

'GlideSysAttachment.copy'

https://developer.servicenow.com/dev.do#!/reference/api/vancouver/server_legacy/GlideSysAttachmentGl...

To copy the only 1st attachment you can try with if loop, so it will take only 1st attachment.

If you find the solution useful Please mark this response as correct 

Thanks and regard,

Siddhesh

Aniket Chavan
Tera Sage
Tera Sage

Hello @Petr Pastuszek1 ,

Please give a try to the code below and see how it works for you,

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

    // Get the current task record
    var taskRec = new GlideRecord('sc_task');
    if (taskRec.get(current.table_sys_id)) {

        // Query for the most recent attachment with the current timestamp
        var attachment = new GlideRecord('sys_attachment');
        attachment.addQuery('table_sys_id', current.table_sys_id);
        attachment.orderByDesc('sys_created_on');
        attachment.setLimit(1); // Limit the result set to 1 record
        attachment.query();

        if (attachment.next()) {

            // Get the associated RITM record
            var ritmRec = new GlideRecord('sc_req_item');
            if (ritmRec.get(taskRec.request_item)) {

                // Copy the latest attachment to the RITM
                var gsa = new GlideSysAttachment();
                gsa.writeContentStream(
                    ritmRec,
                    attachment.file_name,
                    attachment.content_type,
                    gsa.getContentStream(attachment.sys_id)
                );
            }
        }
    }

})(current, previous);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks & Regards,

Aniket.

Thank you!!!
I will give it try today in few hours,not at pc now.

user can insert 1 or more attachments at one time and those I need to duplicate to rirm always.

when I tried yesterday glidesysattachcopy function from ServiceNow,it always copy attachments uploaded in past whcih is not what we want. Only those files uploaded at current time.

 

/Petr

/Petr

Hello @Petr Pastuszek1 ,

Okay then please give a try to the script below, which will allow you to copy multiple attachments for the current time

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

    // Get the current task record
    var taskRec = new GlideRecord('sc_task');
    if (taskRec.get(current.table_sys_id)) {

        // Query for attachments with the current timestamp
        var attachment = new GlideRecord('sys_attachment');
        attachment.addQuery('table_sys_id', current.table_sys_id);
        attachment.addQuery('sys_created_on', '>=', gs.daysAgo(0)); // Attachments created today
        attachment.orderByDesc('sys_created_on');
        attachment.query();

        // Loop through the attachments
        while (attachment.next()) {

            // Get the associated RITM record
            var ritmRec = new GlideRecord('sc_req_item');
            if (ritmRec.get(taskRec.request_item)) {

                // Copy each attachment to the RITM
                var gsa = new GlideSysAttachment();
                gsa.writeContentStream(
                    ritmRec,
                    attachment.file_name,
                    attachment.content_type,
                    gsa.getContentStream(attachment.sys_id)
                );
            }
        }
    }

})(current, previous);