How to set the value in the reference field when you have name

shaik_irfan
Tera Guru

Hi

I have a value like which is the name of the location which i need to set in the refernence field which is location.

 

Can i do like below?

 

gr.u_location = "India"; // u_location is a reference field

1 ACCEPTED SOLUTION

johnfeist
Mega Sage
Mega Sage

Hi Shaik Irfan,

What you need to do is something like this:

var locations = new GliedRecord("cmn_location");
locations.get("name",<variable or literal with the name>);
gr.setValue("u_location", locations.sys_id);

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

View solution in original post

6 REPLIES 6

johnfeist
Mega Sage
Mega Sage

Hi Shaik Irfan,

What you need to do is something like this:

var locations = new GliedRecord("cmn_location");
locations.get("name",<variable or literal with the name>);
gr.setValue("u_location", locations.sys_id);

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

ersureshbe
Giga Sage
Giga Sage

Hi,

If location is reference field you should set sys_id as the value. ie., you should set 'india' sys_id to the gr.u_location field.

You are not aware of sys_id of india then you should glide the value from required table.

I'm not sure which table it holds the value. 

Regards,

Suresh.

Regards,
Suresh.

Saiganeshraja
Kilo Sage
Kilo Sage

function ID(val){
var s= new GlideRecord('cmn_location');

s.addQuery('name',val);

s.query();

if(s.next())

{

return s.sys_id;

}

}

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

if you are sure the location name is exact then you can use setDisplayValue to set the display value

gr.setDisplayValue('u_location', 'India');

Regards
Ankur

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