GlideSysAttachment().copy Not copying the attachment - HR Scoped application
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2022 08:26 AM
Hi Team,
I have created a after insert BR to insert a HR task and then copy a specific attachment from a Case to HR task .
The attachment is not getting copied to HR task. Please suggest
All the log messages are getting triggered correctly , but the attachment is not found on target record.
(function executeRule(current, previous /*null when async*/ ) {
gs.info("the BR triggerred for status");
var grCattch = new GlideRecord('sys_attachment');
grCattch.addQuery('table_name', 'sn_hr_core_case_workforce_admin');
grCattch.addQuery('table_sys_id', current.sys_id);
grCattch.addEncodedQuery('file_nameSTARTSWITHStatus Record Certificate');
grCattch.query();
if (grCattch.next()) {
var grCmnl = new GlideRecord('sn_hr_core_task');
grCmnl.initialize();
grCmnl.parent = current.sys_id;
grCmnl.assignment_group = '3506330cdbc92054e803f3551d9619b0';
grCmnl.state = '10';
grCmnl.short_description = "Validate Status Record";
grCmnl.insert();
gs.info("the record status is found " + 'attchsysid----' + grCattch.getUniqueValue() + 'attchtable---' + grCattch.getTableName() + 'tasktable----' + grCmnl.getTableName() + 'tasksysid---' + grCmnl.getUniqueValue());
new GlideSysAttachment().copy(grCattch.getTableName(), grCattch.getUniqueValue(), grCmnl.getTableName(), grCmnl.getUniqueValue());
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-24-2022 02:00 AM
you have to correct this line
(function executeRule(current, previous /*null when async*/ ) {
gs.info("the BR triggerred for status");
var grCattch = new GlideRecord('sys_attachment');
grCattch.addQuery('table_name', 'sn_hr_core_case_workforce_admin');
grCattch.addQuery('table_sys_id', current.sys_id);
grCattch.addEncodedQuery('file_nameSTARTSWITHStatus Record Certificate');
grCattch.query();
if (grCattch.next()) {
var grCmnl = new GlideRecord('sn_hr_core_task');
grCmnl.initialize();
grCmnl.parent = current.sys_id;
grCmnl.assignment_group = '3506330cdbc92054e803f3551d9619b0';
grCmnl.state = '10';
grCmnl.short_description = "Validate Status Record";
grCmnl.insert();
new GlideSysAttachment().copy('sn_hr_core_case_workforce_admin', grCattch.getUniqueValue(), 'sn_hr_core_task', grCmnl.getUniqueValue());
}
})(current, previous);
Also to copy specific file you will have to update the logic
GlideSysAttachment.copy will copy all the attachments from source to target
I have shared solution 3 years ago; refer that
Is there any way to copy single attachment from multiple attachments ??
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-24-2022 02:03 AM
update as this to copy single file
(function executeRule(current, previous /*null when async*/ ) {
var grCattch = new GlideRecord('sys_attachment');
grCattch.addQuery('table_name', 'sn_hr_core_case_workforce_admin');
grCattch.addQuery('table_sys_id', current.sys_id);
grCattch.addEncodedQuery('file_nameSTARTSWITHStatus Record Certificate');
grCattch.query();
if (grCattch.next()) {
var grCmnl = new GlideRecord('sn_hr_core_task');
grCmnl.initialize();
grCmnl.parent = current.sys_id;
grCmnl.assignment_group = '3506330cdbc92054e803f3551d9619b0';
grCmnl.state = '10';
grCmnl.short_description = "Validate Status Record";
grCmnl.insert();
var gr1 = new GlideRecord('sys_attachment');
gr1.initialize();
gr1.file_name = grCattch.file_name;
gr1.content_type = grCattch.content_type;
gr1.compressed = grCattch.compressed;
gr1.table_name = 'sn_hr_core_task';
gr1.size_bytes = grCattch.size_bytes;
gr1.size_compressed = grCattch.size_compressed;
gr1.table_sys_id = grCmnl.getUniqueValue();
var attRec = gr1.insert();
var attDoc = new GlideRecord('sys_attachment_doc');
attDoc.addQuery('sys_attachment', grCattch.sys_id);
attDoc.query();
while(attDoc.next()){
var attDocCopy = new GlideRecord('sys_attachment_doc');
attDocCopy.initialize();
attDocCopy.sys_attachment = attRec;
attDocCopy.position = attDoc.position;
attDocCopy.length = attDoc.length;
attDocCopy.data = attDoc.data;
attDocCopy.insert();
}
}
})(current, previous);
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-24-2022 02:26 AM
Please suggest .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-24-2022 02:32 AM
Hi,
So you referred to 1st script or 2nd one
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-24-2022 02:39 AM
The file is getting attached but the unable to open the file. Please suggest
(function executeRule(current, previous /*null when async*/ ) {
var grCattch = new GlideRecord('sys_attachment');
grCattch.addQuery('table_name', 'sn_hr_core_case_workforce_admin');
grCattch.addQuery('table_sys_id', current.sys_id);
grCattch.addEncodedQuery('file_nameSTARTSWITHStatus Record Certificate');
grCattch.query();
if (grCattch.next()) {
var oldtype = grCattch.content_type.toString();
var oldfilname = grCattch.file_name.toString();
var oldfile = grCattch.file_name.split('.');
var grCmnl = new GlideRecord('sn_hr_core_task');
grCmnl.initialize();
grCmnl.parent = current.sys_id;
grCmnl.assignment_group = ''3506330cdbc92054e803f3551d9619b0';
grCmnl.state = '10';
grCmnl.short_description = "Validate Status Record";
grCmnl.insert();
var grtskatt = new GlideRecord('sys_attachment');
grtskatt.initialize();
grtskatt.file_name = 'Status Record Certificate' + '.' + oldfile[1];
grtskatt.content_type = grCattch.content_type;
grtskatt.compressed = grCattch.compressed;
grtskatt.table_name = 'sn_hr_core_task';
grtskatt.size_bytes = grCattch.size_bytes;
grtskatt.size_compressed = grCattch.size_compressed;
grtskatt.table_sys_id = grCmnl.getUniqueValue();
var attRec = grtskatt.insert();
var attDoc = new GlideRecord('sys_attachment_doc');
attDoc.addQuery('sys_attachment', grCattch.sys_id);
attDoc.query();
while(attDoc.next()){
var attDocCopy = new GlideRecord('sys_attachment_doc');
attDocCopy.initialize();
attDocCopy.sys_attachment = attRec;
attDocCopy.position = attDoc.position;
attDocCopy.length = attDoc.length;
attDocCopy.data = attDoc.data;
attDocCopy.insert();
}
}
})(current, previous);