Array.push Replacing the old values and keeping the new one only.

Kanwal Nasir
Tera Contributor

Hello, This is my script to get all the incident records which are created today or yesterday, I'm getting the data but the problem is array.push is replacing my old data with a new one and only keeping the new one in response. I have tried to stringify it in many ways still keep the new data only. Please can someone guide me on how to overcome this problem and get the multiple records in an array or work out it will be very helpful.

  var body = {};
 var i=0;
    var grInc = new GlideRecord('incident');
    grInc.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^ORsys_created_onONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()');

    grInc.query();
    while (grInc.next()) {
     
        body.number = grInc.number;
        body.short_description = grInc.short_description;
        body.category = grInc.category;
        body.caller_id = grInc.caller_id;
        body.state = grInc.state;
        body.assignment_group = grInc.assignment_group;
        body.assigned_to = grInc.assigned_to;
		
      result.push(body);
		
		
		
    }
    response.setBody(result);
1 ACCEPTED SOLUTION

DScroggins
Kilo Sage

Hello.

When you loop through a GlideRecord response the variable, in this case grInc, is just a pointer to the current record in the loop hence why you are only getting a single object in your array. You need to get the string value of the pointer using either getValue() or toString() when setting the properties of the object. So update your code to the following:


 var i=0;
    var grInc = new GlideRecord('incident');
    grInc.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^ORsys_created_onONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()');

    grInc.query();
    while (grInc.next()) {
     var body = {};
        body.number = grInc.number.toString();
        body.short_description = grInc.short_description.toString();
        body.category = grInc.category.toString();
        body.caller_id = grInc.caller_id.toString();
        body.state = grInc.state.toString();
        body.assignment_group = grInc.assignment_group.toString();
        body.assigned_to = grInc.assigned_to.toString();
		
      result.push(body);
		
		
		
    }
    response.setBody(result);

 

 

Hope this helps.

--David

View solution in original post

6 REPLIES 6

vkachineni
Kilo Sage
Kilo Sage

The line

var body = {};

 

should be inside the while() loop.

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

DScroggins
Kilo Sage

Hello.

When you loop through a GlideRecord response the variable, in this case grInc, is just a pointer to the current record in the loop hence why you are only getting a single object in your array. You need to get the string value of the pointer using either getValue() or toString() when setting the properties of the object. So update your code to the following:


 var i=0;
    var grInc = new GlideRecord('incident');
    grInc.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^ORsys_created_onONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()');

    grInc.query();
    while (grInc.next()) {
     var body = {};
        body.number = grInc.number.toString();
        body.short_description = grInc.short_description.toString();
        body.category = grInc.category.toString();
        body.caller_id = grInc.caller_id.toString();
        body.state = grInc.state.toString();
        body.assignment_group = grInc.assignment_group.toString();
        body.assigned_to = grInc.assigned_to.toString();
		
      result.push(body);
		
		
		
    }
    response.setBody(result);

 

 

Hope this helps.

--David