- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
05-22-2022 11:51 PM
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?
Solved! Go to Solution.
- Labels:
 - 
						
							
		
			Incident Management
 
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
05-23-2022 12:02 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
05-22-2022 11:54 PM
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
Regards,
Musab
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
05-23-2022 12:00 AM
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 🙂
Aman Kumar
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
05-23-2022 12:02 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
