Copy content from one Article body to another KB article body

Maxwell3
Kilo Guru

Hello all,

I am wondering if there is a way to copy the content of one KB Article body to another KB Article body on update.

When a user updates the Service Desk KB, the corresponding Self-Help KB should also get updated withing the Article body. 

I created a script in the workflow, I also created and Advanced Business Rule and I also tried using the condition builder in the Business Rule without any script but no success. Am I missing something? Has someone else encountered something similar?

I have used the script below in the workflow and also as an Advanced Business Rule.

var kb = '';
//Query knowledge base table for self help selfHelpArticle
var selfHelp=new GlideRecord('kb_knowledge_base');
selfHelp.addQuery('title', 'Self-Help Portal');
selfHelp.query();
if(selfHelp.next())
{
gs.log('Inside the First IF Statement');
kb = selfHelp.article_id;
}
if (kb != ''){
var selfHelpArticle = new GlideRecord('kb_knowledge');
//Find the Self Help article in the kb_knowledge that matches the current article.
selfHelpArticle.addQuery('meta', current.number);
selfHelpArticle.addQuery('kb_knowledge_base', kb);
selfHelpArticle.query();

if (selfHelpArticle.next()){
gs.log(' Inside the second IF statement!!!');
//If we find the article, we update it
selfHelpArticle.text = current.text;
selfHelpArticle.update();

}

}

 

1 ACCEPTED SOLUTION

MrMuhammad
Giga Sage

 You might need to pass sys_id of the Knowledge Base as on Knowledge Knowledge base is a reference field. Please try this updated code.  

var kb = '';

//Query knowledge base table for self help selfHelpArticle
var selfHelp=new GlideRecord('kb_knowledge_base');
selfHelp.addQuery('title', 'Self-Help Portal');
selfHelp.query();
if(selfHelp.next())
{
 gs.log('Inside the First IF Statement');
 kb = selfHelp.sys_id;
}
if (kb != ''){
var selfHelpArticle = new GlideRecord('kb_knowledge');
//Find the Self Help article in the kb_knowledge that matches the current article.
selfHelpArticle.addQuery('meta', current.number);
selfHelpArticle.addQuery('kb_knowledge_base', kb);
selfHelpArticle.query();

if (selfHelpArticle.next()){
  gs.log(' Inside the second IF statement!!!');
  //If we find the article, we update it
  selfHelpArticle.text = current.text;
  selfHelpArticle.update();

}

}

 

Please mark this ACCEPTED & HELPFUL if it helps.

 

Thanks & Regards,

Sharjeel

Regards,
Muhammad

View solution in original post

3 REPLIES 3

MrMuhammad
Giga Sage

 You might need to pass sys_id of the Knowledge Base as on Knowledge Knowledge base is a reference field. Please try this updated code.  

var kb = '';

//Query knowledge base table for self help selfHelpArticle
var selfHelp=new GlideRecord('kb_knowledge_base');
selfHelp.addQuery('title', 'Self-Help Portal');
selfHelp.query();
if(selfHelp.next())
{
 gs.log('Inside the First IF Statement');
 kb = selfHelp.sys_id;
}
if (kb != ''){
var selfHelpArticle = new GlideRecord('kb_knowledge');
//Find the Self Help article in the kb_knowledge that matches the current article.
selfHelpArticle.addQuery('meta', current.number);
selfHelpArticle.addQuery('kb_knowledge_base', kb);
selfHelpArticle.query();

if (selfHelpArticle.next()){
  gs.log(' Inside the second IF statement!!!');
  //If we find the article, we update it
  selfHelpArticle.text = current.text;
  selfHelpArticle.update();

}

}

 

Please mark this ACCEPTED & HELPFUL if it helps.

 

Thanks & Regards,

Sharjeel

Regards,
Muhammad

Hello Muhammad,

It seems that the script was updating an older version of the KB so I updated the query to search and update only published KB's.

Kieran Anson
Kilo Patron

Hi Maxwell,

Just as a suggestion, it might be worth investigating and using the kb_2_kb table (related articles) to create a link rather than relying on meta values.

If you were to do this, the script would look like the below:

When: After & Update

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

	// Add your code here
	var kb2 = new GlideRecord('kb_2_kb');
	kb2.addQuery('kb_knowledge',current.getUniqueValue());
	kb2.query();
	while(kb2.next()){
		var kb = new GlideRecord(kb2.sys_class_name);
		kb.get(kb2.getUniqueValue());
		kb.setValue('text',current.getValue('text'));
		kb.update();
		gs.addInfoMessage(gs.getMessage("Self-Service KB {0} has been updated based on the updated body proviced",kb.getValue('number')));
	}

})(current, previous);

 

else, I would create a system property as I presume your self-help knowledge base will always be the same.

var know = new GlideRecord('kb_knowledge');
    know.addQuery('kb_knowledge_base', gs.getProperty('self_help.knowledge_base'));
    know.addQuery('meta', 'CONTAINS', current.getValue('number'));
    know.query();
    while (know.next()) {
        know.setValue('text', current.getValue('text'));
        know.update();
        gs.addInfoMessage(gs.getMessage("Self-Service KB {0} has been updated based on the updated body proviced", kb.getValue('number')));
    }

If my reply helped with your issue please mark helpful 👍 and correct if your issue is now resolved.
By doing so you help other community members find resolved questions which may relate to an issue they're having.