Need help with widget to display kb article content

ARANI
Tera Contributor

Hi everyone,

I am working on an widget to display a knowledge article on a catalog item based on a variable on the form. There are multiple KB article depended on the value of this variable. I have stored the KB numbers in a system property and fetch those in the server script and display the KB article body on the HTML template.

This is the client controller to watch the variable on the catalog item and update it on the server-

function($scope,$rootScope,$sce) {
/* widget controller */
var c = this;
c.data.showWidget=false;
$scope.$watch(function () {
return $scope.page.g_form.getValue('api_dummy_variable');
},function (value) {
c.data.action=value;
if (c.data.action!=""){
c.server.get({action:c.data.action}).then(function(response){});
}
});
}

 

This is the server script-

(function() {
var gr1= new GlideRecord('kb_knowledge');
if (input.action=="r_change"){
gr1.addQuery("number="+gs.getProperty("api_knowledge_article").split(",")[0].split(":")[1]+"^workflow_state=published"); //fetching the kb article number from a property
gr1.query();
if (gr1.next()){
data.text=new global.KBViewModel().getArticleContentBySysId(gr1.getUniqueValue());
}
}

else if (input.action=="w_change"){
gr1.addQuery("number="+gs.getProperty("api_knowledge_article").split(",")[1].split(":")[1]+"^workflow_state=published"); //fetching the kb article number from a property
gr1.query();
if (gr1.next()){
data.text=new global.KBViewModel().getArticleContentBySysId(gr1.getUniqueValue());
}
}
})();

 

This is the HTML template-

<div>
<h2>Knowledge Article:</h2>
<div ng-if="!data.direct" class="kb_article" ng-class="{'knowledge-wiki-text' : data.articleType == 'wiki'}"
ng-bind-html="::data.text" style="overflow-x:auto;"></div>
</div>

 

There's nothing being displayed on the form. If I remove the if condition from the server script, it seems to work but  that is not the intended functionality. 

Any idea how can I achieve this?

7 REPLIES 7

@ARANI did this work?


Raghav
MVP 2023

Abhijit4
Mega Sage

Did you check if this is because you missed adding "c.server.update()" to see the update changes? If you have made changes in data and want to see the same changes in ui then you need to use c.server.update() in client script.

 

Please mark answer as Correct or Helpful based on impact.

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

ARANI
Tera Contributor

I'll try this out. Will you please let me know why should I use c.server.update() & not c.server.get()? Actually I am new to serviceNow, so I wanted to know the difference. I did read about these methods but I not quite clear.