To send a single email notification when the author name of KB articles is changed

Rina Mal
Tera Contributor

When a user is offboarded or marked as inactive, identify the knowledge articles where the workflow is published and the author is the current user. Update the author to the user's manager and send a single notification to the manager containing a list of the KB article numbers.

But it is generating multiple emails to its manager instead of single email. Please suggest any solution how to solve this issue. 

1 ACCEPTED SOLUTION

Vishal Jaswal
Giga Sage

Hello @Rina Mal 

Updated Async Business Rule:

 

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	if (!current.active) {
   var manager = current.manager;
   if (!manager) {
       return;
   }
   var kb = new GlideRecord("kb_knowledge");
   kb.addQuery("workflow_state", "published");
   kb.addQuery("author", current.sys_id);
   kb.query();
   var updatedKBs = [];
   while (kb.next()) {
       kb.author = manager;
       kb.update();
       updatedKBs.push(kb.number.toString()); // Collect updated KB numbers
   }
   // Send one email if there are updates
   if (updatedKBs.length > 0) {
       var kbNumbersStr = updatedKBs.join(", ");
       gs.eventQueueUnique("event_name", current, manager, kbNumbersStr);
   }
}

})(current, previous);

 



vishal_jaswal_0-1741042049591.png

Additional Information: If you want to construct the e-mail body from your Business Rule, then below is the updated code:

 

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
if (!current.active) {
   var manager = current.manager;
   if (!manager) {
       return;
   }
   var kb = new GlideRecord("kb_knowledge");
   kb.addQuery("workflow_state", "published");
   kb.addQuery("author", current.sys_id);
   kb.query();
   var updatedKBs = [];
   while (kb.next()) {
       kb.author = manager;
       kb.update();
       // Construct table row for each KB article
       var kbLink = gs.getProperty('glide.servlet.uri') + 'kb_view.do?sys_id=' + kb.sys_id;
       updatedKBs.push("<tr><td><a href='" + kbLink + "'>" + kb.number + "</a></td><td>" + kb.short_description + "</td></tr>");
   }
   // If updates happened, send an event with the formatted table
   if (updatedKBs.length > 0) {
       var kbTable = "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse;'>" +
                     "<tr><th>KB Number</th><th>Short Description</th></tr>" +
                     updatedKBs.join("") +
                     "</table>";
       gs.eventQueue("kb_update_notify", current, manager.sys_id, kbTable);
   }
}
//gs.log(updatedKBs); // for logging purposes
})(current, previous);

 


1. System Policy > Events > Registry -- Created one sample event as shown below

vishal_jaswal_1-1741042142069.png
2.  Created System Notification > Email > Notifications

vishal_jaswal_2-1741042258151.png

vishal_jaswal_3-1741042313982.png



3. Updated one dummy user active to false in sys_user table and here is one e-mail sent:

vishal_jaswal_4-1741042354292.png

 



 

 




Hope that helps!

View solution in original post

4 REPLIES 4

Medi C
Giga Sage

@rina 

You may want to use

  • Flow Designer: which would be triggered whenever there is an updated on sys_user table if a record is becoming inactive. Within the Flow, you have the flexibility to check and KB articles where the user is an author, you can store those within a Flow Variable, ...
  • Notification: To be sent at the end of the flow and it should contain. (You can use Event to trigger notification, and pass the content from the Flow Variable "KB Articles" as an event parameter). You would end up having one notification for all KBs

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

Rina Mal
Tera Contributor

Thank you for your response.

 

Here I am implementing this using an async business rule on the sys_user table when Active is set to false.

 

if (!current.active) {
        var manager = current.manager;
        if (!manager) {
            return;
        }
        var kb = new GlideRecord("kb_knowledge");
        kb.addQuery("workflow_state", "published");
        kb.addQuery("author", current.sys_id);
        kb.query();

        var updatedKBs = [];

        while (kb.next()) {
            kb.author = manager;
            kb.update();
           
            if (!updatedKBs.includes(kb.number)) {
                updatedKBs.push(kb.number.toString()); // Collect updated KB numbers
            }
        }
       
            if (updatedKBs.length > 0) {
                var kbNumbersStr = updatedKBs.join(", ");
                gs.eventQueueUnique("event_name", kb, manager, kbNumbersStr);
            }
            //current.setAbortAction(true);
       

    }

@Rina Mal 
Could you use the following instead of "gs.eventQueueUnique("event_name", kb, manager, kbNumbersStr);"?

gs.eventQueue("event_name", current, manager, kbNumbersStr);

 

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

Vishal Jaswal
Giga Sage

Hello @Rina Mal 

Updated Async Business Rule:

 

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	if (!current.active) {
   var manager = current.manager;
   if (!manager) {
       return;
   }
   var kb = new GlideRecord("kb_knowledge");
   kb.addQuery("workflow_state", "published");
   kb.addQuery("author", current.sys_id);
   kb.query();
   var updatedKBs = [];
   while (kb.next()) {
       kb.author = manager;
       kb.update();
       updatedKBs.push(kb.number.toString()); // Collect updated KB numbers
   }
   // Send one email if there are updates
   if (updatedKBs.length > 0) {
       var kbNumbersStr = updatedKBs.join(", ");
       gs.eventQueueUnique("event_name", current, manager, kbNumbersStr);
   }
}

})(current, previous);

 



vishal_jaswal_0-1741042049591.png

Additional Information: If you want to construct the e-mail body from your Business Rule, then below is the updated code:

 

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
if (!current.active) {
   var manager = current.manager;
   if (!manager) {
       return;
   }
   var kb = new GlideRecord("kb_knowledge");
   kb.addQuery("workflow_state", "published");
   kb.addQuery("author", current.sys_id);
   kb.query();
   var updatedKBs = [];
   while (kb.next()) {
       kb.author = manager;
       kb.update();
       // Construct table row for each KB article
       var kbLink = gs.getProperty('glide.servlet.uri') + 'kb_view.do?sys_id=' + kb.sys_id;
       updatedKBs.push("<tr><td><a href='" + kbLink + "'>" + kb.number + "</a></td><td>" + kb.short_description + "</td></tr>");
   }
   // If updates happened, send an event with the formatted table
   if (updatedKBs.length > 0) {
       var kbTable = "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse;'>" +
                     "<tr><th>KB Number</th><th>Short Description</th></tr>" +
                     updatedKBs.join("") +
                     "</table>";
       gs.eventQueue("kb_update_notify", current, manager.sys_id, kbTable);
   }
}
//gs.log(updatedKBs); // for logging purposes
})(current, previous);

 


1. System Policy > Events > Registry -- Created one sample event as shown below

vishal_jaswal_1-1741042142069.png
2.  Created System Notification > Email > Notifications

vishal_jaswal_2-1741042258151.png

vishal_jaswal_3-1741042313982.png



3. Updated one dummy user active to false in sys_user table and here is one e-mail sent:

vishal_jaswal_4-1741042354292.png

 



 

 




Hope that helps!