Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Server-Side Components/Background Scripts/Add Comments/01README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
ServiceNow Background Script – Add Comments on Behalf of a User
Overview

This ServiceNow Background Script allows you to add comments or work notes to a record on behalf of a specific user.
Normally, when comments are added directly via scripts, they appear as the integration user. Using this script, the comment will show the actual user in the Activity Formatter.

This is perfect for Hacktoberfest contributions, demos, or integration testing.

Features

Works directly in Scripts – Background in ServiceNow.

Adds comments or work notes as a specified user.

Can be used for the Incident table or easily adapted for other tables.

Logs success or error messages in the Background Script output.

How to Use

Open Scripts – Background in your ServiceNow instance.

Copy and paste the script.

Update the input variables at the top of the script:

var incidentSysId = 'INCIDENT_SYSID'; // sys_id of the record
var userName = 'john.doe'; // user_name to attribute the comment to
var commentText = 'Your comment here'; // comment content
var journalField = 'comments'; // 'comments' or 'work_notes'


Click Run Script.

Check the Activity Formatter of the record to see the comment added under the correct user.

Example
var incidentSysId = '1234567890abcdef';
var userName = 'john.doe';
var commentText = 'This is my Hacktoberfest comment!';
var journalField = 'comments';


After running, the comment will appear in the Incident’s activity log as if john.doe added it.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var incidentSysId = 'e9859dd4c39b2210ffd47205e401312d'; // sys_id of the incident
var userName = 'john.doe'; // user_name of the user adding the comment
var commentText = 'This is my Hacktoberfest comment via Background Script!'; // Comment text
var journalField = 'comments'; // Can be 'comments' or 'work_notes'
//var journalField='work_notes';

var incidentGR = new GlideRecord('incident');
if (incidentGR.get(incidentSysId)) {
incidentGR[journalField].setJournalEntry(commentText, userName);
incidentGR.update();
gs.info('Comment added successfully on Incident ' + incidentSysId + ' by ' + userName + '.');
} else {
gs.error('Incident not found for sys_id: ' + incidentSysId);
}
Loading