Help with script to auto populate Manager fields

nicolemccray
Tera Expert

I have a script that will populate Manager info based on the user logged in:

function onLoad() {
    //Type appropriate comment here, and begin script below
          var id = g_form.getValue('superv_fname');//replace 'u_first_field' with the name of your reference field.
var requester = '';

var user = new GlideRecord('sys_user');  

          user.addQuery('sys_id',id);  

          user.query();  

if ( user.next() ) {    

  g_form.setValue('superv_emp_id', user.employee_number);
g_form.setValue('superv_email', user.email);
g_form.setValue('superv_logid', user.user_name);
g_form.setValue('superv_title', user.title);

}  
}

But I need the information update to reflect the correct values if request is for another user.   I have another script I've used for this, but it will only update to reflect the 'requestor' information (email, job title, etc.), not the Manager's:

function onChange(control,
oldValue, newValue, isLoading)

{

var requester = '';

if(g_form.getValue('ConfirmInfoReqOtherChoice')
== 'No') // Which means user requesting request for himself

{

requester = g_user.userID; // This
gives login user id  

}

else

{

requester =
g_form.getValue('ConfirmInfoReqName');

}

var user = new
GlideRecord('sys_user');

  1. user.addQuery('sys_id',requester);
  2. user.query();

if ( user.next() )

{


g_form.setValue('manager', user.manager);

}

}

Is there a way to combine these to get what I need?

7 REPLIES 7

peterhansen
Kilo Expert

Hey Nicole



There is no need for a OnLoad client script. You can put the following in on the default value of the field: javascript:gs.getUser().getManagerID()



Let me know if it worked.


Thank you! I was struggling for quite sometime and this worked!

Darshani Sambar
Giga Contributor

Hi nicolemccray,

Set default value as: javascript:gs.getUser().getManagerName()

Please refer below screenshot.

 

Please mark helpful/correct based on impact.

 

Thanks & Regards

Darshani Sambare