We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Push all table field data into JavaScript array

xiaix
Tera Guru

Here's an example:

function get_BCP_Site_Data(userLocation_sysID)

{

      var siteDATA = [];

      var gr = new GlideRecord('u_bcpa_sites');

      gr.addQuery('u_active', true);

      gr.addQuery('u_location', userLocation_sysID);

      gr.query();

      if (gr.next())

      {

              siteDATA.push({

                      city:gr.u_city.toString(),

                      state:gr.u_state.toString(),

                      zip:gr.u_zip.toString()

              });

      }

      return siteDATA;

}

Above, I'm only exampling 3 fileds, city, state and zip.   However, I have about 75 more I need data from.

Is there any way to get all the fields without explicitly referencing each field?

1 ACCEPTED SOLUTION

Or... here's the ES5 way to do it to get dynamic array keys:



function get_BCP_Site_Data(userLocation_sysID)


{


      var siteDATA = [];


      var gr = new GlideRecord('u_bcpa_sites');


      gr.addQuery('u_active', true);


      gr.addQuery('u_location', userLocation_sysID);


      gr.query();


      if (gr.next())


      {


              var fields = new GlideRecordUtil().getFields(gr);


              var fieldName = '';        


              for (var i = 0; i < fields.length; i++)


              {


                      var obj = {};


                      fieldName = fields[i];


                      var thetop = fieldName;


                      obj[fieldName] = gr.getDisplayValue(fieldName);


                      siteDATA.push(obj);


              }


      }


      return siteDATA;


}




find_real_file.png


View solution in original post

7 REPLIES 7

Or... here's the ES5 way to do it to get dynamic array keys:



function get_BCP_Site_Data(userLocation_sysID)


{


      var siteDATA = [];


      var gr = new GlideRecord('u_bcpa_sites');


      gr.addQuery('u_active', true);


      gr.addQuery('u_location', userLocation_sysID);


      gr.query();


      if (gr.next())


      {


              var fields = new GlideRecordUtil().getFields(gr);


              var fieldName = '';        


              for (var i = 0; i < fields.length; i++)


              {


                      var obj = {};


                      fieldName = fields[i];


                      var thetop = fieldName;


                      obj[fieldName] = gr.getDisplayValue(fieldName);


                      siteDATA.push(obj);


              }


      }


      return siteDATA;


}




find_real_file.png


P-Rudenko-SN
ServiceNow Employee

Hello,

I'm wondering, how can we push only specific fields to the array instead of all?

Any ideas?

 

Regards,

Pavlo

Fatima Ben Zaid
Tera Contributor
I have a problem with this script it show me just the first conference, it doesn't show me all the conferences that check the condition, i try to make it on arraylist but it doesn't work too .
 
(function execute() {

 

var gr = newGlideRecord("u_conferences");
gr.query();
while (gr.next()) {
 
if(gs.getUser().isMemberOf(gr.getValue('u_acronym'))){
 
return gr.u_acronym;
}
 
}
})()