The CreatorCon Call for Content is officially open! Get started here.

how to update variable inside callback function

Akki1
Tera Contributor

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

 

22 REPLIES 22

But my requirement is such that callback is running only if specific option is chosen else it is not 

Kartik Sethi
Tera Guru

Hi @Akki 

 

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

I'll write a script inlude and get it .

Is this more efficient ?

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

This was sample I'm not just alerting there is much more scripts below this which updates te field