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.

Update KB article using script

Sunny45
Kilo Guru

Hello Team,

I have requirements to update Published KB article automatically at pre-defined interval using script. 

Basically I need to update the Article body field. (below) which contains HTML data.

find_real_file.png

 

 Please advise,

Thanks,

1 ACCEPTED SOLUTION

Allen Andreas
Tera Patron

Hi,

Ok. You can use GlideRecord to query and get the published article(s) as needed and then add your string to the "text" field and then update the record.

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/c_GlideRecordSco...

Let me know after attempting yourself if you need further assistance and if so, with what.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

4 REPLIES 4

Rajanmehta
Mega Guru

Sunny,

You may try using regular script to modify field ('text' column name for Article body) 

var rec = new GlideRecord("kb_knowledge")
rec.addQuery('sys_id','<sys_ID>');
rec.query();

gs.info(rec.getRowCount());
if(rec.next()){
var result = rec.setValue(<New Value of Article Body >);
result.update();
}

Hope this helps.

Allen Andreas
Tera Patron

Hi,

Ok. You can use GlideRecord to query and get the published article(s) as needed and then add your string to the "text" field and then update the record.

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/c_GlideRecordSco...

Let me know after attempting yourself if you need further assistance and if so, with what.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Thanks Allen.

I was thinking, may be it is HTML we might have to do it different way. 

Quick question, Would it matter if the article is published? (i.e. After the update, will it create new version or save it under same version).

Please advise.

Thanks.

 

Hi Allen,

How are you getting through the publish process?  In our instance we are using instant publish/instant retire workflows and a standard update script won't seem to work.  I tried setting the autosysfields and the workflow to false but it still doesn't seem to work.  We are currently running Vancouver and here's a sample script I used:

function run() {
var gr = new GlideRecord('kb_knowledge');
gr.addEncodedQuery('workflow_state=published^number=KB0010032');
gr.query();

var log = [];
while (gr.next()) {
gr.setWorkflow(false);
gr.autoSysFields(false);
gr.ownership_group = 'sysid-goes-here';
gr.update();
log.push(gr.getDisplayValue());

}
return log;
}
run();