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
LithiumsLithiums 

Passing Value from controller to Page

After the method call, I am setting the flag value to true, but on the page it is always false. Can someone please look into this and let me know if i am m issing something.

 

<apex:page standardController="Opportunity" extensions="testcontroller">
<script type="text/javascript">
function testclick(Value){
testcheck(Value);
alert({!flag});
}
</script>
<apex:form >
<apex:actionFunction action="{!phaseCheck}" name="testcheck" reRender="">
<apex:param assignTo="{!pValue}" value="" name="someName"/>
</apex:actionFunction>
<apex:pageBlock >

<apex:inputCheckbox title="Test" id="chkbox" onclick="testclick('Test')"/>
<apex:outputLabel id="olabel" for="chkbox">Test</apex:outputLabel>
</apex:pageBlock>

</apex:form>
</apex:page>

 

public class testcontroller{

public testcontroller(ApexPages.StandardController controller) {

}

public Static String pValue{get;set;}
public Static boolean flag{get;set;}

public static void phaseCheck(){
system.debug('Test --->' + pValue);
flag = true;
system.debug('flag --->' + flag);

}
}

Best Answer chosen by Admin (Salesforce Developers) 
Laxman RaoLaxman Rao

transient does not work.

 

Try this :

Hopefully it should work.

Copy and paste complete code :

<apex:page standardController="Opportunity" extensions="testcontroller">
<apex:form id="formId">
<script>
function testclick(Value)
{
testcheck(Value);
}

function handleRequest()
{
alert({!flag});
}
</script>
<apex:actionFunction action="{!phaseCheck}" name="testcheck" reRender="formId" oncomplete="handleRequest()">
<apex:param assignTo="{!pValue}" value="" name="someName"/>
</apex:actionFunction>
<apex:pageBlock >
<apex:inputCheckbox title="Test" id="chkbox" onclick="testclick('Test')"/>
<apex:outputLabel id="olabel" for="chkbox">Test</apex:outputLabel>
</apex:pageBlock>
</apex:form>
</apex:page>

 

All Answers

Laxman RaoLaxman Rao

That was the problem with reRender.

 

 

Try this :

<apex:page standardController="Opportunity" extensions="testcontroller">
<apex:form id="formId">
<script>
function testclick(Value)
{
alert({!flag});
testcheck(Value);
}
</script>

<apex:actionFunction action="{!phaseCheck}" name="testcheck" reRender="formId">
<apex:param assignTo="{!pValue}" value="" name="someName"/>
</apex:actionFunction>
<apex:pageBlock >
<apex:inputCheckbox title="Test" id="chkbox" onclick="testclick('Test')"/>
<apex:outputLabel id="olabel" for="chkbox">Test</apex:outputLabel>
</apex:pageBlock>

</apex:form>
</apex:page>

LithiumsLithiums

Thanks for the reply, I tried your solution but unfortunately it doesnt work. I can see the value being setting in the controller but somehow it is not reflecting on the page.

Laxman RaoLaxman Rao

You copy this complete code and paste in your visual force

 

<apex:page standardController="Opportunity" extensions="testcontroller">
<apex:form id="formId">
<script>
function testclick(Value)
{
alert({!flag});
testcheck(Value);
}
</script>

<apex:actionFunction action="{!phaseCheck}" name="testcheck" reRender="formId">
<apex:param assignTo="{!pValue}" value="" name="someName"/>
</apex:actionFunction>
<apex:pageBlock >
<apex:inputCheckbox title="Test" id="chkbox" onclick="testclick('Test')"/>
<apex:outputLabel id="olabel" for="chkbox">Test</apex:outputLabel>
</apex:pageBlock>
</apex:form>
</apex:page>

 

One more thing, First Check it and the Un-Check it.

You will find the difference.

LithiumsLithiums

It is returning a true, when I check it for the second time. I am trying display an error message when it is checked for the first time. If I do it this way it will not be captured the first time but it will done on second click, which defeats the purpose. Is there any other way to do this?

LithiumsLithiums

I will give it a try with the transient keyword and see if it works.

Laxman RaoLaxman Rao

transient does not work.

 

Try this :

Hopefully it should work.

Copy and paste complete code :

<apex:page standardController="Opportunity" extensions="testcontroller">
<apex:form id="formId">
<script>
function testclick(Value)
{
testcheck(Value);
}

function handleRequest()
{
alert({!flag});
}
</script>
<apex:actionFunction action="{!phaseCheck}" name="testcheck" reRender="formId" oncomplete="handleRequest()">
<apex:param assignTo="{!pValue}" value="" name="someName"/>
</apex:actionFunction>
<apex:pageBlock >
<apex:inputCheckbox title="Test" id="chkbox" onclick="testclick('Test')"/>
<apex:outputLabel id="olabel" for="chkbox">Test</apex:outputLabel>
</apex:pageBlock>
</apex:form>
</apex:page>

 

This was selected as the best answer
LithiumsLithiums

Awesome, worked like a charm. Thanks for all the effort