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.

all old attachments are coping on every new attachment update in SC_TASK to RITM.

Thanvik1
Tera Contributor

Hi All,

 

I am using the below script to copy the attachments from sc task to RITM, if any fulfiller attachdocument to sctask that need to be shown to customer (to copy to RTIM).  but on every attachment upload all other attachments in SC TAsk is copying to RITM including new one.

-------------------------------------------------------------------------------------------------

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

    var taskRec = new GlideRecord('sc_task');

taskRec.addQuery('sys_id', current.table_sys_id);

taskRec.query();

if(taskRec.next()){

var ritmRec = new GlideRecord('sc_req_item');

ritmRec.addQuery('sys_id',taskRec.request_item);

ritmRec.query();

if(ritmRec.next()){

GlideSysAttachment.copy('sc_task', current.table_sys_id, 'sc_req_item', ritmRec.sys_id);

}

}

})(current, previous);
3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@Thanvik1 

that's OOTB behavior when you use copy() method

Approach 1:

-> delete files on target before copying and then use copy() method

I shared approach few years ago

Prevent duplicate attachments when copying 

OR

Approach 2:

-> copy only specific file

I shared approach for this as well 6-7 years ago

Is there any way to copy single attachment from multiple attachments ?? 

💡 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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Brad Bowman
Kilo Patron
Kilo Patron

It is best practice to avoid copying attachments whenever possible, due to adding unnecessary database bloat, and the confusion this will cause when the attachments become out of sync between the two records because one is deleted in one place, but not the other...

 

Consider an alternate approach.  You can create a Related List to add to the RITM form that shows all of the attachments on all of the Catalog Tasks related to this RITM.  To do this, first create a new Relationship that looks like this:

 

BradBowman_0-1763641859686.png

You can name it whatever you want to appear on the tab on the RITM record.  Here is the script:

function refineQuery(current, parent) {
	var childArr = [];
	var sctask = new GlideRecord('sc_task');
	sctask.addQuery('request_item', parent.sys_id);
	sctask.query();
	while (sctask.next()) {
		childArr.push(sctask.sys_id.toString());
	}
	current.addQuery('table_sys_id', 'IN', childArr);
})(current, parent);

On an RITM record, right-click and Configure > Related Lists to add your new Relationship name.

BradBowman_1-1763642173001.png

 

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Thanvik1 

copying files on records will increase attachment table size

instead you can show them via related list

see this blog and enhance

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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader