How to get the value of the 'location' field from sys_user tale using client script

synthia
Giga Expert

Hi All ,

I have written a catalog client script to populate the values of the user id logged into the off boarding form . However the user.location field is coming as null. The 'location' field is coming as 'https://dev78523.service-now.com/com.glideapp.servicecatalog_cat_item_view.do?v=1&sysparm_id=4dcaf086db227300100384da0b961990'

Can someone please help me fix it ? I have used DisplayBox.value but it was not working. Below is the code used onLoad

 

function onLoad() {
var user = g_form.getReference('requested_for',populateReqForDetails);

function populateReqForDetails(user) {
alert(user.location);
g_form.setValue('location',user.location);
alert(location);

}
}

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Is your requested_for already populated when form is loaded? what value you are getting in the alert of user.location?

function onLoad() { 
var user = g_form.getReference('requested_for',populateReqForDetails);

function populateReqForDetails(user) {
alert(user.location);
g_form.setValue('location',user.location);


}
}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Hi Ankur ,

Yes , requested_for is populated onLoad. I am getting null for the alert(user.location).

Hi,

Can you try to give alert for requested_for variable?

also does that user have location populated in user table?

function onLoad() { 

alert(g_form.getValue('requested_for'));
var user = g_form.getReference('requested_for',populateReqForDetails);

function populateReqForDetails(user) {
alert(user.location);
g_form.setValue('location',user.location);


}
}

Also check the user with which you are testing is having access to location table i.e. cmn_location.LIST

see whether it shows records or not or read ACL is blocking the data for that user

Regards

Ankur

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

Sanket Khabiya
Kilo Sage

Hi Synthia,

Can you try below client script:

function onLoad(){

var user = g_form.getReference('requested_for',doAlert); // doAlert is our callback function

}

function doAlert(caller) { //reference is passed

alert(user.location);

if (user.location != ''){
g_form.setValue('location',user.location);

}

}