Write a code for server script for fetching a custom table value ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-04-2024 11:37 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-05-2024 09:24 PM
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 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-05-2024 09:56 PM
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);
}
})();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-05-2024 10:06 PM
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....??
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-05-2024 10:19 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-05-2024 10:30 PM
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);
})();
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates