You need to sign in to do that
Don't have an account?
Value passed to a variable in the custom function cant be accessd in any other function
VF page
<apex:page controller="passtest" >
<script>
function callit()
{
var well="Hello world";
call(well);
}
</script>
<apex:form id="frm">
<apex:commandButton value="clickit" onclick="callit();"/>
<apex:commandButton value="testit" action="{!testfunction}"/>
<apex:actionFunction name="call" action="{!recieveit}" reRender="frm">
<apex:param name="well" assignTo="{!ok}" value=""/>
</apex:actionFunction>
</apex:form>
</apex:page>
Controller
public class passtest
{
public string ok{get;set;}
public string ok1{get;set;}
public PageReference recieveit()
{
system.debug('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&'+ok);
ok1=ok;
return null;
}
public void testfunction()
{
system.debug('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'+ok1);
}
}
On the click of the first button the value of OK is set through the actionfuntion. However the value of ok is NULL when accessed in the second function by the click of the second button! even though the value is global. Can anyone explain why this happens??. Could you suggest a solution?
Took me a while to figure this one out, but I reckon you have a race condition going on here.
By specifying an onclick that calls an actionfunction, you effectively have two submissions of the form going on - one via the actionfunction and the other via the default behaviour of a command button, which is simply to refresh the page. Thus I think what is happening is that the view state is deserialised by the action function, you apply your updates to ok and ok1, then the viewstate is deserialised again by the command button postback, which overwrites the ok and ok1 values with their previous values- null.
If you are handing off form submission to an actionfunction, you'll need to stop the command button posting the form by returning false from the onclick handler, e.g.
All Answers
Took me a while to figure this one out, but I reckon you have a race condition going on here.
By specifying an onclick that calls an actionfunction, you effectively have two submissions of the form going on - one via the actionfunction and the other via the default behaviour of a command button, which is simply to refresh the page. Thus I think what is happening is that the view state is deserialised by the action function, you apply your updates to ok and ok1, then the viewstate is deserialised again by the command button postback, which overwrites the ok and ok1 values with their previous values- null.
If you are handing off form submission to an actionfunction, you'll need to stop the command button posting the form by returning false from the onclick handler, e.g.
great bob,
Very nice explanation, thanks!
Oh my god Bob!...I'm completely a fan of yours now!....Thank you so much (again)!....
I love the way you explain the answers.......Its so simple and clear.......
Great work! :D
@Rahul....It sure did....Thanks!