Client Script to set location of user

RRolling
Tera Guru

Hello,

Issue: I have a catalog item with a location field which I want to be set on load based on requested_for location. More specifically I want the parent location displayed.

Client script is:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || (g_form.isLiveUpdating && g_form.isLiveUpdating()))
      return;

   if (newValue == '') {
      g_form.setValue('requested_region', '');
      return;
   }
	
   var caller = g_form.getReference('requested_by', setLocation);
}

function setLocation(caller) {
   if (caller)
       g_form.setValue('requested_region', caller.location);
	   //g_form.setValue('phone_number', caller.phone);
}

Currently it loads the city the requested_for but I need the Parent location

find_real_file.png

find_real_file.png

 

I would think its g_form.setValue('requested_region', caller.location.parent); but its not.

 

Any help?

 

2 REPLIES 2

Mike Patel
Tera Sage

For onLoad do

Add default value to variable - gs.getUser().getRecord().getValue('location.parent'); 

for onChange

Client script

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	// Call the GA function
	var ga = new GlideAjax('u_userInfoAjax');
	ga.addParam('sysparm_name', "getInfo");
	ga.addParam('sysparm_user', g_form.getValue('requested_by'));
	ga.getXMLAnswer(function(answer){
		// Save the return to a global variable
		var info = JSON.parse(answer);
		 g_form.setValue('requested_region', info.location);
	   //g_form.setValue('phone_number', info.phone);

	});

}

Script Includes

var u_userInfoAjax = Class.create();
u_userInfoAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getInfo: function(){
		var user = this.getParameter('sysparm_user');
		var gr = new GlideRecord("sys_user");
		gr.get(user);
		var location = gr.location.parent;
		var phone = gr.phone;

		var response = {};
		response.location = location;
		response.phone = phone;

		return JSON.stringify(response);
	},

	type: 'u_userInfoAjax'
});

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can do only 1 level of dot walking using getReference() callback; to go more into deep level of dot walk you would require glideajax as Mike suggested

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