Need client script to autopopulate manager's manager name based on user field

User_267
Tera Contributor

Hi all, 

I have two fields- user and manager.

Ex user: system admin manager is Abel tutor and Abel tutor manager is abhraham lincoln.

In user field(reference type) if we select system admin.

Then in manager field it needs to populate system admin manager's manager(Abraham Lincoln).

I have below script for manager name ,but don't know how to modify for manager's manager.

1 REPLY 1

Anirudh Pathak
Mega Sage

Hi @User_267 ,

You won't be able to double dot walk in the client script. Also it's best practice to use GlideAjax for server side calls in the client script.

Please use the below code to get manager's manager. You can make necessary changes according to the field names on your form. Make sure your script include is client callable.

 

Script include -

 

getData: function() {
		var id = this.getParameter('sysparm_user');
		var ans = '';
		var userManager = new GlideRecord('sys_user');
		if(userManager.get(id)) {
			ans = userManager.manager.manager;
		}
		return ans;
		
	}

 

Client Script -

 

 

var caller = g_form.getValue('your_user_field_name');
    var user = new GlideAjax('your_script_include_name');
    user.addParam('sysparm_name', 'getData');// script include function name
    user.addParam('sysparm_user',caller)
    user.getXMLAnswer(getInfo);

    function getInfo(response) {
        g_form.setValue('manager', response);

    }