I have to copy Attachments from HR case to HR task

Naga Ravindra R
Kilo Sage

HI team,

 

I have to copy attachments from HR case to HR task. I tried below code 

Table : sn_hr_core_case

After BRule with inssert and update.

 

code should be:

var sysID = current.sys_id;
var CopyAttach = new GlideRecord('sn_hr_core_task');
CopyAttach.addQuery('parent', current.sys_id);
CopyAttach.query();

while(CopyAttach.next()){
var attachments = new GlideSysAttachment();
var CopiedAttachments = attachments.copy('sn_hr_core_case', sysID, 'sn_hr_core_task', "Not sure what to be placed here" );
}

can you please help us?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Naga Ravindra Reddy 

you need to have after insert BR on sys_attachment table

Condition:

current.table_name == 'sn_hr_core_case'

Script:

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

	// Add your code here
	var sysID = current.table_sys_id;
	var CopyAttach = new GlideRecord('sn_hr_core_task');
	CopyAttach.addQuery('parent', sysID);
	CopyAttach.query();
	while(CopyAttach.next()){

		// first delete all files so that it won't make the duplicate files

		var gr = new GlideRecord("sys_attachment");
		gr.addQuery("table_sys_id", CopyAttach.getUniqueValue());
		gr.query();
		gr.deleteMultiple();

		// then copy
		var attachments = new GlideSysAttachment();
		attachments.copy('sn_hr_core_case', sysID, 'sn_hr_core_task', CopyAttach.getUniqueValue());
	}


})(current, previous);

OR

I would suggest use this instead of copying files as it might increase attachment table size

"Related Attachments" Related List

Regards
Ankur

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

View solution in original post

3 REPLIES 3

Musab Rasheed
Tera Sage
Tera Sage

Hello,

Try below

https://community.servicenow.com/community?id=community_question&sys_id=296847eddb1cdbc01dcaf3231f9619ba

https://community.servicenow.com/community?id=community_question&sys_id=b7bb64cfdbc13b44fff8a345ca96191a

Regards

Please hit like and mark my response as correct if that helps
Regards,
Musab

Aman Kumar S
Kilo Patron

You need to pass sys_id of the record where you want to add the attachment, so in your case it should be sys_id of your HR task record

Your 4th param should be

CopyAttach.getUniquevalue();

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

Best Regards
Aman Kumar

Ankur Bawiskar
Tera Patron
Tera Patron

@Naga Ravindra Reddy 

you need to have after insert BR on sys_attachment table

Condition:

current.table_name == 'sn_hr_core_case'

Script:

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

	// Add your code here
	var sysID = current.table_sys_id;
	var CopyAttach = new GlideRecord('sn_hr_core_task');
	CopyAttach.addQuery('parent', sysID);
	CopyAttach.query();
	while(CopyAttach.next()){

		// first delete all files so that it won't make the duplicate files

		var gr = new GlideRecord("sys_attachment");
		gr.addQuery("table_sys_id", CopyAttach.getUniqueValue());
		gr.query();
		gr.deleteMultiple();

		// then copy
		var attachments = new GlideSysAttachment();
		attachments.copy('sn_hr_core_case', sysID, 'sn_hr_core_task', CopyAttach.getUniqueValue());
	}


})(current, previous);

OR

I would suggest use this instead of copying files as it might increase attachment table size

"Related Attachments" Related List

Regards
Ankur

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