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

Danish Bhairag2
Tera Sage
Tera Sage

Hi @1dusjhyahnt ,

 

Do you want this to happen on Catalog Item form? As per the screenshot believe that is what u require. If yes then You can use Auto populate feature of ServiceNow where while creating any variable there is a tab for autopopulate where u can specify the field names & table from where it should auto populate . It will provide u the result. No coding required for this.

 

Thanks,

Danish

 

HI @Danish Bhairag2 

 

USER requirement is that he want a editable table in portal when he update the value then automatically updation occur in artificatory table ?

Vishal Birajdar
Giga Sage

Hello @1dusjhyahnt 

 

Can you try like below :

Have done for demo on incident table - 

(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('incident');
	grInc.addQuery('sys_id','45b29e1b3b101300088d832b44efc492');
	grInc.query();

	if(grInc.next()){
		/*2.1 Create empty object*/
		var myObject = {
			
		};
               /*2.2 Set object values*/
		myObject.number = grInc.getValue('number');
		myObject.state = grInc.getValue('state');
		/*2.3 Push object in array*/
		myArray.push(myObject);
	}
	/*3. Set array to data */
	data.values = myArray;
	   
	      gs.addInfoMessage(data.values[0].number);
		gs.addInfoMessage(data.values[0].state);

})();

 

 

VishalBirajdar_0-1707119472203.png

 

 

 

 

Vishal Birajdar
ServiceNow Developer

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

hii @Vishal Birajdar 

 

(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.addQuery('sys_id','458688b893c04210e4f0398efaba10e7');
grInc.query();
 
if(grInc.next()){
/*2.1 Create empty object*/
var myObject = {
 
};
               /*2.2 Set object values*/
myObject.number = grInc.getValue('u_name');
myObject.state = grInc.getValue('u_class');
/*2.3 Push object in array*/
myArray.push(myObject);
}
/*3. Set array to data */
data.values = myArray;
   
      gs.addInfoMessage(data.values[1].u_name);
gs.addInfoMessage(data.values[1].u_class);
 
})();
 
Not Working for me