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.

setting kowledge author to last knowledge article body updated by

ChanduM
Tera Contributor

Hello Everyone, 

 

I have a requirement where I need to update the knowledge author to last article updated by that means whoever modified the knowledge article body last. 

Need to write a BR for this where I have taken table as Knowledge and in filter condition knowledge base as HR. 

Can anyone please help me with the script.

1 ACCEPTED SOLUTION

swathisarang98
Giga Sage

Hi @ChanduM ,

 

You can write a simple Before update business rule as below,

Table: Kb_knowledge

Condition: 

Knowledge base is HR 

Article body changes

swathisarang98_0-1715677576631.png

swathisarang98_1-1715677603530.png

Code:

 

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

    var updatedBy = current.sys_updated_by;
   // gs.info('Updated By' + updatedBy);
   // gs.info("Author : " + current.author);
    var gr = new GlideRecord('sys_user');
    gr.addQuery('user_name', updatedBy);
    gr.query();
    if (gr.next()) {
        current.author = gr.sys_id;
       // gs.info("Author : " + current.author);
    }



})(current, previous);

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

View solution in original post

3 REPLIES 3

swathisarang98
Giga Sage

Hi @ChanduM ,

 

You can write a simple Before update business rule as below,

Table: Kb_knowledge

Condition: 

Knowledge base is HR 

Article body changes

swathisarang98_0-1715677576631.png

swathisarang98_1-1715677603530.png

Code:

 

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

    var updatedBy = current.sys_updated_by;
   // gs.info('Updated By' + updatedBy);
   // gs.info("Author : " + current.author);
    var gr = new GlideRecord('sys_user');
    gr.addQuery('user_name', updatedBy);
    gr.query();
    if (gr.next()) {
        current.author = gr.sys_id;
       // gs.info("Author : " + current.author);
    }



})(current, previous);

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

Hello Swathi, 

 

It worked!! thank you!!

Sohail Khilji
Kilo Patron

Hi @ChanduM ,

 

Your conditions are right. Just add a condition to check if article body changes so you dont beed to trigger BR only if Kbase is HR, Hence there will be 2 conditon check , and in script section go for the below script:

 

var lastUpdated = current.sys_updated_by;
var grr = new GlideRecord('sys_user');
grr.addQuery('user_name', lastUpdated );
grr.query();
if(grr){
current.author = grr.sys_id;
} //script by sohail khilji

 

 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect