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
Ty WhitfieldTy Whitfield 

Issue pulling Visualforce variable into Apex

I'm clearly missing something in this process.  I have a VF page that looks like this
User-added image

What I would like to have happen is for the "Points Remaining" to total the number of "Found xx Total Exeuction Agent Points" minus the PakSize Input text amount.  

Problems:
  1. With my code below, I don't understand why on page load, it processes the getCount() Method.  
  2. I have been unsuccessful in passing the input field (pak) to Apex
VF Page
<table  >      
             <tr>
              <td colspan="10">              
                     <b>  <apex:outputText value="Found {!Text(newOpps.Quantity)} Total Execution Agent Points " id="pointsTotal" />  <br />
                      <apex:outputText value="Points Remaining: {!count}"   id="pointsRemaining" label=""/>
              </b>
              <hr />
              </td>
             </tr>
        <th><b>Quantity</b></th>
        <th></th>
        <th><b>PakSize (Points)</b></th>
             
        <tr>
        <td> 
             <apex:inputText id="quantity"  required="true"  />
            </td>
            <td rowspan="20" style="width:50px">
            </td>
        <td>
           
              <apex:outputpanel id="panel1">
           			 <apex:inputText id="pak"    value="{!pak}"   />                 
                    <apex:actionSupport event="onchange"  action="{!getCount}" status="counterStatus2" rerender="pointsRemaining"/>
                    </apex:outputpanel> 
            <apex:actionStatus id="counterStatus2" startText=" (Calculating...)" stopText=" (Completed)"/>

            </td>           
        </tr>
           
        <tr>
        <td colspan="3">
   <center>
          <apex:commandButton id="cmdaddLine" disabled="{!showButtons}" value="Add Another Multiple" />
   </center> 
         </td></tr>
        <tr>
         <td colspan="3">
            <hr />
            </td></tr>
        <tr>
         <td colspan="2">      
               <apex:commandButton id="cmdupdateOpp" disabled="{!showButtons}" value="Update Opportunity" />       
             </td>
             <td>
                 <apex:commandButton id="cmdCancel" onclick="window.history.go(-1); return false;"  value="Cancel" />
            </td></tr>
             <!-- </apex:repeat> -->
     </table>


Apex Code:
public  class SplitExecutionAgentController {
   @AuraEnabled
    
    public Boolean showButtons {get;set;}
    Integer pointsTotal = 0;
    Public Integer points {get;set;} 
    Integer count = 0;
    Public Integer pak {get;set;} // input text1 value  from vf
   
    
     public PageReference setParams()
    {
        return null;
    }
      
  public OpportunityLineItem getNewOpps() {     
      OpportunityLineItem A;
      A = [SELECT Id, Quantity, PricebookEntryID, TotalPrice, UnitPrice, ListPrice, Description  FROM OpportunityLineItem WHERE PricebookEntry.Name LIKE '%Execution Agent%' AND OpportunityId =  :System.currentPageReference().getParameters().get('Id')];      
         
      pointsTotal = Integer.valueOf(A.Quantity);
     points = Integer.valueOf(A.Quantity);
     count = Integer.valueOf(A.Quantity);
      pak = 0;
     
       return A;
  }    


                    
    public Integer getCount() {
        points = pointstotal - pak ;     
 
        return points;
    }
    
   
}


 
Best Answer chosen by Ty Whitfield
Pradeep SinghPradeep Singh
Hi, Please use system.debug to find what values are you getting in the method.
I have checked and its working fine :-

check this one :-


<apex:page controller="testRerender">
    <apex:form>
    <table>
        <tr>
            <td colspan="10">
                <b>  
                    <apex:outputText value="Points Remaining: {!points}" id="pointsRemaining" label=""/>
                </b>
                <hr/>
            </td>
        </tr>
    
        <tr>            
            <td>
                <apex:outputpanel id="panel1">
                    <apex:inputText id="pak" value="{!pak}" />
                    <apex:actionSupport event="onchange" action="{!calcPoints}" status="counterStatus2" rerender="pointsRemaining" />
                </apex:outputpanel>
                <apex:actionStatus id="counterStatus2" startText=" (Calculating...)" stopText=" (Completed)" />    
            </td>
        </tr>        
    </table>
    </apex:form>
</apex:page> 

-----------------------------------------
public class testRerender{
    
    public integer pak{get;set;}
    public integer points{get;set;}
    
    public testRerender(){
        
    }
    
    public void calcPoints(){
        points = pak +15 ;
    }
}

All Answers

Pradeep SinghPradeep Singh
Hi,
  • Getter and setter methods are called everytime there is a request from VFpage to controller.
  • Use Public Integer count{get;set;} instead of Integer count = 0;
  • Change the name fo the method from getCount to any other name.
Let me know if you need any help.
Ty WhitfieldTy Whitfield
Thanks so I made a few adjustment based upon your comments.  Just to note, I really didn't need the "count" integer.   I still can't get it to work.  You can see my changes below.  What I'm focused on is why the (new) calcPoints() isn't pushing back the totals to pointsRemaining field.

VF
<apex:pageBlock >
 
   <apex:pageMessages />  
    <table >        
        <tr>
        <td>
             <img src=" https://www.advsyscon.com/en-us/CorporateSite/media/Images/salesforce/splitexecutionagent2.png"  style="width: 75px; " />
            </td>
        <td>
            <h1 style="font-size:1.5em">
                Split Execution Agent Opportunity Product Line Item
            </h1>
            </td></tr>
    </table>
    <hr /> 
    <table  >      
             <tr>
              <td colspan="10">              
                     <b>  <apex:outputText value="Found {!Text(newOpps.Quantity)} Total Execution Agent Points " id="pointsTotal" />  <br />
                      <apex:outputText value="Points Remaining: {!points}"   id="pointsRemaining" label=""/>
              </b>
              <hr />
              </td>
             </tr>
        <th><b>Quantity</b></th>
        <th></th>
        <th><b>PakSize (Points)</b></th>
             
        <tr>
        <td> 
             <apex:inputText id="quantity"  required="true"  />
            </td>
            <td rowspan="20" style="width:50px">
            </td>
        <td>
           
              <apex:outputpanel id="panel1">
           			 <apex:inputText id="pak"    value="{!pak}"   />                 
                    <apex:actionSupport event="onchange"  action="{!calcPoints}" status="counterStatus2" rerender="pointsRemaining"/>
                    </apex:outputpanel> 
            <apex:actionStatus id="counterStatus2" startText=" (Calculating...)" stopText=" (Completed)"/>

            </td>           
        </tr>
           
        <tr>
        <td colspan="3">
   <center>
          <apex:commandButton id="cmdaddLine" disabled="{!showButtons}" value="Add Another Multiple" />
   </center> 
         </td></tr>
        <tr>
         <td colspan="3">
            <hr />
            </td></tr>
        <tr>
         <td colspan="2">      
               <apex:commandButton id="cmdupdateOpp" disabled="{!showButtons}" value="Update Opportunity" />       
             </td>
             <td>
                 <apex:commandButton id="cmdCancel" onclick="window.history.go(-1); return false;"  value="Cancel" />
            </td></tr>
             <!-- </apex:repeat> -->
     </table> 
   
    </apex:pageBlock>     
   
</apex:form>
 
     <!-- Java script starts Here -->
  <script>
setParams("pak");
  </script> 
</apex:page>

And the Apex Code
public  class SplitExecutionAgentController {
   @AuraEnabled
    
    public Boolean showButtons {get;set;}    
    Public Integer pointsTotal {get;set;} 
    Public Integer points {get;set;} 
    Public Integer pak {get;set;} 
   
    
     public PageReference setParams()
    {
        return null;
    }
      
  public OpportunityLineItem getNewOpps() {
    
      OpportunityLineItem A;
      A = [SELECT Id, Quantity, PricebookEntryID, TotalPrice, UnitPrice, ListPrice, Description  FROM OpportunityLineItem WHERE PricebookEntry.Name LIKE '%Execution Agent%' AND OpportunityId =  :System.currentPageReference().getParameters().get('Id')];
      
         
      pointsTotal = Integer.valueOf(A.Quantity);
     points = Integer.valueOf(A.Quantity);

     
       return A;
  }
    
      public Integer calcPoints() {    
        points = pointstotal - pak ; 
     
        return points;
    }



   
}

 
Pradeep SinghPradeep Singh
For public Integer calcPoints, you can use void as a returntype as you have value on points variable.Rest {get; set;} will do the job.
To get the values reflected on page Try to reRender the table in which you are using the points variable. 
Ty WhitfieldTy Whitfield
I've changed the calcPoints to 
public void calcPoints() {    
        points = pointstotal - pak ; 
     
        //return points;
    }

Regarding the reRender, isn't that what the actionSupport doing?  If not, is there a better way?
<apex:actionSupport event="onchange"  action="{!calcPoints}" status="counterStatus2" rerender="pointsRemaining"/>

  <apex:outputText value="Points Remaining: {!points}"   id="pointsRemaining" label=""/>

 
Ty WhitfieldTy Whitfield
As I side note, if I change the calcPoints() to below, my pointsRemaining field still doesn't update.  It's either not invoking or not updating the pointsRemaining field.
public void calcPoints() {   
      
     points = 30;
       
    }
Pradeep SinghPradeep Singh
Hi, Please use system.debug to find what values are you getting in the method.
I have checked and its working fine :-

check this one :-


<apex:page controller="testRerender">
    <apex:form>
    <table>
        <tr>
            <td colspan="10">
                <b>  
                    <apex:outputText value="Points Remaining: {!points}" id="pointsRemaining" label=""/>
                </b>
                <hr/>
            </td>
        </tr>
    
        <tr>            
            <td>
                <apex:outputpanel id="panel1">
                    <apex:inputText id="pak" value="{!pak}" />
                    <apex:actionSupport event="onchange" action="{!calcPoints}" status="counterStatus2" rerender="pointsRemaining" />
                </apex:outputpanel>
                <apex:actionStatus id="counterStatus2" startText=" (Calculating...)" stopText=" (Completed)" />    
            </td>
        </tr>        
    </table>
    </apex:form>
</apex:page> 

-----------------------------------------
public class testRerender{
    
    public integer pak{get;set;}
    public integer points{get;set;}
    
    public testRerender(){
        
    }
    
    public void calcPoints(){
        points = pak +15 ;
    }
}
This was selected as the best answer
Ty WhitfieldTy Whitfield
Thanks Pradeep - I finally got it going but forgot to mark this as answered