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

Rama Chandra D
Kilo Guru

Hi Nicole,



I don't see where you've used the 'requester' variable in your scripts.Also, do you want the fields to auto populate based on the user or the value in the superv_fname field, because I see you've queried the user table with supervisor's id rather than the users.



I wouldn't suggest a GlideRecord on client scripts. You could use, call back functions in the client script.



function onLoad() {


  //Type appropriate comment here, and begin script below


  g_form.getReference("superv_fname", function(gr){


  g_form.setValue("superv_email'", gr.email);


  g_form.setValue("superv_phone", gr.phone);


//add the other fields you want here.


  });


}



You could use the same script for onChange as well. So, whenever the superv_fname changes, the phone and other fields are populated.



Darshak


This worked great to populate the Supervisor fields, but I used the same script for 'onChange' to update if Supervisor changed, and it is not working.   I think I'm missing the part of the script to update the Supervisor first (superv_fname) based on the user indicated in 'ConfirmInfoReqName', and then update the other fields based on that:





find_real_file.png


why don't you use glide ajax instead of call back function


Hey Nicole,



Can you try this,



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


g_form.getReference("superv_fname", function(gr){


  g_form.setValue("superv_email'", gr.email);


  g_form.setValue("superv_phone", gr.phone);


//add the other fields you want here.


  });


}



Darshak