How to get reference list type value from server side script

SystemsGo
Tera Contributor

Hi experts,

 

In table [customer_account], we created a customized field u_service_location with type of list and reference to another table [cmn_location],

 

but in service side script when I use the following list, I always get "" or null for that field.

 

I'm sure there has value in that field. How can I get value from that field?

 

var accountRec = new GlideRecord("customer_account"); 
accountRec.get(accountId); 
var aa= accountRec.u_service_location;// return "" 
var bb= String(accountRec.u_service_location);// return "" 
var cc= JSON.stringify(accountRec.u_service_location);// return {} 
var dd= accountRec.u_service_location.toString();// return "" 
var ee= accountRec.getValue("u_service_location");// return null

 

3 ACCEPTED SOLUTIONS

ankitbanerj
Tera Expert

Hi @SystemsGo ,
try this -- as this is an async call you need to check next().

var accountRec = new GlideRecord("customer_account"); 
accountRec.get(accountId); 
if (accountRec.next()){
var ee= accountRec.getValue("u_service_location");
}


If you found this response helpful, please mark it as the accepted solution and give it a ‘Helpful’ rating. Your feedback supports better answers and benefits the entire community.

View solution in original post

Maik Skoddow
Tera Patron
Tera Patron

Hi @SystemsGo 

I don't know what "accountId" is, however when using the get() method of GlideRecord you ALWAYS have to check if there was a result. Otherwise you would work on an empty object (which could be the case for you).

 

var accountRec = new GlideRecord("customer_account"); 
if (accountRec.get(accountId)) {
  var aa= accountRec.u_service_location;// return "" 
  var bb= String(accountRec.u_service_location);// return "" 
  var cc= JSON.stringify(accountRec.u_service_location);// return {} 
  var dd= accountRec.u_service_location.toString();// return "" 
  var ee= accountRec.getValue("u_service_location");// return null
}
else {
  gs.error('No record found!');
}

 Maik

View solution in original post

@SystemsGo 

I believe I shared similar code.

If my response helped please mark it correct as well so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

8 REPLIES 8

ankitbanerj
Tera Expert

Hi @SystemsGo ,
try this -- as this is an async call you need to check next().

var accountRec = new GlideRecord("customer_account"); 
accountRec.get(accountId); 
if (accountRec.next()){
var ee= accountRec.getValue("u_service_location");
}


If you found this response helpful, please mark it as the accepted solution and give it a ‘Helpful’ rating. Your feedback supports better answers and benefits the entire community.

@ankitbanerj 

This is not an async call!

I've just tried, it still return "";

Maik Skoddow
Tera Patron
Tera Patron

Hi @SystemsGo 

I don't know what "accountId" is, however when using the get() method of GlideRecord you ALWAYS have to check if there was a result. Otherwise you would work on an empty object (which could be the case for you).

 

var accountRec = new GlideRecord("customer_account"); 
if (accountRec.get(accountId)) {
  var aa= accountRec.u_service_location;// return "" 
  var bb= String(accountRec.u_service_location);// return "" 
  var cc= JSON.stringify(accountRec.u_service_location);// return {} 
  var dd= accountRec.u_service_location.toString();// return "" 
  var ee= accountRec.getValue("u_service_location");// return null
}
else {
  gs.error('No record found!');
}

 Maik