The CreatorCon Call for Content is officially open! Get started here.

I get duplicate comments (meanwhile Work notes works fine) when impersonating non-admin user

ronro2
Tera Contributor

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:

ronro2_0-1739892243556.png

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: 

ronro2_1-1739892243554.png

 

 

The same doesn't happen with the Business Rule regarding the work_notes element, even though it follows the exact same code: 

ronro2_2-1739892243555.png

 


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!

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@ronro2 

what's your business requirement here?

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

jcmings
Mega Sage

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?