getReference value

Prerna_T
Tera Contributor

Hello Experts!!

I am trying to fetch location from sys_user table to incident table in a custom field using getRefernce, but it is showing sys_id of the location.

Can anyone help ?

1 ACCEPTED SOLUTION

Maddysunil
Kilo Sage

@Prerna_T 

If you're using getReference() to fetch the location from the sys_user table to the incident table in a custom field, it's expected that you're seeing the sys_id of the location. This is because getRefernce() retrieves the reference field value, which is typically the sys_id of the referenced record.

 

 

 

 var locationSysId = current.location.toString(); // Assuming 'location' is the reference field name

    // Fetch the location record
    var locationRecord = new GlideRecord('sys_user');
    if (locationRecord.get(locationSysId)) {
        // Populate the location name in the custom field
        current.custom_location_field = locationRecord.getValue('location'); // Assuming 'custom_location_field' is the custom field name
    }

 

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

View solution in original post

6 REPLIES 6

Harish KM
Kilo Patron
Kilo Patron

Hi @Prerna_T location is a reference field in user table, so you need to dot walk location.name to get Name of the location. Also using getReference() is a bad practice, you need to use script include and client script using glideAjax call,

 

 

Regards
Harish

Sandeep Rajput
Tera Patron
Tera Patron

On which field you are trying to use GetReference?

 

 

Deepak Shaerma
Kilo Sage

Hello @Prerna_T 

when you’re using getReference to fetch information related to a reference field (like pulling location data from sys_user into an incident record), it indeed fetches the entire record, including the sys_id by default. However, if you want to display a human-readable value (like the name of the location) rather than the sys_id, you need to specifically access that field from the fetched record. 

use getDisplayValue() or specific field name 'name' 
for example :

 

 var locationGR = new GlideRecord('cmn_location');
            if (locationGR.get(user.location)) {
current.field_name = locationGR.getDisplayValue(); // Or locationGR.getValue('name');

 

Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma

Astik Thombare
Tera Sage

Hi @Prerna_T ,

 

You need to change the type of that custom field to reference type and it should reference to location table .then only it will populate display value and if the custom field is of string type consider writing glideAjax .

 

For more information on glideajax please follow below link -

 

https://developer.servicenow.com/dev.do#!/learn/learning-plans/utah/new_to_servicenow/app_store_lear...

 

If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

 

Thanks,

Astik