function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
CreatobugCreatobug 

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?

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

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.

 

<apex:commandButton value="clickit" onclick="callit(); return false;"/>

 

All Answers

bob_buzzardbob_buzzard

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.

 

<apex:commandButton value="clickit" onclick="callit(); return false;"/>

 

This was selected as the best answer
Rahul SharmaRahul Sharma

great bob,

Very nice explanation, thanks!

CreatobugCreatobug

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

 

 

CreatobugCreatobug

@Rahul....It sure did....Thanks!