Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Business Rule Script for Notification KB Emails

beatricelopes
Tera Contributor

Hello people!

I came here today to seek some script advice

I have a Business Rule that trigger an email notification for a list of users > The Owner and the Managers of a Public Knowledge base 

beatricelopes_1-1704892783057.png

beatricelopes_2-1704892828216.png

the conditions:

 

beatricelopes_3-1704892880585.png

 

I am trying to dot walk into it, but it is not working 

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

    // Add your code here
    var gr = new GlideRecord('kb_knowledge_base');
	gr.addQuery('active','true');
	gr.query();
	if(gr.next()){
    var check = gr.getValue('u_notify_when_someone_subscribes_to_knowledge_base_article');
   // gs.log('check is'+check);
	
    if (gr.get(current.sub_obj_id) && check == 1) {
        //(gr.get(current.sub_obj_id)){
        gs.eventQueue('kb.subscribe.notify.knowledgebase', current.gr.kb_managers.email);
    }
	}

})(current, previous);

it works when I do
gs.eventQueue('kb.subscribe.notify.knowledgebase', current, gr.owner, gr.kb_managers);
but only for the Owner, I need this email to be sent to the KB Managers too

Right now is sending to empty recipients and is going to send-ignored type

beatricelopes_4-1704893154576.png

 

Do you have any ideas of how can I solve this? 

 

1 ACCEPTED SOLUTION

NotNowBoss
Tera Guru

Yo, @beatricelopes 

 

You have literate over kb_managers, since kb_manager is a list field its contains multiple sysid, not a user object, so we take the sysid and use it to get the user object from sys_user table

 

var sysidList = gr.kb_managers

// Split the sysidList on ','
var sysids = sysidList.split(',');

// Iterate over each sysid
for (var i = 0; i < sysids.length; i++) {
    var sysid = sysids[i].trim(); // Trim any extra spaces
 var user = GlideRecord("sys_user");
user.get(sysid);


    // Run your code for each sysid
    gs.eventQueue('kb.subscribe.notify.knowledgebase', current, user.email);
}

 

 

Ill try test it later on PDI instance

View solution in original post

2 REPLIES 2

NotNowBoss
Tera Guru

Yo, @beatricelopes 

 

You have literate over kb_managers, since kb_manager is a list field its contains multiple sysid, not a user object, so we take the sysid and use it to get the user object from sys_user table

 

var sysidList = gr.kb_managers

// Split the sysidList on ','
var sysids = sysidList.split(',');

// Iterate over each sysid
for (var i = 0; i < sysids.length; i++) {
    var sysid = sysids[i].trim(); // Trim any extra spaces
 var user = GlideRecord("sys_user");
user.get(sysid);


    // Run your code for each sysid
    gs.eventQueue('kb.subscribe.notify.knowledgebase', current, user.email);
}

 

 

Ill try test it later on PDI instance

Good!
I was able to make it work by breaking the line

gs.eventQueue('kb.subscribe.notify', current, gr.kb_knowledge_base.owner, gr.kb_knowledge_base.kb_managers);
in two 
 gs.eventQueue('kb.subscribe.notify.knowledgebase', current, gr.owner);
 gs.eventQueue('kb.subscribe.notify.knowledgebase', current, gr.kb_managers);

but I believe your solution is a better practice to apply it! 
thank you