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

Ankur Bawiskar
Tera Patron
Tera Patron

@SystemsGo 

try this and it should work if accountId is sysId of that account record

var accountRec = new GlideRecord("customer_account");
accountRec.addQuery('sys_id', accountId);
accountRec.query();
if (accountRec.next()) {
    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

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

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

Hi Ankur,

 

Thank you so much for your reply.

 

I just tried it. Still return "", And the query can get return result, also there's value stores in "u_service_location" field

      var accountRec = new GlideRecord("customer_account");
	//accountRec.get(accountId);
	accountRec.addQuery("sys_id",accountId);
	accountRec.query();
	var serLoc = "";
	if(accountRec.next()){
		serLoc = accountRec.u_service_location;
	}
	gs.info("-====serLoc==="+serLoc);

 

SystemsGo
Tera Contributor

Thank you for all of your replies.

 

When I use blow code, it worked.

 

Thank you for all

if (accountRec.next()){

 

@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