Why is getReference returning undefined value?

arnabbose
Tera Expert

Hi,

I am trying to alert a reference field (team_admin).

function onSubmit() {

var teamad1 = g_form.getReference('team_admin', setAdmin);

alert(teamad1.name);

Why do I get an undefined value as an alert out of this?

6 REPLIES 6

Arnab,



Using callBack function will not work in your case, since it is asynchronous. This script should work for you. I have tested this on my instance and it is working as expected


function onSubmit() {


  //Type appropriate comment here, and begin script below



  var loc1 = g_form.getReference('location');


  alert(loc1.name);


  var loc2 = g_form.getReference('location1');


  alert(loc2.name);


  var loc3 = g_form.getReference('location_2');


  alert(loc3.name);



  var depart1 = g_form.getReference('department');


  alert(depart1.name);


  var depart2 = g_form.getReference('department_1');


  alert(depart2.name);


  var depart3 = g_form.getReference('department_2');


  alert(depart3.name);




  if(loc1.name == loc2.name && loc1.name == loc3.name && loc2.name == loc3.name && depart1.name == depart2.name && depart1.name == depart3.name && depart2.name == depart3.name)


  {


  alert("Inside If ");


  //do nothing;


  }


  else {


  alert("The team admins you are trying to add do not belong to same department and location." );


  g_form.clearValue('team_admin_2');


  g_form.clearValue('team_admin_3');


  g_form.clearValue('department_1');


  g_form.clearValue('department_2');


  g_form.clearValue('location1');


  g_form.clearValue('location_2');


  return false;


  }


}


Aka Guglielmo
ServiceNow Employee
ServiceNow Employee

Hi arnabbose,


I know it's an old request, but it could be helpful for others with the same problem.



The right way is:



function onSubmit() {


var teamad1 = g_form.getReference('team_admin', setAdmin);


}



function setAdmin(teamad1) {


alert(teamad1.name);


}



HTH,