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
cinfidelcinfidel 

Display a string returned from APEX class on a VF page

I'm trying to display a string stored as a public variable in the controller apex class onto the visualforce page.

 

Is this even possible? Here's my test code so far (it doesn't display anything...) Thanks in advance!

 

VF:

<apex:page controller="VFController">

 

  <apex:form >

      <apex:commandButton value="numCases" onclick="execute_numCases()"/>

      <apex:actionFunction action="{!numCases}" name="execute_numCases" rerender="showstate" >

   </apex:actionFunction> 

  </apex:form>

  

  <apex:outputtext style="border:1px;background-color:#F3F3EC;" value="{!log}"/>

  

  

</apex:page>

 

Controller:

 

    public String log {get; set;}

   

 

    //a test method that returns a string saying how many cases are in the system

    public string numCases(){

     Integer count= 0;

     log ='something is wrong';

     Boolean done = false;

     Case mark = [select id, CaseNumber from case order by CaseNumber limit 1];

     if (mark != null) {count++;}

     else {log = 'There are currently 0 cases in our database.'; return log;}

     while (!done){

     Case[] cases = [select id, CaseNumber from case where CaseNumber > :mark.CaseNumber order by CaseNumber limit 499 ];

     count += cases.size();

     if (cases.size() == 0) {done = true; break;}

     mark = cases[cases.size()-1];

     }

     log = 'There are currently ' +count + ' cases in our database.';

     return log;

    } 

AvromAvrom

What does rerender="showstate" refer to? I don't see any components with an ID of showstate in your sample code.

 

Does this work if you give your outputText an id="showstate"?

 

cinfidelcinfidel

Oh it must be a residue from a code snippet I copied and pasted. I did what you said, but it is still not displayed.

<apex:form >      <apex:commandButton value="numCases" onclick="execute_numCases()"/>      <apex:actionFunction action="{!numCases}" name="execute_numCases" rerender="showstate" >      </apex:actionFunction>   </apex:form>   <apex:outputtext id="showstate" value="{!log}"> </apex:outputtext>

 

cinfidelcinfidel
Sorry about the weird format... I don't know why it did that... here's the same code I pasted above. 
 
 
  <apex:form >
      <apex:commandButton value="numCases" onclick="execute_numCases()"/>
      <apex:actionFunction action="{!numCases}" name="execute_numCases" rerender="showstate" >
      </apex:actionFunction> 
  </apex:form>
 
  <apex:outputtext id="showstate" style="border:2px;border-style:solid;background-color:#F3F3EC;height:150px;" value="{!log}">
  </apex:outputtext>
AvromAvrom

Any reason why your action returns a String? A standard action returns a PageReference (and if you want to stay on the current page, you should return null).

 

cinfidelcinfidel

I originally wanted the string returned by that method to be displayed in apex:outputlabel. I realized that can't be done. So I made a public variable called log and made this method update this string. This method really should be void as it doesn't really do anything other than calculating the number of cases and store that number to log.

 

AvromAvrom

Try changing the method signature so that it returns a PageReference, and change the return statement so that it returns null.

 

SteveBowerSteveBower

The onclick attribute is referring to a javascript function that doesn't exist, and isn't needed.   Any reason not to use the count() aggregate in the select statement?  If your goal is just to count the number of cases, that seems the easiest way to do it.  Unless you want to force the user to hit the button before you do the query, you don't even need to have a button, you could just have the getter for num do the query.

 

Just typing it in, but perhaps you want something like this:

<apex:page controller="VFController"> <apex:form > <apex:commandButton value="numCases" action="{!numcases}"/> </apex:form> <apex:outputtext style="border:1px;background-color:#F3F3EC;" value="There are currently {!num} cases in our database."/> </apex:page> public Integer num {get; set;} public pageReference getnumcases() { if (num == null) num = [select count() from case]; return null; }

 

Or, without the button,

 

 

<apex:page controller="VFController"> <apex:outputtext style="border:1px;background-color:#F3F3EC;" value="There are currently {!num} cases in our database."/> </apex:page> public with sharing class VFController() { private Integer num; public Integer getnum() { if (num == null) num = [select count() from case]; return num; }

 

Or perhaps I'm missing what you're trying to do.  Best, Steve.

 

 

cinfidelcinfidel

Thanks a lot Steve and Avrom. This is my first time doing anything with VF, and this method is just a test to see if I can it to work. I'll modify my method so it returns pagereference and see if it works.

 

 

ivantsyivantsy

Hi!

I also new to VF and also trying different ideas with VF.

Next code is working and looks like it is doing what you are asking:

 

 

Page:

 

<apex:page controller="ActionPageController">
    <apex:form >
   
        <apex:actionFunction name="hitMe" action="{!iWantMyJSValues}" rerender="jsvalues">
            <apex:param name="FirstJSParameter" value="" />
            <apex:param name="SecondJSParameter" value="" />
        </apex:actionFunction>
      
        <apex:outputPanel id="jsvalues">
            <apex:outputText value="Value of FirstJSParameter: {!valueOne}" /><br/>
            <apex:outputText value="Value of SecondJSParameter: {!valueTwo}" /><br />          
        </apex:outputPanel>
      
        <span style="cursor: pointer;"
            onclick="hitMe(Date(), 'best shot')">Hit Me</span>
    </apex:form>
</apex:page>

 

 

Controller:

public with sharing class ActionPageController {

    public String valueOne { get; set; }
    public String valueTwo { get; set; }
  
    public PageReference iWantMyJSValues() {
        valueOne = Apexpages.currentPage().getParameters().get('FirstJSParameter');
        valueTwo = Apexpages.currentPage().getParameters().get('SecondJSParameter');
        return null;
    }
}