Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Client script onChange

snowuser111
Kilo Guru

Hi,

Can anyone help please.

I have a reference field(sys_user) in incident . When a user is selected automatically the other field (u_user_manager) should populate the manager of the user.

Thanks

1 ACCEPTED SOLUTION

manikorada
ServiceNow Employee

You need to have a Client Script as:



  1. function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  2.     if (isLoading || newValue == '') {
  3.           return;
  4.     }
  5.     var user = g_form.getReference('<<fieldname>>');
  6.     g_form.setValue('u_user_manager', user.manager);
  7.    
  8. }

View solution in original post

17 REPLIES 17

manikorada
ServiceNow Employee

You need to have a Client Script as:



  1. function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  2.     if (isLoading || newValue == '') {
  3.           return;
  4.     }
  5.     var user = g_form.getReference('<<fieldname>>');
  6.     g_form.setValue('u_user_manager', user.manager);
  7.    
  8. }

Pradeep Sharma
ServiceNow Employee

Hi Snowuser,



You can achieve this req with the help of Dot-Walking. Here is the more details.


http://wiki.servicenow.com/index.php?title=Dot-Walking


Alternatively you can use the below client script


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


    if (isLoading)


          return;




    if (newValue == '') {


          g_form.setValue('u_user_manager', '');


          return;


    }




    if (!g_form.getControl('u_user_manager'))


          return;




    var caller = g_form.getReference('caller_id', setManager);


}




function setManager(caller) {


    if (caller)


            g_form.setValue('u_user_manager', caller.manager);


}













Thanks all for the script. The scripts are working but only issue is I am getting sys_id of the manager in the field, not the name


snowuser11,



Check if the manager field is a reference field to user table. if not please change it like that.


Make sure the manager is a reference field to the sys_user table.