Log work note on adding or removing attachment
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 03:21 AM
Hi All,
Good day!!
I have a requirement where I should see a work note added to an incident/problem/change record whenever an attachment is added/removed to the record, saying:
<File Name> has been attached to <Record Number>
Kindly suggest script action for the above requirement.
Regards,
Priya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 03:30 AM
You can achieve this by creating a Business Rule on the sys_attachment table to detect when an attachment is added or removed and then update the corresponding record (Incident, Problem, or Change) with a work note.
- Table: sys_attachment
- When: Before
- Insert: ✅ (Checked)
- Delete: ✅ (Checked)
- Update: ❌ (Unchecked)
Script:
(function executeRule(current, previous /*null when async*/) {
// Get the parent record (Incident, Problem, Change, etc.)
var parentTable = current.table_name; // Table where attachment was added
var parentSysId = current.table_sys_id; // Record Sys ID
if (!parentTable || !parentSysId) {
return; // Exit if there's no parent record
}
// Get the corresponding record
var parentRecord = new GlideRecord(parentTable);
if (parentRecord.get(parentSysId)) {
var action = current.operation() === "insert" ? "attached to" : "removed from";
var workNoteMessage = '"' + current.file_name + '" has been ' + action + ' ' + parentRecord.number;
// Add work note
parentRecord.work_notes = workNoteMessage;
parentRecord.update();
}
})(current, previous);
Please mark helpful/correct if this helps you thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 03:34 AM
Hi Sunil,
can we not achieve this via script action?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 03:58 AM
Is this a business requirement because there could be many users working and adding/removing files and will increase the number of work notes
script action for deletion case
Add a note on the Activity Log when an attachment is deleted from a record
You can use after insert business rule on sys_attachment, condition as Table IS ONE OF incident or problem or change_request
(function executeRule(current, previous /*null when async*/ ) {
// Get the corresponding record
var parentRecord = new GlideRecord(current.table_name);
if (parentRecord.get(current.table_sys_id)) {
var workNoteMessage = current.file_name + ' has been attached to ' + parentRecord.number;
parentRecord.work_notes = workNoteMessage;
parentRecord.update();
}
})(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
07-03-2025 12:58 AM
Hope you are doing good.
Did my reply answer your question?
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