How to use a reference field in the if condition in client scripts

varunkumar1
Tera Contributor

Hi,

I have a reference field 'employee'(which is fetching records from User table) on a form. I have few more string fields like level, status etc..

Now I want to build the functionality like if 'employee' field is filled with some value say "Sample User", then the above fields should be auto filled with level as 3 and status as 'Current Employee'

I am trying to write a client script, but I am not able to achieve this functionality by using reference field in the if condition in the below script..

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

    if (isLoading || newValue === '') {

          return;

    }

var p = g_form.getValue('u_employee');

  //alert(g_form.getValue('u_employee'));

if (p && p = 'Sample User')

{

g_form.setValue('u_status', 'Current Employee');

g_form.setValue('u_level', '3');

}

Is it not correct to reference field in if condition? Also when I tried to display the employee value, it is showing me the sysId but not the actual value. Please help me out what is happening in this case.

Also please let me know how can I achieve my functionality.

Thanks

1 ACCEPTED SOLUTION

Steven1
Tera Expert

You need to create a "OnChange" Client script and use a call back function.




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


  if(!isLoading && oldValue != newValue){


  g_form.getReference('caller_id',changeUserInfo);


  }


}




function changeUserInfo(userInfo){


  g_form.setValue('u_incident_email', userInfo.email);


  g_form.setValue('u_phone',userInfo.phone);


  g_form.setValue('u_wing', userInfo.u_wing);


  g_form.setValue('location', userInfo.location);


  g_form.setValue('u_room', userInfo.u_room);


  g_form.setValue('u_floor', userInfo.u_floor);


}








more info : GlideForm (g form) - ServiceNow Wiki


View solution in original post

1 REPLY 1

Steven1
Tera Expert

You need to create a "OnChange" Client script and use a call back function.




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


  if(!isLoading && oldValue != newValue){


  g_form.getReference('caller_id',changeUserInfo);


  }


}




function changeUserInfo(userInfo){


  g_form.setValue('u_incident_email', userInfo.email);


  g_form.setValue('u_phone',userInfo.phone);


  g_form.setValue('u_wing', userInfo.u_wing);


  g_form.setValue('location', userInfo.location);


  g_form.setValue('u_room', userInfo.u_room);


  g_form.setValue('u_floor', userInfo.u_floor);


}








more info : GlideForm (g form) - ServiceNow Wiki