Help with kb_knowledge table query

Magali Legaspi
Tera Contributor

Hi all!

 

I need help querying the kb_knowledge table on a record producer so I can attach the latest version of a kb article on the incidents it creates.

 

var kbArt = new GlideRecord('kb_knowledge');
kbArt.addQuery('number', 'KB0107111');
kbArt.orderByDesc('sys_created_on');
kbArt.setLimit(1);
kbArt.query();

if (kbArt.next()) {
    var tableRec = new GlideRecord('m2m_kb_task');
    tableRec.initialize();
    tableRec.kb_knowledge = kbArt.sys_id;
    tableRec.task = current.sys_id;
    tableRec.insert();
}

 

Any help is much appreciated.

3 REPLIES 3

Sainath N
Mega Sage
Mega Sage

@Magali Legaspi : I used the same script in my PDI, and it worked. Please check if the KB number you are supplying as input is valid. Check if the user to whom you are trying to submit the record producer has access to create entries in the table. I tried with the admin user, and it worked.

 

If there is other login in your record producer script, please make sure that this piece of code is executing by placing some log statements.

 

sainathnekkanti_0-1704470745180.pngsainathnekkanti_1-1704470766192.png

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution. 

Siddhesh Gawade
Mega Sage
Mega Sage

Hello @Magali Legaspi ,

 

Do you want to attach the article as a attachment or do you wanted to create a new record in m2m_kb_task table? 

Paul Curwen
Giga Sage
  • To ensure you are getting the latest version on the KB Article include the 'latest' field in your query. There is no need for the line below, just remove that 
kbArt.orderByDesc('sys_created_on');

 

e.g.

 

 

var kbArt = new GlideRecord('kb_knowledge');
kbArt.addQuery('number', 'KB0107111');
kbArt.addQuery('latest', 'true');
kbArt.setLimit(1);
kbArt.query();

if (kbArt.next()) {
    var tableRec = new GlideRecord('m2m_kb_task');
    tableRec.initialize();
    tableRec.kb_knowledge = kbArt.sys_id;
    tableRec.task = current.sys_id;
    tableRec.insert();
}

 

 

 

***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul