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
swethasfdcswethasfdc 

Not returning an integer from apex page to visualforce page

When I tried to return an integer value from apex page to a visualforce page,It is not allowing integer values in my action button method.It was taking only string and pagereference values.Why is it so???

When i am using string.valueof(integer) and converting into string only it was able to go through the apex to VF.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

The return value of an action function is used to redirect the browser to a new page. This means that it makes no sense to return a date, integer, boolean, or any other value that is not a String or PageReference. If you want a value from a function to be put into your page, use an accessor function instead. They take the following forms:

 

public Integer myNumber1 { get; set; }
public Integer getmyNumber2() {
   return aNumber;
}

The former method allocates space for myNumber1 inside the view state, and can be modified by any function, even your action function. The latter method returns a hypothetical value (aNumber), which could be any variable in scope that is an Integer, or even the results of a calculation or query.

 

To access a value obtained by these means, you could use a number of different methods:

 

<apex:page ...>
...

<script>
function doSomethingWithNumber(source) {
   var myNum = parseInt(source)
   ...
}
</script>
<apex:form id="form">
   
   <apex:inputText id="number1" value="{!myNumber1}"/>
   <apex:inputHidden id="number2" value="{!myNumber2}"/>
   <apex:commandButton action="{!doAction1}" reRender="form" oncomplete="doSomethingWithNumber(document.getElementById('{!$Component.form.number1}').value)" value="Action 1"/>
</apex:form>
</apex:page>

There's other ways to accomplish what you're trying to do.

All Answers

sfdcfoxsfdcfox

The return value of an action function is used to redirect the browser to a new page. This means that it makes no sense to return a date, integer, boolean, or any other value that is not a String or PageReference. If you want a value from a function to be put into your page, use an accessor function instead. They take the following forms:

 

public Integer myNumber1 { get; set; }
public Integer getmyNumber2() {
   return aNumber;
}

The former method allocates space for myNumber1 inside the view state, and can be modified by any function, even your action function. The latter method returns a hypothetical value (aNumber), which could be any variable in scope that is an Integer, or even the results of a calculation or query.

 

To access a value obtained by these means, you could use a number of different methods:

 

<apex:page ...>
...

<script>
function doSomethingWithNumber(source) {
   var myNum = parseInt(source)
   ...
}
</script>
<apex:form id="form">
   
   <apex:inputText id="number1" value="{!myNumber1}"/>
   <apex:inputHidden id="number2" value="{!myNumber2}"/>
   <apex:commandButton action="{!doAction1}" reRender="form" oncomplete="doSomethingWithNumber(document.getElementById('{!$Component.form.number1}').value)" value="Action 1"/>
</apex:form>
</apex:page>

There's other ways to accomplish what you're trying to do.

This was selected as the best answer
swethasfdcswethasfdc

Thanks for ur reply, I got ur answer.

Can i know when and why we use the rerender and oncomplete.

Andy BoettcherAndy Boettcher

rerender = if you want to "refresh" a particular section of the VF page via AJAX on completion of your action method.

oncomplete = if you want to fire off some Javascript code on completion of the action method.

 

-Andy