Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

copy attchment ritm to sctask

raju Chinna
Tera Contributor

when any attachment is attached to requested item .it should copy to sctask 

3 REPLIES 3

Rafael Batistot
Mega Patron

Hi @raju Chinna 

 

May you use an After Insert Business Rule on sys_attachment to copy attachments from the RITM (sc_req_item) to its related Catalog Task (sc_task).

 

Table: sys_attachment

When: After

Insert: True

 

(function executeRule(current, previous) {
var ritmId = current.table_sys_id.toString();

var ritm = new GlideRecord('sc_req_item');
if (!ritm.get(ritmId)) {
return;
}

var task = new GlideRecord('sc_task');
task.addQuery('request_item', ritmId);
task.query();

while (task.next()) {
var attachment = new GlideSysAttachment();
attachment.copy(
'sc_req_item',
ritmId,
'sc_task',
task.getUniqueValue()
);
}
})(current, previous);

 

If this response was helpful, please mark it as Helpful and, if applicable, as Correct.
This helps other users find accurate and useful information more easily

Ankur Bawiskar
Tera Patron

@raju Chinna 

won't this increase the attachment table as you are copying same file and keeping on both the records

instead you can use this feature and show the attachments on SC Task related list

TNT: "Related Attachments" Related List 

💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ehab Pilloor
Giga Sage

Hi @raju Chinna,

 

Your query would increase storage consumption for many file duplicates.

 

I would suggest you keep the attachments on RITM and access it via Related List in CTask. 

Create a relationship record 'Related Attachments', Apply on Task table, queries from sys_attachment table.

 

(function refineQuery(current, parent) {

    // Extended task tables to include attachments from.
    // Add more tables here as needed.
    var taskTables = ['sc_req_item', 'sc_task'];
    var ids = [];

    function applyAttachmentQuery(ids) {

        var qc = current.addQuery('table_name', 'ENDSWITH', taskTables[0]);

        for (var i = 1; i < taskTables.length; i++) {
            qc.addOrCondition('table_name', 'ENDSWITH', taskTables[i]);
        }

        current.addQuery('table_sys_id', 'IN', ids.join(','));
    }


    if (parent.getTableName() == 'sc_req_item') {

        ids = [];
        ids.push(parent.getUniqueValue());

        // Child Catalog Tasks
        var task = new GlideRecord('sc_task');
        task.addQuery('request_item', parent.getUniqueValue());
        task.query();

        while (task.next()) {
            ids.push(task.getUniqueValue());
        }

        // RITM + Catalog Task attachments,
        // including ZZ_YY-prefixed variable attachments
        applyAttachmentQuery(ids);


    } else if (parent.getTableName() == 'sc_task') {

        ids = [];
        ids.push(parent.getUniqueValue());

        // Parent RITM
        if (parent.request_item) {
            ids.push(parent.request_item.toString());
        }

        // Sibling Catalog Tasks
        var sibling = new GlideRecord('sc_task');
        sibling.addQuery('request_item', parent.request_item);
        sibling.addQuery('sys_id', '!=', parent.getUniqueValue());
        sibling.query();

        while (sibling.next()) {
            ids.push(sibling.getUniqueValue());
        }

        // Current task + RITM + sibling tasks,
        // including ZZ_YY-prefixed variable attachments
        applyAttachmentQuery(ids);


    } else {

        current.addQuery('table_sys_id', parent.getUniqueValue());

    }

    // Newest attachments first
    current.orderByDesc('sys_created_on');

})(current, parent);

 

I have implemented this for a customer, also I use a BR to move attachments from CTask to RITM so that it is visible in all child tasks after particular CTask is Closed.

BR runs on async update, State changes to closed complete.

(function executeRule(current, previous) {


    var attachment = new GlideRecord('sys_attachment');
    attachment.addQuery('table_name', 'sc_task');
    attachment.addQuery('table_sys_id', current.getUniqueValue());
    attachment.query();

    while (attachment.next()) {

        attachment.setValue('table_name', 'sc_req_item');
        attachment.setValue('table_sys_id', current.getValue('request_item'));
		attachment.setWorkflow(false);
        attachment.update();
    }

})(current, previous);

 

Please Accept this response as Solution if it assisted you with your question & Mark this response as Helpful.

 

Kind Regards,

Ehab Pilloor