- 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 07:02 AM
Always use getValue() (or in the case of reference fields, getDisplayValue() ) when using GlideRecords inside a loop.
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.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);

- 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 07:16 AM
I saw that, but it didn't click (still early for me). Yeah, you need to create a new body object inside the loop for each record. Really, you don't need to declare it outside the loop either since it's not used outside the loop any other time.
var queryParams = request.queryParams;
var base_url = gs.getProperty('glide.servlet.uri');
var urlArray = [];
var kb = new GlideRecord('kb_knowledge');
kb.addQuery('short_description', 'CONTAINS', queryParams.searchfor);
kb.query();
kb.next();
while(kb.next()) {
var 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);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2017 07:16 AM
If I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.
If you are viewing this from the community inbox you will not see the correct answer button. If so, please review How to Mark Answers Correct From Inbox View.
Thank you