Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

The SN Nerd
Giga Sage
Giga Sage

Few follow up questions:

-Is it returning null for all users or just end-users?
If just end-users could be ACL issue preventing read access to location table.

-Are there any JavaScript errors in the browser console?
Other erroneous scripts could cause this script to fail.

-Does the user have a location?

I'm thinking this should be an onChange & on Load script:

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

   if ( newValue == '') {
   	g_form.setValue('location','');
      return;
   }

   // Note the getReference is not Best Practice
   var user = g_form.getReference('requested_for',populateReqForDetails);

	function populateReqForDetails(user) {
		// Note that no display value is provided, resulting in another AJAX call to server
		g_form.setValue('location',user.location);

	}

}

Note: getReference() is actually not good practice, as it results in a round-trip to get the display value when populating reference fields and returns the entire record.
Most optimal would be to use GlideAjax, but this is a more advanced topic.


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022