- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2017 06:52 AM
Hi All
I was trying the below code in the scripted web service
var queryParams = request.queryParams;
var base_url = gs.getProperty('glide.servlet.uri');
var urlArray = [];
var body = {};
var kb = new GlideRecord('kb_knowledge');
kb.addQuery('short_description', 'CONTAINS', queryParams.searchfor);
kb.query();
kb.next();
while(kb.next())
{
body.number=kb.number.toString();
base_url=base_url+"kb_knowledge"+'.do?sys_id='+ kb.sys_id;
body.url=base_url.toString();
urlArray.push(body);
}
response.setBody(urlArray);
The issue here is the response is repeating same number multiple times, the array.push does not seems to work. Ideally it should show different KB articles inspite of repeating the same one
.Can somebody plz help on the below
Thanks
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2017 07:12 AM
In addition to what Chuck said, also refreshing the body object was necessary for me to get this working in my instance.
See line 11 below.
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var queryParams = request.queryParams;
var base_url = gs.getProperty('glide.servlet.uri');
var urlArray = [];
var body = {};
var kb = new GlideRecord('kb_knowledge');
kb.addQuery('short_description', 'CONTAINS', queryParams.searchfor);
kb.query();
while(kb.next()){
body = {};
body.number=kb.getValue('number');
base_url=base_url+"kb_knowledge"+'.do?sys_id='+ kb.getValue('sys_id');
body.url=base_url.toString();
urlArray.push(body);
}
response.setBody(urlArray);
})(request, response);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2017 02:24 PM
Just following on to what ctomasi rightly pointed out, I would strongly recommend always using getter/setters when dealing with GlideElement objects, for several reasons. In this case (inside a loop), the primary reason is because of a quirk (aka "feature") of JavaScript called pass-by-reference. Here is an article with a bunch of detail on why that is, and especially on pass-by-reference.
A couple of related points and pro-tips:
- On the server, every GlideRecord field-element (such as gr.short_description) is a GlideElement object. You rarely see GlideElement objects outside of GlideRecord objects, or instantiate them yourself, so many folks don't know that there is actually decent documentation on them, here.
- On the client, there is no GlideElement API, BUT it is still a good idea to be in the habit of using getters and setters!
- There ARE good reasons to directly access the GlideElement object, but you should virtually never directly set another variable to that object.
- Use the GlideElement object, not .getValue(), to access journal fields' values.
- The GlideElement object has useful methods like .toString() (which emulates the result of .getValue(), by returning a string primitive) and .getDisplayValue() (which is great for time, reference, and choice fields).