Duplicate clickable URL comments posted when impersonating non-admin user

ronro2
Tera Contributor

Hello! 

The business requirement here: admins + non-admins are to be able to post one clickable URL comment on errands. 

 

I have created two Business Rules in Servicenow which are based on the sys_journal_field table. However, the problem is that when I impersonate a non-admin user and post a comment, the comment is posted 2 times (duplicates). This does not occur when posting a comment as an admin or a Work note as any type of user. 

 

Here is what the Business Rules looks like regarding the "comments" element:

ronro2_0-1739904030913.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 an illustration of the comment duplicates:

ronro2_1-1739892243554.png

 

 

This is the Business Rule for the "work_notes" element which functions as wanted (no duplicates no matter type of user) even though it follows the exact same code example as above: 

ronro2_1-1739904100273.png


And the BR script for Work notes clickable URL:

 

(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!

1 ACCEPTED SOLUTION

lukasz szumilas
Tera Guru

I would add logging in the BR to track executions:


gs.info("BR executed for user: " + current.getUserID() + ", comment: " + current.value);


Then check System Logs to confirm if the BR runs twice for non-admin impersonation.

If so modify the Business Rule to prevent duplicates

View solution in original post

2 REPLIES 2

lukasz szumilas
Tera Guru

I would add logging in the BR to track executions:


gs.info("BR executed for user: " + current.getUserID() + ", comment: " + current.value);


Then check System Logs to confirm if the BR runs twice for non-admin impersonation.

If so modify the Business Rule to prevent duplicates

ronro2
Tera Contributor

It never did run twice, it was some bug that got fixed witht he Xanadu upgrade. 😄