Getting undefined value using GetReference method outside callback function

vivek72
Tera Guru

Hi,

I am using getReference method to check value set in a variable but getting undefined in alert:

Please let me know if I am missing anything, result in variable 'gr' should be test but I am getting undefined.

It's giving 'test' when I am trying to put alert inside callback function but the problem is only when this alert is set outside callback function.

I need to check this value to write my other code so it should return correct value outside callback function as well.

var gr;

var a1 = g_form.getReference('assignment_group', check);
function check(a1) {

if (a1.u_system == 'test') {

gr = a1.u_system;
}

}
alert(gr);

Thanks,

Vivek

1 ACCEPTED SOLUTION

Abhijit4
Mega Sage

Hi Vivek,

When you use getReference method with callback function then it acts as Aynchronous call which will execute your alert line before your callback function that is the reason it is showing value as empty.

If you want to check mandatory field based on some server side data then you would need to make synchronous call which is something like below,

var a1 = g_form.getReference('assignment_group');
if (a1.u_system == 'test') {
gr = a1.u_system;
}
alert(gr);

ServiceNow don't recommend synchronous call on client side. However, if you have such requirement then it should be fine.

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

View solution in original post

10 REPLIES 10

Instead of popup make those field mandatory within the client script of UI action.


Thanks and Regards,

Saurabh Gupta

No, Pop up is needed at the end to get user confirmation to procced as a part of req.

Anil Lande
Kilo Patron

Hi,

Please use below script:

var gr;
var a1 = g_form.getReference('assignment_group', check);
function check(a1) {
alert('u_system is '+a1.u_system);  
if (a1.u_system == 'test') {  // if this condtion is not true then a1 will be undefined
alert('condition is true');
gr = a1.u_system;
}
}
alert(gr);

 

Make sure u_system have 'test' value. I have added comments and additional alerts to show value of 'a1.u_system' and use same in comparison.

 

Also as suggested by Saurabh, prefer to use GlideAjax to improve performance.

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Hi Anil,

I am facing same issue even while calling script include from UI action code.

Alert inside Callback function showing correct value but alert outside function showing incorrect value.

Basically my req. is to check mandatory fields(not mandatory on dictionary level but mandatory for a state) for a particular state on incident table and then display popup to user to click ok to proceed.

In this I need to fetch value of a custom field from Group table and then on that basis I need to make a field as mandatory else optional.

If I am making this check inside callback function, then mandatory field check is being executed after pop up message, if I am making this check outside callback function, then it's showing undefined value meaning it's not able to access the value needed.

Thanks,

Vivek

Abhijit4
Mega Sage

Hi Vivek,

When you use getReference method with callback function then it acts as Aynchronous call which will execute your alert line before your callback function that is the reason it is showing value as empty.

If you want to check mandatory field based on some server side data then you would need to make synchronous call which is something like below,

var a1 = g_form.getReference('assignment_group');
if (a1.u_system == 'test') {
gr = a1.u_system;
}
alert(gr);

ServiceNow don't recommend synchronous call on client side. However, if you have such requirement then it should be fine.

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP