Need help with query for sys_template table

alhicks
Tera Guru

Trying to setup an After insert/update business rule to query the sys_template table for any templates containing the just retired KB article.   We're wanting to open a submission if a template contains a KB article that is changed to Retired.

current.workflow_state.changesTo('retired')

var kb = current.sys_id;

var msg = ' ';

var gr = new GlideRecord('sys_template');

gr.addQuery('table','incident');

gr.addQuery('template', 'CONTAINS', 'kb');

gr.query();

while(gr._next()){

function submitCandidate() {

var gr = new GlideRecord('kb_submission');

gr.initialize();

gr.parent = current.sys_id;

gr.short_description = current.short_description;

gr.assignment_group.setDisplayValue('SD - Analyst');

gr.insert();

gs.addInfoMessage('Knowledge submission created:' + gr.number);

}

}

1 ACCEPTED SOLUTION

Hello Goran,



Ended up getting the business rule to work with the below business rule.   I'm thinking the problem was around the if (template.length > 0) {.   With that in the script, it opens a submission even if there's no kb article in the templates.   So, I removed it and it's working great now, so I'm not sure what happened with that but that's ok, we typically don't have the same kb in multiple templates.     Wanted to thank you for all your help.



var exp_sysids = "";



var exp_sysid = current.sys_id;


var template = 'Following Templates has a retired KB in them: \n';



var exp_sysids = "";



var exp_sysid = current.sys_id;



var gr = new GlideRecord('sys_template');


gr.addQuery('table','incident');


gr.addQuery('active','true');


gr.addQuery('template', 'CONTAINS', exp_sysid);


gr._query();



while (gr._next()){


     


template += gr.name + ", " + '\n';



      //if (template.length > 0) {


     


var gr2 = new GlideRecord('kb_submission');


gr2.newRecord();


gr2.short_description = current.short_description;


gr2.text = template;


gr2.assignment_group.setDisplayValue('SD - Analyst');


gr2.insert();



}


View solution in original post

4 REPLIES 4

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Hi,


Not sure if you want to create one submission for each template you find or just one submission for all the templates.




A couple of things.


1. Put the "condition in the condition builder instead of in the code.


1. You are using/declaring the variable gr two times, that wont work.


2. _next() isn't the correct method, next() is.


3. You never call the function you have written.


4. And a few other minor things.



Try something like this:


First set the condition like this:


find_real_file.png


Then put in the code that looks like this:



This code will make 1 kb submission with all the template names in the "text" field.



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


var kb = current.getUniqueValue();


var template = 'Following Templates has a retired KB in them: \n';


var gr = new GlideRecord('sys_template');


gr.addQuery('table','incident');


gr.addQuery('template', 'CONTAINS', kb);


gr.query();



while (gr.next()){


//Put all templates names in a string


  template += gr.name + '\n';


}


//Now make a kb submission for including all templates if there were any


if (template.length > 0) {


  var gr2 = new GlideRecord('kb_submission');


  gr2.newRecord();


  gr2.parent = x;//Parent is the current Template in the array


  gr2.short_description = current.short_description;


  gr3.text = template;


  gr2.assignment_group.setDisplayValue('SD - Analyst');


  gr2.insert();


}


  })(current, previous);


Hello Goran,



Thank you for the quick reply.   I've tried what you said but I'm not having any luck yet..  


Hello Goran,



So it's creating a submission now but doesn't seem to be querying the sys_template table for the KB number.   It's creating a submission every time I retire a KB doc, even if that KB doc is not in a template.     If I leave the while at gr.next, then it doesn't add the templates into the text.   If, I change it to   (gr._next()){   then it includes the template names for the kb's that are actually in a template.



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



var kb = current.getUniqueValue();



var template = 'Following Templates has a retired KB in them: \n';



var gr = new GlideRecord('sys_template');


gr.addQuery('table','incident');


gr.addQuery('template', 'CONTAINS', kb);



gr.query();



while (gr._next()){



//Put all templates names in a string



template += gr.name + '\n';



}



//Now make a kb submission for including all templates if there were any



if (template.length > 0) {


     


  var gr2 = new GlideRecord('kb_submission');


  gr2.newRecord();


  //gr2.parent = x;//Parent is the current Template in the array


  gr2.short_description = current.short_description;


  gr2.text = template;


  gr2.assignment_group.setDisplayValue('SD - Analyst');


  gr2.insert();



}




  })(current, previous);


Hello Goran,



Ended up getting the business rule to work with the below business rule.   I'm thinking the problem was around the if (template.length > 0) {.   With that in the script, it opens a submission even if there's no kb article in the templates.   So, I removed it and it's working great now, so I'm not sure what happened with that but that's ok, we typically don't have the same kb in multiple templates.     Wanted to thank you for all your help.



var exp_sysids = "";



var exp_sysid = current.sys_id;


var template = 'Following Templates has a retired KB in them: \n';



var exp_sysids = "";



var exp_sysid = current.sys_id;



var gr = new GlideRecord('sys_template');


gr.addQuery('table','incident');


gr.addQuery('active','true');


gr.addQuery('template', 'CONTAINS', exp_sysid);


gr._query();



while (gr._next()){


     


template += gr.name + ", " + '\n';



      //if (template.length > 0) {


     


var gr2 = new GlideRecord('kb_submission');


gr2.newRecord();


gr2.short_description = current.short_description;


gr2.text = template;


gr2.assignment_group.setDisplayValue('SD - Analyst');


gr2.insert();



}