ServiceNow支持在email发送附件。
//如果只想看 如何在email里面绑定发送指定的附件(不是link连接的形式),请直接看文章后半段拓展部分。
目前官方文档提供了两个方法:
1. Attaching documents with scripting
Linking to attachment records in this fashion requires using email notification scripting. For example:
template.print ( 'Attachment: <a rel="nofollow" href="/sysattachment.do?sysid=' + gr. sysid + '">' + gr. filename + '</a>\n ' ) ;
2. Include attachments
You can include all attachments from the source record with the notification. For example, if an incident update generates a notification, you can include all attachments from the incident record with the notification. To include all attachments from the source record, select the check box for the Include attachments field. Note that email messages, including attachments, cannot exceed the maximum email size.
总结:在email中要么以link的形式显示特定的attachment,或是普通邮件附带record的全部附件。
当我们也想让特定的attachment也像正常的附件背负呆在email里,而不是link的形式。应该怎么办呢?
经过查看ServiceNow社区以及相关文档。大多使用如下scripting:
```
// 'sys_email' 表里面创建 Business Rule (Before insert)
var gr = new GlideRecord('sys_attachment');
var table = '';
gr.addQuery('table_sys_id',current.instance);
gr.addQuery('file_name','test.xlsx');
gr.query();
while(gr.next()) {
table = gr.table_name;
}
if (table != '' ) {
GlideSysAttachment.copy(table,current.instance,'sys_email',current.sys_id);
}
透过代码看本质。每一个Notification被trigger后会产生一条对应的email record。给email添加附件,就是在email的状态变成‘sent’ 前给当前email记录绑定好attachment。
![](https://s4.51cto.com/images/blog/202008/27/adc18b3e5dd4740c7a0eb30d539b9298.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
但是问题是官方API GlideSysAttachment.copy()的解释:
Copies attachments from the source record to the target record. Returns
Type | Description |
---|---|
String | Array of sysIDs of the attachments that were copied. |
它的意思就是把source record里面所有的attachment都进行复制到target record。那和官方的**'Include attachments'**方案的本质相同了。
** 拓展:***********************************************************************
在目前的基础上我进一步研究,思考出了下面的方法。
依然创建一个‘sys_email’表的Business Rule.
```
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_name','incident');
attachment.addQuery('table_sys_id',current.instance+'');
attachment.addQuery('file_name',current.instance.number +'incident.pdf');
attachment.query();
if(attachment.next()){
attachment.table_name = current.getTableName();
attachment.table_sys_id = current.sys_id+'';
attachment.update();
}
这个方法的本质,就是把某条确定的Attachment绑定到本条email record。 如果想保留attachment 和原source record的绑定。那也只需要在Attachment的逻辑里面用insert()的思路替换update()的思路就好。
Created by Aaron Wang欢迎转载留言讨论**