Add Prefix to Work Note
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 hours ago
Trying to add a prefix to work notes on a custom table. I initially thought a business rule would fit the bill, so I tried that...
/**
* name : work note prefix
* table : x_somescope_table
* when : before
* order : 100
* update : true
* filter conditions : [Work notes][changes]
*/
// Advanced
// Script
(function executeRule(current, previous) {
// Get the work notes from the actual form submission
var workNoteInput = current.getValue('work_notes');
var pre = "This is my PREFIX | -- ";
if (!workNoteInput.nil()) {
current.work_notes = pre + workNoteInput;
} else {
gs.addErrorMessage("Work notes cannot be empty.");
current.setAbortAction(true);
}
})(current, previous);
This works, EXCEPT it produces 3 work notes...
1. This is my PREFIX | -- Here we go
2. Here we go
3. This is my PREFIX | -- Here we go
Here we go
After failing a few times, I thought, why not create a BR on Journal Entry [sys_journal_field] instead, so I tried there too...
/**
* name : work note prefix
* table : x_somescope_table
* when : before
* insert : true
* filter conditions : [Element][is][work_notes], AND [Name][is][x_somescope_table]
*/
// Advanced
// Script
(function executeRule(current, previous) {
var PREFIX = "This is my PREFIX -- ";
// Only for Incident Work notes
if (current.name != 'x_somescope_table') return;
if (current.element != 'work_notes') return;
var v = (current.value + '') || '';
if (!v) return;
// Prevent double-prefixing
if (v.indexOf(PREFIX) !== 0) {
current.value = PREFIX + v;
}
})(current, previous);
And I still received multiple work notes...
1. This is my PREFIX -- OK, let's try this from another angle...
2. OK, let's try this from another angle...
So, how does one simply add a prefix to a work note without producing duplicate entries?
Has anyone done this already?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
The reason you’re seeing duplicate work notes is because of how ServiceNow handles journal fields:
Work Notes are journal fields – every time you set current.work_notes on the main table, ServiceNow automatically creates a new journal entry. So if you modify it in a Before Update Business Rule, you end up creating multiple entries: the system adds one automatically, and your script adds another.
When using a Business Rule on sys_journal_field, each insert into the journal can trigger the rule again if not properly filtered, which can also create duplicates.
Correct Approach
The recommended way is to use a Business Rule on sys_journal_field, Before Insert, and make sure you check if the prefix already exists:
(function executeRule(current, previous) {
var PREFIX = "This is my PREFIX -- ";
// Only for your custom table
if (current.name != 'x_somescope_table') return;
if (current.element != 'work_notes') return;
// Prevent duplicate prefix
if (current.value.indexOf(PREFIX) === 0) return;
// Add prefix
current.value = PREFIX + current.value;
})(current, previous);Key points:
Run the BR on Before Insert in sys_journal_field.
Filter by element = work_notes and name = x_somescope_table.
Only add the prefix if it doesn’t already exist (indexOf(PREFIX) === 0).
Do not modify current.work_notes directly in the main table’s BR, otherwise you’ll create duplicate entries.
✅ This approach ensures the prefix is added only once, even if the work note is edited or updated later.
Was this explanation helpful? If so, please mark it as correct!
Carlos Petrucio
ServiceNow Developer
