1

I have a do while loop that generates a couple of numbers: enter image description here

The problem that I'm having is that it only displays the last value after the button has been pressed and the method has been called. enter image description here

I need to find a way to display all of the large numbers generated.

The apex method looks like this:

public class GenerateNumber {
 public String numbersgenerated {get;set;}
 public Integer userinput {get;set;}
 public Decimal largenum {get;set;}
 list<Number_Setting__c> cslist = [SELECT Largest_Number__c FROM Number_Setting__c];

public void  genumber(){
do{

    if(cslist.size() >0)
    largeNum = cslist[0].Largest_Number__c;
    largeNum ++;

     for (Number_Setting__c updt : cslist){
        updt.Largest_Number__c = largeNum; 
        update updt; 
     }

      userinput --;

    } while(userinput >  0);
  }
}

and the form on the looks like this:

 <apex:form >    
 <apex:inputtext value="{!userinput}" />
 <apex:commandButton value="Generatethenum" action="{!genumber}" status="status" styleClass="btn btn-default" />
  <apex:outputText value="{!largeNum}" />
 </apex:form>  
  • 1
    Greg, please take the time to visit the Help center and read How do I ask a good question. If you post what you've written so far and where you're stuck, I'm confident you'll find plenty of people willing to assist you. Most likely your problem is that you're not saving your intermediate values to a list instead of single variable, but without seeing your existing code, it's impossible for me to say. – crmprogdev Mar 2 '16 at 15:02
  • Right, sorry as you suggested I've updated my question with the code for the apex class as well as the visualforce page to make it more clearer. – Greg Mar 2 '16 at 15:07
  • 2
    My guess is that you have one variable which is getting overwritten in every loop iteration If you want to show all values then store them in array. Show all those values using <apex:repeat > – AtulRajguru9 Mar 2 '16 at 15:13
  • @Ratan Is action poller a standard Apex function if so is there any documentation on it ? – Greg Mar 2 '16 at 15:21
  • @Ratan I've implemented the action poller on the website but there is nothing to call the method now as it isn't a button. Is there a way to assign a button to the action poller ? – Greg Mar 2 '16 at 15:44
1

Create a map to store both user input and large number map<Integer, Decimal>

and display in VF page using apex:repeat

on each click on button you need to initialize this variable so it will contain updated user input related data

/* Controller: */

public class exampleCon {

    public String numbersgenerated {get;set;}
    public Integer userinput {get;set;}
    public Decimal largenum {get;set;}
    public map<Integer, Decimal> mapUserInputToLargeNo {get; private set;}
    list<Number_Setting__c> cslist = [SELECT Largest_Number__c FROM Number_Setting__c];

    public void  genumber(){
        mapUserInputToLargeNo = new map<Integer, Decimal>();
    do{

        if(cslist.size() >0)
        largeNum = cslist[0].Largest_Number__c;
        largeNum ++;

         for (Number_Setting__c updt : cslist){
            updt.Largest_Number__c = largeNum; 
            update updt; 
         }
         mapUserInputToLargeNo.put(userinput, largeNum);
         userinput --;

        } while(userinput >  0);
      }
}

/* VF page */

<apex:page controller="exampleCon">
    <apex:form id="frm">
        <apex:inputtext value="{!userinput}" />
         <apex:commandButton value="Generatethenum" action="{!genumber}" status="status" styleClass="btn btn-default" rerender="frm"/>
         <br/>
         <apex:repeat value="{!mapUserInputToLargeNo}" var="map">
             User Input: {!map} 
             Large Num: {!mapUserInputToLargeNo[map]}<br/>
         </apex:repeat>
    </apex:form>
</apex:page>
|improve this answer|||||
  • 1
    that is amazing, thank you so much for your effort – Greg Mar 3 '16 at 8:17
  • @Greg Welcome.. – Ratan Paul Mar 3 '16 at 8:32
1

By definition, your code is only displaying the largest number when you specify the following:

largeNum = cslist[0].Largest_Number__c;

If you want all the other values, you'll need to get all the values of cslist.Largest_Number__c, not just the one that's at cslist[0].

To do that, you'd also need to iterate or do a repeat on <apex:outputText value="{!largeNum}" /> replacing it with appropriate values of cslist during the <reapeat>. The problem you appear to have though is that you're doing an update, so you'll need to store them in a list, then output the list to the page with a repeat on that instead of largeNum.

|improve this answer|||||
  • The Largest_Number__c is a field in a custom setting that gets updated each time this method is called without storing the other values. – Greg Mar 2 '16 at 15:18
  • 1
    You'll need to store those values in a list that is retained in your viewstate. – crmprogdev Mar 2 '16 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.