Log work note on adding or removing attachment

Priya V M
Tera Contributor

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

4 REPLIES 4

sunil maddheshi
Tera Guru

@Priya V M 

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!

 

Priya V M
Tera Contributor

Hi Sunil,

can we not achieve this via script action?

Ankur Bawiskar
Tera Patron
Tera Patron

@Priya V M 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Priya V M 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader