Catalog Item Widget that Relates to Knowledge Article

Kev9
Kilo Sage

Hi,

In Service Portal I'd like a widget in my knowledge article that shows sc_cat_ items related to that article. I'd guess there'd need to be some relationship between the meta in the article and the cat item but can't really figure out how to do this.

Can anyone offer suggestions?

Many thanks.

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

Hi,

So your post was interesting to me and I had been working on this over the past 2 days, but didn't get a chance to post until just now.

So I built out a custom widget that will query the knowledge article's metadata and then query your catalog item's metadata and if any term matches between the two, it'll show the "related catalog item" for the user to simply click and it takes them right to the form to fill out.

Obviously this is just for Service Portal, but this should help ease the user's experience and make things pretty fluid.

To accomplish what I did, you'd need to go to: Service Portal > Service Portal Configuration > Widget Editor and then click create new widget. Name it whatever you'd like and then in each of these sections, copy and paste the code from below:

HTML

<sp-panel ng-if="data.items.length > 0" >
  <ul class="list-group">
    <li class="list-group-item" ng-repeat="a in data.items">
      <a href="?id=sc_cat_item&sys_id={{::a.sys_id}}">{{::a.name}}</a>
      <div>
      </div>
    </li>
  </ul>
</sp-panel>

CSS

.transclude:last-child {
  padding-bottom: 10px;
}

.list-group {
 	margin-bottom: 0px; 
}

.list-group-item {
    padding: 0;
    border: none;
    margin-bottom: 10px;
    border-radius: 0px;
}

.list-group-item:last-child {
	margin-bottom: 10px;
}

Client Script/Controller

function() {
  /* widget controller */
  var c = this;
}

Server Script

(function() {
	data.items = [];
	options.title = options.title || gs.getMessage("Related Catalog Items");
	
	var sys_id = $sp.getParameter('sys_id');
	var kbmeta;
	
	if(sys_id != null ){
		var gr = new GlideRecord("kb_knowledge");
		if(gr.get($sp.getParameter('sys_id'))){ 
			data.kbmeta = gr.getValue('meta');
			kbmeta = gr.getValue('meta').toString().split(',');
		} else
			 return false;	
	}
	runscript(kbmeta);
})();

	function runscript(kbmeta) {
	var relatedItems = [];
	var gr, i;
	for (i = 0; i < kbmeta.length; i++) {
	gr = new GlideRecord('sc_cat_item');
	gr.addQuery('meta', 'CONTAINS', kbmeta[i]);
	gr.addQuery('sys_id', 'NOT IN', relatedItems);
	gr.query();
	while (gr.next()) {
		if (gr.getRowCount() > 0) {
		relatedItems.push(gr.sys_id+'');
		var a = {};
		$sp.getRecordValues(a, gr, 'name,short_description,sys_id'); 
		data.items.push(a);
   }
  }
 }
}

now click "Save".

Now, go to Service Portal > Service Portal Configuration > Designer and click the page for the Knowledge Articles (where you wanted this to show) - for me I put it on the kb_article page. Once you're on that page in designer mode, simply find the widget along the left-hand side and drag and drop it where you want it to show on the page. Once it's there...hover your mouse over it and click the pencil icon, like so:

find_real_file.png

Now you can clean it up a bit more and add a Title and Bootstrap Color (like I chose title of Related Catalog Items and bootstrap of Primary).

Once you're all finished it should look something like this:

find_real_file.png

So the user would then just click on relevant catalog item. I have several showing as an example, but I assume you'd really only have like 1 or 2 maybe 3 showing as far as matching metadata.

Tested and working in Madrid too, by the way!

Please mark reply as Helpful/Correct. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

21 REPLIES 21

Hi Paul,

 

as far as i can find there is OOB for CatalogItems - But i like to Link ContentItems. Is there a way to accompish this via the OOB?

In my CatalogItems i do find a related list for Articles, but this is not present for ContentItems 😞

I just updated the reference qualifier on the Related Items table to let me choose Content Items.

I thought it was a strange limitation OOB.


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Hi Paul,

could you please provide me some screenshots to reproduce this? I don`t know how to update the reference qualifier 🙂

Thank you very much!

 

Edit:

Found it  --> Thank you very much! - Works as Designed! 🙂

find_real_file.png

Showing this error in new release

hi @manibaig : Did you find a solution for this error?