I get duplicate comments (meanwhile Work notes works fine) when impersonating non-admin user
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 07:36 AM
Hello!
I have a problem with a Business Rule in Servicenow which is based on the sys_journal_field table (to prevent duplicate comments from happening).
The problem is that when I impersonate a non-admin user and post a comment, the comment is posted 2 times (duplicates). This problem does not occur when posting a Work note.
But If I comment as myself (an admin) then there is no duplicate.
Here is what the Business Rules looks like regarding the comments element:
Here is the script for the comments Business Rule:
(function executeRule(current, previous /*null when async*/) {
// Kontrollera att det gäller comments
if (current.element == "comments") {
var task = new GlideRecord("task");
// Hämta den relaterade tasken
if (task.get(current.element_id)) {
// Kontrollera att det INTE är BEH Task eller Driftöverlämning
if (task.sys_class_name != "x_vgll_beh_task" && task.sys_class_name != "x_vgll_service_rev_service_operations_review") {
var comment = current.value; // Hämta den faktiska kommentaren
gs.log('tele2' + current.getUserID());
var isUrl = /(((https?:\/\/)|(www\.))[^\s]+)/g; // Regex för URL
var matches = comment.match(isUrl);
if (matches) {
var links = matches.map(function(link) {
if (link.startsWith("www.")) {
link = 'https://' + link;
}
return '[code]<a href="' + link + '" target="_blank">' + link + ' </a>[/code]';
});
var linkString = 'Innehåller följande länkar:\n' + links.join('\n');
// Behåll originaltexten och lägg till länkarna under den
current.value = comment + '\n\n' + linkString;
}
}
}
}
})(current, previous);
Here is what happens:
The same doesn't happen with the Business Rule regarding the work_notes element, even though it follows the exact same code:
And the BR script for Work notes:
(function executeRule(current, previous /*null when async*/) {
// Kontrollera att det gäller work_notes
if (current.element == "work_notes") {
var task = new GlideRecord("task");
// Hämta den relaterade tasken
if (task.get(current.element_id)) {
// Kontrollera att det INTE är BEH Task eller Driftöverlämning
if (task.sys_class_name != "x_vgll_beh_task" && task.sys_class_name != "x_vgll_service_rev_service_operations_review") {
var workNote = current.value; // Hämta den faktiska work note-posten
gs.log('work_notes script executed for user: ' + current.getUserID());
var isUrl = /(((https?:\/\/)|(www\.))[^\s]+)/g; // Regex för URL
var matches = workNote.match(isUrl);
if (matches) {
var links = matches.map(function(link) {
if (link.startsWith("www.")) {
link = 'https://' + link;
}
return '[code]<a href="' + link + '" target="_blank">' + link + ' </a>[/code]';
});
var linkString = 'Innehåller följande länkar:\n' + links.join('\n');
// Behåll originaltexten och lägg till länkarna under den
current.value = workNote + '\n\n' + linkString;
}
}
}
}
})(current, previous);
What do you guys think is the issue? I couldn't find any other BR that would interfer or cause duplicate comments, nor any Script Includes or Script Actions.
Thanks in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 07:41 AM
what's your business requirement here?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 10:56 AM
https://www.servicenow.com/community/developer-forum/duplicate-clickable-url-comments-posted-when-im...
Sorry for not being clear.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 09:16 AM
The screenshots are unfortunately a bit small and hard to read. But if the comment is being posted twice, I'm guessing there's a BR that is doing a current.update(). What are you ultimately trying to accomplish?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 10:57 AM