Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Widget on service portal

JoseSantosA
Tera Contributor

I am working on service platform. I have cloned the out of the box widget call "KB Most viewed", when I drag and drop original widget into my page it works fine but when I drag and drop the one that I cloned from the original is causing an error, is not being displayed. 

I have this piece of code in the server script but it seems I have some access issues because i dont see the log from line #11.

 

JoseSantosA_0-1762455342169.png


I have made the cloned widged as public, the page is public as well, idk what else i need to make public so this can work

 

6 REPLIES 6

Sarthak Kashyap
Mega Sage

Hi @JoseSantosA ,

 

Are you getting the log at line number 7 ? 

Try below code 

 

gs.info("Widget Clone start Test ");
    try {
        var gr = new GlideRecord("kb_knowledge");
        gr.addQuery("number", "KB0000492");
        gr.query();
        gs.info("Record count: " + gr.getRowCount());
        while (gr.next()) {
            gs.info("Found record =  " + gr.number);
        }
        gs.info("Widget Clone End Test");
    } catch (e) {
        gs.info("Error in KB widget clone: " + e.message);
    }

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak

 

SVimes
Kilo Sage

First, you shouldn't try logging console from the server script. Console logging is something that should be done in the client controller. You can populate the data object in the server script which the client script can reference like this:

 

SVimes_0-1762458565969.png

 

Second, I cloned the same widget and added it without modifications to a blank page which works just fine by itself which tells me something else is conflicting. Are you able to share the full html, server, and client code for the new widget?

Sable Vimes - CSA

this is the HTML 

<sp-panel ng-if="data.articles.length > 0" >
  <ul class="list-group" aria-label="{{::options.title}}">
    <li class="list-group-item" ng-repeat="a in data.articles">
      <a href="?id=kb_article&sys_id={{::a.sys_id}}">{{::a.short_description}}</a>
      <div>
        <span ng-if="a.sys_view_count == 1" class="views pad-right">
          <glyph sn-char="eye-open" class="pad-right"></glyph>
          ${{{::a.sys_view_count}} View}
        </span>
        <span ng-if="a.sys_view_count > 1" class="views pad-right">
          <glyph sn-char="eye-open" class="pad-right"></glyph>
          ${{{::a.sys_view_count}} Views}
        </span>
        <span ng-if="false" class="published pad-right">
          <span ng-if="a.sys_view_count > 0" class="pad-right">&#8226;</span> <glyph sn-char="calendar" class="pad-right"></glyph>
          <sn-day-ago date="a.published"></sn-day-ago>
        </span>
      </div>
    </li>
  </ul>
</sp-panel>


this is the server script:

(function() {

	console.log("Widget Clone start Test ");
    try {
        var gr = new GlideRecord("kb_knowledge");
        gr.addQuery("number", "KB0010359");
        gr.query();
        console.log("Record count: " + gr.getRowCount()); // this is returning 1
        while (gr.next()) { // it never enters the loop
            console.log("Found record =  " + gr.number);
        }
        console.log("Widget Clone End Test");
    } catch (e) {
        console.log("Error in KB widget clone: " + e.message);
    }

})();


and all i got in the client controller is this:

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


im pretty sure most likely this error is due to some ACL read rules but i still have no clue

Ok. It is very clear why nothing displays with that information.

 

In your HTML, you have the following right at the top:

<sp-panel ng-if="data.articles.length > 0" >

 

In your server script, data.articles is not even defined which means it is not > 0 so nothing displays. Take a look at the OOTB widget to see how data.articles is being defined and update your server script accordingly. Example:

 

(function() {
	data.articles = [];
	options.title = options.title || gs.getMessage("Most Viewed Articles");
	
	var z;
	var knowledge_bases;
	var sys_id = $sp.getParameter('sys_id');
	var kb_id = $sp.getParameter('kb_id');
	var gr = new GlideRecord("kb_knowledge");
	
	var foundArticle = false;
	if (sys_id) { // sys_id specified, try to find an article by sys_id or number
		gr.addQuery("sys_id", sys_id).addOrCondition("number", sys_id);
		gr.query();
		if (gr.next() && gr.canRead()) {
			// When sys_id matches a kb_record, get KB of the article
			knowledge_bases = String(gr.getValue('kb_knowledge_base'));
			foundArticle = true;
		}
	}
	
	if (!foundArticle) { /* For all other pages this code will get executed. Used on knowledge home page.*/
		/*If KB is selected display most viewed articles only from that KB*/
		if (kb_id != null)
			knowledge_bases = String(kb_id);
		else {
			 /*Get all knowledge bases associated with Portal*/
			knowledge_bases = String($sp.getKnowledgeBases());
		}
	}
	
	if (GlideStringUtil.notNil(knowledge_bases))
		z = $sp.getAllKBRecords(knowledge_bases);
	else //If there are no accessible KBs for logged in user
		return;
	
	z.addQuery("sys_view_count", ">", "0");
	z.addQuery("number", "KB0010359"); //<--DELETE ME WHEN DONE TESTING
	if (options.kb_category)
		z.addQuery("kb_category", options.kb_category);
	z.orderByDesc('sys_view_count');
	z.setLimit(options.max_number || 5);
	z.query();
	while (z.next()) {
		if (!z.canRead())
			continue;

		var a = {};
		$sp.getRecordValues(a, z, 'short_description,sys_view_count,sys_id,published');
		data.articles.push(a);
	}
})();
Sable Vimes - CSA