- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2023 02:03 AM
Hi All,
When I am trying to add attachment from sys_attachment table to sys_email record using GlideSysAttachment method in Business Rule, it is adding multiple attachments with same filename.
My requirement is to add only one latest attachment created.
(function executeRule(current, previous /*null when async*/) {
var s_Attach = new GlideRecord('sys_attachment');
s_Attach.addEncodedQuery('file_name=Servers_Information.csv^table_name=sys_attachment');
s_Attach.orderByDesc('sys_created_on');
s_Attach.setLimit(1);
s_Attach.query();
if(s_Attach.next()){
var sSysID = s_Attach.table_sys_id;
gs.info('the sSysID is for Attachment '+sSysID);
}
var sTable = 'sys_attachment';
var tTable = 'sys_email';
var tSysID = current.sys_id;
gs.info('the tSysID '+tSysID);
var copyAtt = new GlideSysAttachment();
copyAtt.copy(sTable,sSysID,tTable,tSysID); // Attaching the Attachment to the email
gs.info('the notifications sysID'+tSysID);
gs.info('the table name is '+tTable);
})(current, previous);
Please provide inputs to achieve it.
Regards,
Reshma.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2023 03:07 AM
are you sure the file name you are comparing is exact?
similar script has worked for me earlier
My BR was After Insert on sys_email
try checking by not adding file name in query
(function executeRule(current, previous /*null when async*/) {
var s_Attach = new GlideRecord('sys_attachment');
s_Attach.addEncodedQuery('table_sys_id=' + current.instance);
s_Attach.orderByDesc('sys_created_on');
s_Attach.setLimit(1);
s_Attach.query();
if(s_Attach.next()){
var rec = new GlideRecord('sys_email_attachment');
rec.initialize();
rec.attachment = s_Attach.sys_id;
rec.file_name = s_Attach.file_name;
rec.source = 'notification';
rec.content_disposition = 'attachment';
rec.email = current.sys_id;
rec.insert();
}
})(current, previous);
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2023 02:14 AM
this script will work fine
(function executeRule(current, previous /*null when async*/) {
var s_Attach = new GlideRecord('sys_attachment');
s_Attach.addEncodedQuery('file_name=Servers_Information.csv^table_sys_id=' + current.instance);
s_Attach.orderByDesc('sys_created_on');
s_Attach.setLimit(1);
s_Attach.query();
if(s_Attach.next()){
var rec = new GlideRecord('sys_email_attachment');
rec.initialize();
rec.attachment = s_Attach.sys_id;
rec.file_name = s_Attach.file_name;
rec.source = 'notification';
rec.content_disposition = 'attachment';
rec.email = current.sys_id;
rec.insert();
}
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2023 02:52 AM
Thanks for your inputs,
It is not working for my requirement.
My Exact requirement is one email will create in sys_email table. For that created email we need to add attachment automatically using after Business rule.
So, we are using GlideSysAttachment method. It is adding multiple attachment with same file name.
Need to add current created attachment of that file name from sys_attachment.
Regards,
Reshma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2023 03:07 AM
are you sure the file name you are comparing is exact?
similar script has worked for me earlier
My BR was After Insert on sys_email
try checking by not adding file name in query
(function executeRule(current, previous /*null when async*/) {
var s_Attach = new GlideRecord('sys_attachment');
s_Attach.addEncodedQuery('table_sys_id=' + current.instance);
s_Attach.orderByDesc('sys_created_on');
s_Attach.setLimit(1);
s_Attach.query();
if(s_Attach.next()){
var rec = new GlideRecord('sys_email_attachment');
rec.initialize();
rec.attachment = s_Attach.sys_id;
rec.file_name = s_Attach.file_name;
rec.source = 'notification';
rec.content_disposition = 'attachment';
rec.email = current.sys_id;
rec.insert();
}
})(current, previous);
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2023 08:30 AM
It is working fine.
Attachment is adding at related list of sys_email_attachment. How to set and add that attachment at top header of created email (Means at Manage Attachments the files need to add).
Presently it is showing no attachments added at that point.
Regards,
Reshma