- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2022 04:31 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2022 11:51 PM
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
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2022 11:59 PM
Instead of popup make those field mandatory within the client script of UI action.
Thanks and Regards,
Saurabh Gupta
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 12:11 AM
No, Pop up is needed at the end to get user confirmation to procced as a part of req.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2022 07:09 PM
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
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2022 11:32 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2022 11:51 PM
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
Regards,
Abhijit
ServiceNow MVP