The CreatorCon Call for Content is officially open! Get started here.

Write a code for server script for fetching a custom table value ?

1dusjhyahnt
Tera Contributor

Screenshot 2024-02-05 at 10.41.50 AM.jpegScreenshot 2024-02-05 at 1.05.00 PM.jpegScreenshot 2024-02-05 at 10.36.03 AM.jpeg

16 REPLIES 16

HII @Vishal Birajdar 

 

how to fetch all the record without hardcoded sys_id means there are 5 record in table , i want to fetch all the record how is it possible ?

Hello @1dusjhyahnt ,

 

Kindly use below updated to fetch all the records

(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */

/*1. Declare the array*/
var myArray =[];
/*2. Glide record on table*/
var grInc = new GlideRecord('u_artificatory');
grInc.query();

while(grInc.next()){
/*2.1 Create empty object*/
var myObject = {

};
/*2.2 Set object values*/
myObject.name = grInc.getValue('u_name');
myObject.class_name = grInc.getValue('u_class'); //updated
/*2.3 Push object in array*/
myArray.push(myObject);
}
/*3. Set array to data */
data.values = myArray;

for(var i=0;i<myArray.length; i=i+1)   // To iterate to all 5 records 
{
gs.addInfoMessage(data.values[i].name);
gs.addInfoMessage(data.values[i].class_name);
}
})();

 

Hello @1dusjhyahnt 

 

Is there any chance there will be more record in near future on that table e.g, if 10 more records get added and you want only that 5 specific records....??

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

hi @Vishal Birajdar 

 

yes , in future we will added more record 

Hello @1dusjhyahnt 

 

Ok....so I'm assuming you want only those five record from 10-15 records  

then do like this below or else you can use logic provided by @siva krishna M2 

 

1. Create a system property in 'sys_properties' table 

e.g, Name = custom.my.sys_ids and add those five records sys_ids as comma separated  in property.

 

2. Call that property in server side code like below :

gs.getProperty('your_property_name'); 

 

 

(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */

      /*1. Declare the array & get property*/
var myArray =[];
var mySysIds = gs.getProperty('your_property_name');   //updated

/*2. Glide record on table*/
var grInc = new GlideRecord('u_artificatory');
grInc.addEncodedQuery('sys_idIN' + mySysIds);   //updated
grInc.query();

while(grInc.next()){    //updated
/*2.1 Create empty object*/
var myObject = {

};
               /*2.2 Set object values*/
myObject.name = grInc.getValue('u_name');   
myObject.class_name = grInc.getValue('u_class');   
/*2.3 Push object in array*/
myArray.push(myObject);
}
/*3. Set array to data */
data.values = myArray;

   //Use for loop to get all the values
      gs.addInfoMessage(data.values[0].name);  
gs.addInfoMessage(data.values[0].class_name);  
})();

 

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates