How to add knowledge blocks in knowledge articles using script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2022 06:38 AM
We have a customised knowledge table. That knowledge table contains 5 customised fields which have ACLs to restrict the view of these articles.
I we would like to move these to OOTB structure. I am able to create Knowledge articles using the short description and one of the field values to be article text. I also have created separate knowledge blocks using the content from other fields and have created 1 knowledge block for each field.
I would like to move these blocks to the knowledge article depending on the short description match because the knowledge article and the desired blocks for that article have the same short description. I am unable to do it. Request to kindly help.
The below code inserts the blocks as only pictures inside the article in both article body and block body. When I say pictures I mean that when I open the article as an end user, it shows block picture and not what is written inside that block.
var LIMIT = 10000;
var count = getRecordCount();
var numberOfRuns = count / LIMIT;
var remainder = count % LIMIT;
var location = 0;
for (var i = 0; i < numberOfRuns + 1; i++) {
if (i == numberOfRuns) {
updateArticles(remainder);
} else {
updateArticles(LIMIT);
location += LIMIT
}
}
function getRecordCount() {
var count;
var kbArticlaGA = new GlideAggregate('kb_knowledge');
kbArticlaGA.addAggregate('COUNT');
kbArticlaGA.query();
while (kbArticlaGA.next()) {
count = kbArticlaGA.getAggregate('COUNT');
}
return count;
}
function updateArticles(limit) {
var kbArticleGR = new GlideRecord('kb_knowledge');
kbArticleGR.setLimit(limit);
kbArticleGR.chooseWindow(location, limit + location);
kbArticleGR.query();
while (kbArticleGR.next()) {
var shDescription = kbArticleGR.getValue('short_description');
var kbBlockGR = new GlideRecord('kb_knowledge_block');
kbBlockGR.addQuery('short_description', shDescription);
kbBlockGR.query();
while (kbBlockGR.next()) {
var blockNumber = kbBlockGR.getValue('number');
var randomNumber = Math.floor(Math.random() * 1000000).toString();
var html = '<div id="kbblock_"' + blockNumber + '_' + randomNumber + '"_kbblock" class="kb-block-content-item mceNonEditable" style="border: 1px solid #ddd; padding: 10px; margin: 5px 0; disabled: true; display: block; color: #343d47; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; line-height: 1.42857; font-weight: bold; width: 95%;"><div class="block-info" style="display: inline-block; vertical-align: top;">' + blockNumber + '<span class="block-info" style="display: block; margin-top: 3px;">' + shDescription + '</span></div>';
var existingValue = kbArticleGR.getValue('text');
kbArticleGR.setValue('text', existingValue + '<br>' + html);
kbArticleGR.update();
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2022 07:11 PM
I am just looking at the same problem on San Diego. Did you ever get an answer or find a solution.
Also, the Random Number, do you know its purpose?