What is the best way to get the record?

a41022639
Tera Contributor

Hello Community,

 

I just want to know, if I need to fetch only one record. What would be the best approach.

2 ACCEPTED SOLUTIONS

Sandeep Rajput
Tera Patron
Tera Patron

@a41022639 The best way to get a single record from database is using .get() method. Here is an example of it

 

var grIncident = new GlideRecord('incident');
var returnValue = grIncident.get('99ebb4156fa831005be8883e6b3ee4b9');
gs.info(returnValue); // logs true or false
gs.info(grIncident.number); // logs Incident Number

For more information please refer to https://developer.servicenow.com/dev.do#!/reference/api/vancouver/server_legacy/c_GlideRecordAPI#r_G...(

View solution in original post

Abhishek_Thakur
Mega Sage

Hello @a41022639 ,

 

1.The more focused way to retrieve only one record is with the help setLimit(1) function in GlideRecord.

 

var Inc = new GlideRecord('incident');

inc.addQuery(); // add your condition here

inc.setLimit(1);

if(inc.next()){

gs.info("Incident number is " + inc.number);

}

 

2. You can go with the get() function in GlideRecord();

 

Please mark my answer as accepted solution and give thumbs up.

 

Regards,

Abhishek Thakur

View solution in original post

3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@a41022639 The best way to get a single record from database is using .get() method. Here is an example of it

 

var grIncident = new GlideRecord('incident');
var returnValue = grIncident.get('99ebb4156fa831005be8883e6b3ee4b9');
gs.info(returnValue); // logs true or false
gs.info(grIncident.number); // logs Incident Number

For more information please refer to https://developer.servicenow.com/dev.do#!/reference/api/vancouver/server_legacy/c_GlideRecordAPI#r_G...(

Abhishek_Thakur
Mega Sage

Hello @a41022639 ,

 

1.The more focused way to retrieve only one record is with the help setLimit(1) function in GlideRecord.

 

var Inc = new GlideRecord('incident');

inc.addQuery(); // add your condition here

inc.setLimit(1);

if(inc.next()){

gs.info("Incident number is " + inc.number);

}

 

2. You can go with the get() function in GlideRecord();

 

Please mark my answer as accepted solution and give thumbs up.

 

Regards,

Abhishek Thakur

AnimeshP_96
Tera Guru

@a41022639 
 so I am just assuming you would use gliderecord and use "IF" and also use SetLimit(number)..

so that it will be restricted to only one record.

let me know if this works.


Please accept the solution /mark this response as correct or helpful if it assisted you with your question.




Regards,
Animesh