While Loop Only Returns 1 from kb_knowledge

amielalcala
Kilo Sage

Hi,

 

I was able to display the knowledge article in the portal, but the loop only returns one (1) record. Am I missing something here? Any help would be greatly appreciated (I'm stuck here for 3 days now):

 

Server Script:

 

(function () {

  var t = data;
  var kb = new GlideRecord('kb_knowledge');
  var kbTopic = options.taxonomy_topic;
  kb.addQuery('taxonomy_topic', kbTopic);
  kb.addQuery('workflow_state', 'published');
	kb.orderByDesc('published');
  kb.query();

  t.rowCount = kb.getRowCount();
  t.articles = [];
  t.title = options.title || gs.getMessage("Knowledge Articles");

  while (kb.next()) {
    if (!kb.canRead())
		continue;
  
    var a = {};
    a.short_description = kb.getValue('short_description');
    a.sys_id = kb.getValue('sys_id');
    a.published = kb.getValue('published');
    a.published_display = gs.getMessage("Published {0}", kb.getDisplayValue('published'));

    a.taxonomy_topic = kb.getValue('taxonomy_topic');

    var kb = new GlideRecord('topic');
    kb.addQuery('sys_id', kbTopic);
    kb.query();
    if (kb.next()) {
      data.topic_id = kb.sys_id.getDisplayValue();
    }

    t.articles.push(a);
  }

})();

 

 

1 ACCEPTED SOLUTION

Oh, now I see.
You have a kb query again inside the kb query. What happens is that you're starting a new query for the same variable "kb" so you're replacing your while loop with the single loop you're getting for the topic.
I made some changes and moved the kbTopicQuery to top.

So instead of looping the first kb and then breaking the loop with new var kb statement we keep looping through the rest of the articles found.

(function() {

    var t = data;
    var kbTopic = options.taxonomy_topic;
    var kbTopicQuery = new GlideRecord('topic');
    kbTopicQuery.addQuery('sys_id', kbTopic);
    kbTopicQuery.query();
    if (kbTopicQuery.next()) {
        data.topic_id = kbTopicQuery.sys_id.getDisplayValue();
    }

    var kb = new GlideRecord('kb_knowledge');

    kb.addQuery('taxonomy_topic', kbTopic);
    kb.addQuery('workflow_state', 'published');
    kb.orderByDesc('published');
    kb.query();

    t.rowCount = kb.getRowCount();
    t.articles = [];
    t.title = options.title || gs.getMessage("Knowledge Articles");

    while (kb.next()) {
        if (!kb.canRead())
            continue;

        var a = {};
        a.short_description = kb.getValue('short_description');
        a.sys_id = kb.getValue('sys_id');
        a.published = kb.getValue('published');
        a.published_display = gs.getMessage("Published {0}", kb.getDisplayValue('published'));
        a.taxonomy_topic = kb.getValue('taxonomy_topic');
        t.articles.push(a);
    }

})();




View solution in original post

4 REPLIES 4

Weird
Mega Sage

Have you tried using console log to see what is going on with your script?

On server side console.log might not work on scoped apps, so you could also use gs.info to get the info in the logs.
Below are couple of log examples I added. Here we see the Kb topic found, the amount of records your query returns and finally if you get pass the canRead check, then the description of the article.
If your kbTopic shows up as empty, then it's breaking it. Also if your count is 1 that means you only have one article that matches the criteria.
If you get more results, but only one log after the can read criteria, then that's blocking the articles.

(function () {

  var t = data;
  var kb = new GlideRecord('kb_knowledge');
  var kbTopic = options.taxonomy_topic;
console.log("Kb Topic: " + kbTopic);
  kb.addQuery('taxonomy_topic', kbTopic);
  kb.addQuery('workflow_state', 'published');
	kb.orderByDesc('published');
  kb.query();
console.log("Kb count: " + kb.getRowCount());
  t.rowCount = kb.getRowCount();
  t.articles = [];
  t.title = options.title || gs.getMessage("Knowledge Articles");

  while (kb.next()) {
    if (!kb.canRead())
		continue;
  console.log("Can read " + kb.getValue('short_description');
    var a = {};
    a.short_description = kb.getValue('short_description');
    a.sys_id = kb.getValue('sys_id');
    a.published = kb.getValue('published');
    a.published_display = gs.getMessage("Published {0}", kb.getDisplayValue('published'));

    a.taxonomy_topic = kb.getValue('taxonomy_topic');

    var kb = new GlideRecord('topic');
    kb.addQuery('sys_id', kbTopic);
    kb.query();
    if (kb.next()) {
      data.topic_id = kb.sys_id.getDisplayValue();
    }

    t.articles.push(a);
  }

})();


 

Hi @Weird appreciate your response for this one! I have tried on the background script and it's working as expected but on portal, it only shows one.

amielalcala_0-1706530977766.png

 

Am I missing something on HTML?

<div ng-if="data.articles.length > 0" class="panel panel-{{::options.color}} b">
    <div class="panel-heading">
        <h3 class="panel-title">{{::data.title}}</h3>
    </div>
    <div class="panel-body">
        <div class="content-section-single-cards">
            <div ng-repeat="a in data.articles | limitTo : 5" style="margin-bottom: 1em;" class="flex-grid">
                <span class="topic-type"><i class="fa fa-file-text-o card-icon"></i> Article</span>
                <a class="a-title" href="?id=kb_article&sys_id={{::a.sys_id}}">{{::a.short_description}}</a>
                <div>
                    <span title="{{::a.published_display}}">
                        <!-- <glyph sn-char="calendar"></glyph> -->
                        <sn-day-ago class="date" date="a.published"></sn-day-ago>
                    </span>
                </div>
            </div>
        </div>

        <div class="btn-flex">
            <a class="btn-view" href="?id=emp_taxonomy_topic&topic_id={{data.topic_id}}" title="Show More Articles">Show More Articles</a>
        </div>
    </div>
</div>

Oh, now I see.
You have a kb query again inside the kb query. What happens is that you're starting a new query for the same variable "kb" so you're replacing your while loop with the single loop you're getting for the topic.
I made some changes and moved the kbTopicQuery to top.

So instead of looping the first kb and then breaking the loop with new var kb statement we keep looping through the rest of the articles found.

(function() {

    var t = data;
    var kbTopic = options.taxonomy_topic;
    var kbTopicQuery = new GlideRecord('topic');
    kbTopicQuery.addQuery('sys_id', kbTopic);
    kbTopicQuery.query();
    if (kbTopicQuery.next()) {
        data.topic_id = kbTopicQuery.sys_id.getDisplayValue();
    }

    var kb = new GlideRecord('kb_knowledge');

    kb.addQuery('taxonomy_topic', kbTopic);
    kb.addQuery('workflow_state', 'published');
    kb.orderByDesc('published');
    kb.query();

    t.rowCount = kb.getRowCount();
    t.articles = [];
    t.title = options.title || gs.getMessage("Knowledge Articles");

    while (kb.next()) {
        if (!kb.canRead())
            continue;

        var a = {};
        a.short_description = kb.getValue('short_description');
        a.sys_id = kb.getValue('sys_id');
        a.published = kb.getValue('published');
        a.published_display = gs.getMessage("Published {0}", kb.getDisplayValue('published'));
        a.taxonomy_topic = kb.getValue('taxonomy_topic');
        t.articles.push(a);
    }

})();




@Weird this is amazing! Thank you 🙏 it finally works. And thanks for the detailed explanation!