how to update variable inside callback function
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2022 06:28 AM
HI, I have defined variable outside and i want to update the value inside callback . How can i do it.
I have tried to explain my scenario using an example.
var name='abc';
if(user!=' '){
var x = g_form.getReference('user', callFunction);
function callFunction(x){
var managerName=x.manager;
name=name+"_" + managerName;
}
}
alert(name); // I'm not getting the updated name it still shows abc
//The goal I want to achieve is the variable to update the value and use it outside callFunction
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2022 04:03 AM
But my requirement is such that callback is running only if specific option is chosen else it is not
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-03-2022 12:11 AM
Hi
There is no issue with your code, it is perfect and the reason why you are not getting the value is the async value of the callback function.
Try the below-provided code and see if it works for you:
var name = 'abc';
if (user != ' ') {
var x = g_form.getReference('user', callFunction);
function callFunction(x) {
var managerName = x.manager;
name = name + "_" + managerName;
}
}
//Delay for 3 sec so that callback has set the value
setTimeout(alertFunc, 3000);
function alertFunc() {
alert(name);
}
Remark: Though setTimeout creates a performance overhead so adopt the delay milliseconds (3000) accordingly.
Please mark my answer as correct if this solves your issues!
If it helped you in any way then please mark helpful!
Thanks and regards,
Kartik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-03-2022 12:13 AM
I'll write a script inlude and get it .
Is this more efficient ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-03-2022 12:16 AM
Even with Script Include, you need to make an Async call to adhere to the performance and workability on Service Portal. So even with the Client-callable Script Include you will face the same issue.
The best option is to use setTimeout, but you need to do tests to adjust the minimum delay time in which you are getting the values.
Please mark my answer as correct if this solves your issues!
If it helped you in any way then please mark helpful!
Thanks and regards,
Kartik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-03-2022 12:25 AM
This was sample I'm not just alerting there is much more scripts below this which updates te field