array.push not working

avinash21
Mega Contributor

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

1 ACCEPTED SOLUTION

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);


View solution in original post

5 REPLIES 5

Chuck Tomasi
Tera Patron

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);


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);


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);


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