Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Set Reference field

rmaroti
Tera Contributor

Hi everyone,

 

I  have one requirement like on the user table we have 'department' reference field and same type of reference field  i have added its name 'u_department' in my custom table now I want to set from user source table value of department field to value on department field in my custom table.

 

I have written below code:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
 
   var act = g_form.getValue('u_active');
var emp = g_form.getReference('u_department');
if(act == 'true')
{
g_form.setValue('u_department',emp.name);
}
   
}

could you please help me in  where i have done mistake.

4 REPLIES 4

Sandeep Rajput
Tera Patron
Tera Patron

@rmaroti Do you have User field reference on your custom table?

yes, I  have 'u_employee' field its playing same role like caller  field played in incident table.

@rmaroti Create an onChange client script on the u_employee field on your custom table. As soon as the u_employee field changes, the value of department field defined on custom table will set to the department set on the employee record.

 

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

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

      return;

   }

 

   var act = g_form.getValue('u_active');

   if(act == 'true'||act==true){

      var employee = g_form.getReference('u_employee', setDepartment); /

   }

}

function setDepartment(employee){

   g_form.setValue('u_department',employee.department);

}

 

Hope this helps.

 

AnveshKumar M
Tera Sage
Tera Sage

Hi @rmaroti 

 

Try the following code. Don't forget to replace your user field name in custom table.

 

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

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

      return;

   }

 

   var act = g_form.getValue('u_active');

   if(act == 'true' || act == '1' || act == true){

      var emp = g_form.getReference('u_user', setDept); //Replace u_user with your field name

   }

}

 

function setDept(emp){

   g_form.setValue('u_department',emp.department);

}

 

Please mark my answer helpful and accept as a solution if it helped 👍

Thanks,
Anvesh