- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks 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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
Here's what I did, a UI Policy with an onCondition() script:
/**
* Table: Project Task
* Application: MyApp
* Short Description: Review choice
* Order: 100
* |When to Apply|
* [Decision][is][Approved]
* or [Decision][is][Rejected]
* true : Global
* true : Reverse if false
* |Script|
* true : Run Scripts
* Run scripts in UI type [All]
* Execute if true:
*/
function onCondition() {
getMessage('provide_work_note', function(msg) {
g_form.showFieldMsg('decision', msg, 'error', false);
});
// Get assignment group display value
var rev_decision = g_form.getValue('decision');
var grp = g_form.getDisplayValue('assignment_group');
var ptSysID = g_form.getUniqueValue();
// Error check: if no assignment group, use a fallback or exit
if (!grp || grp === '') {
g_form.addErrorMessage('Assignment group must be set before making a review decision.');
g_form.clearValue('decision');
return;
}
// Get our category
var ga = new GlideAjax('x_some_scope.SomeAjaxCall');
ga.addParam('sysparm_name', 'getCategory');
ga.addParam('sysparm_sys_id', SysID);
ga.getXML(function(response) {
var cat = response.responseXML.documentElement.getAttribute("answer");
// Define prefixes based on the category we just received
var approvedPrefix, rejectedPrefix;
if (cat == "review") {
approvedPrefix = "APPROVED | " + grp + " | -- ";
rejectedPrefix = "REJECTED | " + grp + " | -- ";
} else {
approvedPrefix = "PLAN APPROVED | " + grp + " | -- ";
rejectedPrefix = "PLAN REJECTED | " + grp + " | -- ";
}
if (decision == 'approved') {
g_form.setMandatory('work_notes', true);
g_form.setValue('work_notes', approvedPrefix);
return;
}
if (decision == 'rejected') {
g_form.setMandatory('work_notes', true);
g_form.setValue('work_notes', rejectedPrefix);
return;
}
});
}
Does what I need without hassle.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@Carlos Petrucio Tried it, and it's coming back with the same issue, duplicate entries 😞
First entry : This is my PREFIX -- How about a try today?
Second entry : How about a try today?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
Here's what I did, a UI Policy with an onCondition() script:
/**
* Table: Project Task
* Application: MyApp
* Short Description: Review choice
* Order: 100
* |When to Apply|
* [Decision][is][Approved]
* or [Decision][is][Rejected]
* true : Global
* true : Reverse if false
* |Script|
* true : Run Scripts
* Run scripts in UI type [All]
* Execute if true:
*/
function onCondition() {
getMessage('provide_work_note', function(msg) {
g_form.showFieldMsg('decision', msg, 'error', false);
});
// Get assignment group display value
var rev_decision = g_form.getValue('decision');
var grp = g_form.getDisplayValue('assignment_group');
var ptSysID = g_form.getUniqueValue();
// Error check: if no assignment group, use a fallback or exit
if (!grp || grp === '') {
g_form.addErrorMessage('Assignment group must be set before making a review decision.');
g_form.clearValue('decision');
return;
}
// Get our category
var ga = new GlideAjax('x_some_scope.SomeAjaxCall');
ga.addParam('sysparm_name', 'getCategory');
ga.addParam('sysparm_sys_id', SysID);
ga.getXML(function(response) {
var cat = response.responseXML.documentElement.getAttribute("answer");
// Define prefixes based on the category we just received
var approvedPrefix, rejectedPrefix;
if (cat == "review") {
approvedPrefix = "APPROVED | " + grp + " | -- ";
rejectedPrefix = "REJECTED | " + grp + " | -- ";
} else {
approvedPrefix = "PLAN APPROVED | " + grp + " | -- ";
rejectedPrefix = "PLAN REJECTED | " + grp + " | -- ";
}
if (decision == 'approved') {
g_form.setMandatory('work_notes', true);
g_form.setValue('work_notes', approvedPrefix);
return;
}
if (decision == 'rejected') {
g_form.setMandatory('work_notes', true);
g_form.setValue('work_notes', rejectedPrefix);
return;
}
});
}
Does what I need without hassle.
