- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2023 09:20 PM
Hi All,
I have a requirement to auto attach one document to each record of the table once it is created. Please provide a solution to how to do this and what all needs to be done for this.
Thanks in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2023 10:44 PM
then do this
1) create system property of type string and attach your file to that system property
2) Use before/after insert BR on that table and use this script
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var gr = new GlideRecord("sys_properties");
gr.addQuery("name", "static_file"); // name of property in query
gr.query();
if (gr.next()) {
new GlideSysAttachment().copy('sys_properties',gr.getUniqueValue() , current.sys_class_name, current.getUniqueValue());
}
})(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
05-03-2023 09:36 PM
@Kruthik M Shiva Create an OnAfter Insert Business rule on your table and use
GlideSysAttachment.copy(String sourceTable, String sourceID, String targetTable, String targetID);
method to copy an attachment from one record to another.
Please refer to thread https://www.servicenow.com/community/developer-forum/copy-attachments-from-one-record-to-another/m-p... and check the solution provided by the author.
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2023 09:40 PM
Hi @Kruthik M Shiva ,
Hope you are doing well, please refer below script you can run as an after BR
GlideSysAttachment.copy('kb_knowledge',"knowledge record sys_id",'your table name',current.sys_id);
In the above example I have saved the attachment to be moved in a kb_knowledge record. You can choose any table you want just replace the code with table name and sys_id. You can also save the sys_id /table name in a property.
Please mark this response as correct or helpful if it assisted you with your question.
Regards,
Harshal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2023 09:40 PM
Hi @Kruthik M Shiva ,
I trust you are doing great .
To automatically attach a document to each record of a table once it is created, you can follow these steps:
Create a new table in ServiceNow to store the documents you want to attach.
Add a new column to the table you want to attach documents to. This column will store the reference to the attached document.
Write a business rule to trigger on the creation of a new record in the table. This rule should use the GlideSysAttachment class to create a new attachment record and associate it with the newly created record.
// Get the newly created record
var newRecord = current;
// Create a new attachment record
var attachment = new GlideSysAttachment();
// Set the attachment record's table name and record ID
attachment.setTableName(newRecord.getTableName());
attachment.setRecord(newRecord.sys_id);
// Set the attachment record's file name and content
attachment.write("document.pdf", "Hello World!");
// Set the reference to the attachment record on the new record
newRecord.document_attachment = attachment.sys_id;
newRecord.update();
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2023 09:45 PM
where that file will be present?
You can copy using Attachment copy functionality
what script did you start with?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader