- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2017 02:10 PM
I currently have one Script Include that returns undefined when I try to create an instance of it.
However, when I copy all of the code into a new Script Include in the Global scope (replacing the name), it works perfectly.
I have made the Script Include available in all scopes as well.
I've tried including/excluding the scope in the construction call, and have made it Client Callable, but only the copy in the Global scope is working.
Here's the simple Fix Script to try and test this:
Fix Script |
---|
var temp = []; // var api = new x_uocd2_ucd_portal.ScopedPortalAPI(); This does not work var api = new global.TestAPI(); // This does work api.getKBCategoryArticles('9d4737cbff0221009b20ffffffffff33', temp); for (var i = 0; i < temp.length; ++i) gs.print(temp[i].short_description); |
Here's the Script Include - same code in Global Scope and my custom scope:
Script Include |
---|
var ScopedPortalAPI = Class.create(); ScopedPortalAPI.prototype = Object.extendsObject(global.AbstractAjaxProcessor, { /** * Returns KB articles in the specified category and any of its subcategories. * @param id Sys_id of the specified category * @param array Passed in array to save articles in * @return Array of article objects w/ data members */ getKBCategoryArticles: function(id, array) { var kb_cat = new GlideRecord('kb_category'); kb_cat.get(id); // Grab any articles within this category this._getKBArticles(kb_cat.label, array); // Go down any subcategories within this category var children = new GlideRecord('kb_category'); children.addActiveQuery(); children.addQuery('parent_id', kb_cat.getUniqueValue()); children.query(); while (children.next()) this.getKBCategoryArticles(children.getUniqueValue(), array); }, /** * Helper method for getKBCategoryArticles(). Retrieves all articles within the specified category. * @param label Label of the specified category * @return Array of article objects w/ data members */ _getKBArticles: function(label, array) { var articles = new GlideRecord('kb_knowledge'); articles.addActiveQuery(); articles.addQuery('kb_category.label', label); articles.query(); while (articles._next()) { array.push({ short_description: articles.short_description.getDisplayValue(), sys_view_count: articles.sys_view_count.getDisplayValue(), author: articles.author.getDisplayValue(), rating: articles.rating.getDisplayValue(), published: articles.published.getDisplayValue(), type: articles.type.getDisplayValue(), title: articles.title.getDisplayValue(), sys_id: articles.sys_id.getDisplayValue(), number: articles.number.getDisplayValue(), _table_name: articles._table_name.getDisplayValue(), text: articles.text.getDisplayValue() }); } }, type: 'ScopedPortalAPI' }); |
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2017 03:06 PM
In my system, the kb_knowledge table does not have the following fields in your code:
- type
- _table_name
- title
When running in an application scope, the code fails when it tries to call 'getDisplayValue()' on these fields that do not exist.
When running in global, the incorrect code is happy to continue on without throwing an errors.
I suspect this is because ScopedGlideRecord and/or ScopedGlideElement are slightly different.
This code worked for me:
_getKBArticles: function(label, array) {
var articles = new GlideRecord('kb_knowledge');
articles.addActiveQuery();
articles.addQuery('kb_category.label', label);
articles.query();
while (articles._next()) {
array.push({
short_description: articles.short_description.getDisplayValue(),
sys_view_count: articles.sys_view_count.getDisplayValue(),
author: articles.author.getDisplayValue(),
rating: articles.rating.getDisplayValue(),
published: articles.published.getDisplayValue(),
//type: articles.type.getDisplayValue(),
//title: articles.title.getDisplayValue(),
sys_id: articles.sys_id.getDisplayValue(),
number: articles.number.getDisplayValue(),
//_table_name: articles._table_name.getDisplayValue(),
text: articles.text.getDisplayValue()
});
}
},
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2017 03:06 PM
In my system, the kb_knowledge table does not have the following fields in your code:
- type
- _table_name
- title
When running in an application scope, the code fails when it tries to call 'getDisplayValue()' on these fields that do not exist.
When running in global, the incorrect code is happy to continue on without throwing an errors.
I suspect this is because ScopedGlideRecord and/or ScopedGlideElement are slightly different.
This code worked for me:
_getKBArticles: function(label, array) {
var articles = new GlideRecord('kb_knowledge');
articles.addActiveQuery();
articles.addQuery('kb_category.label', label);
articles.query();
while (articles._next()) {
array.push({
short_description: articles.short_description.getDisplayValue(),
sys_view_count: articles.sys_view_count.getDisplayValue(),
author: articles.author.getDisplayValue(),
rating: articles.rating.getDisplayValue(),
published: articles.published.getDisplayValue(),
//type: articles.type.getDisplayValue(),
//title: articles.title.getDisplayValue(),
sys_id: articles.sys_id.getDisplayValue(),
number: articles.number.getDisplayValue(),
//_table_name: articles._table_name.getDisplayValue(),
text: articles.text.getDisplayValue()
});
}
},
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2017 03:11 PM
Wow, thanks Paul!
I was actually just trying to recreate an API offered via $sp, but for my scoped application (doesn't work outside the global), hence throwing those un-existing columns.
Very interesting that the non-scoped version continues along without error. But yes, commenting those lines out definitely solved the issue. Good catch

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2017 03:16 PM
I used the Xplore Update Set from ServiceNow share to quickly deduce this
It is a very useful tool.
Be sure the delete those commented out lines so you don't confuse other developers!
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022