Convert reference field LOCATION to display name instead of Sys ID

IB98
Giga Sage

HI All, 

(Catalog Item) I am trying to get the variable 'location' to auto populate with the requester information. Initially, the location populates with the signed in requester because I have a default javascript in place that does that. If I change the user, the OnChange script is supposed to set the value for that user, instead it displays the sys id. I did check the display on the location table and it's set to true on name. I am out of ideas. Please help!

I will include the variable default code and the Catalog client script. Also, the location variable is set to a single line text variable.

*I have tried both options that are commented out. The above script works for phone and email variables. 

 find_real_file.png

*Location (Single line text variable) to populate 'Requester' (variable set variable) Location  

find_real_file.png

Thanks, 

1 ACCEPTED SOLUTION

IB98
Giga Sage

Thank you all for your help! I used this code and it helped.

Script Include 

var getLocation = Class.create(); 

getLocation.prototype = Object.extendsObject(AbstractAjaxProcessor, { 

    get_location: function() { 

        var gr = new GlideRecord('sys_user'); 

        gr.addQuery('sys_id', this.getParameter('sysparm_userID')); 

        gr.query(); 

        if (gr.next()) { 

            return gr.location.getDisplayValue(); 

        } 

    }, 

    type: 'getLocation' 

}); 

OnChange Catalog Client script 

function onChange(control, oldValue, newValue, isLoading) { 

if (isLoading || newValue === '') { 

return; 

} 

  

    var ga = new GlideAjax('getLocation'); 

    ga.addParam('sysparm_name', 'get_location'); 

    ga.addParam('sysparm_userID', g_form.getValue('requester')); //the variable name from the variable set

    ga.getXMLAnswer(function(answer){ 

       //if(answer != ''){ 

       g_form.setValue('location', answer); 

       //} 

    }); 

} 

View solution in original post

7 REPLIES 7

Saurav11
Kilo Patron
Kilo Patron

Hello,

Is location a string variable on the catalog.

If yes please use the below in your client script for location

g_form.setValue('location',usr.location.name);

Please mark answer correct/helpful based on impact.

Are you sure usr.location.name (i.e. double dot-walk) will work with getReference()? I doubt so.

@Ilvana if you do not wish to use GlideAjax you can try using getReferenceAdvanced. Follow link for a check

 

hello,

Yes you are correct. My bad 🙂

Abhijit4
Mega Sage

Hi,

You will need to understand getReference method concept here. GetReference method returns gliderecord for the user record(in your case) where you can only access values which are there on user records(upto one dot walk e.g usr.phone or usr.email as you already have used), you can only access usr.location which will return sys_id of the location. Here you are trying to access location display value, it means you are trying to go second dot walk level that is usr.location.getDisplayValue() so this will not work.

I would suggest you to go for another getReference method to get Location glide object, not sure if this works or not but give a try.

e.g.

var location=g_form.getReference("usr.location",getLocation);

function getLocation(location){

}

If above doesn't work then you would need to go with GlideAjax.

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP