0

I try to get sum of values in a column to use it in controller. Vf Page:

<apex:page showHeader="true" sidebar="true" controller="PNLSplitController" doctype="html-5.0" action="{!onloadmethod}">
    <apex:form >
    <apex:variable value="{!0.00}" var="i"/>
        <apex:pageBlock title="PnL Splits" id="p">
            <apex:pageMessages />  
            <apex:commandbutton value="Save" action="{!Save}" immediate="true"/>         
            <apex:pageBlockTable value="{!AllPnL}" var="pnl" rows="5" rendered="{!AllPnL.size!=0}">                
                <apex:column headerValue="PnL Split">
                    <apex:inputField value="{!pnl.PnL_Split_Perc__c}">
                        <apex:actionSupport event="onchange" immediate="true" reRender="t"/>                        
                    </apex:inputField>   
                    <apex:variable var="i" value="{!i + pnl.PnL_Split_Perc__c}" id="t"/>                 
                    <!-- Footer of the first Cell -->                    
                    <apex:facet name="footer">
                        Total: <span class="t1"></span>
                    </apex:facet>
                </apex:column>                
                <apex:column headerValue="Sales Manager">
                    <apex:inputField value="{!pnl.Sales_Manager__c}"/>
                </apex:column>
                <apex:column value="{!pnl.Start_Date__c}"/>
            </apex:pageBlockTable>
            <apex:commandbutton value="Add PnL" action="{!addPnL}" immediate="true"/>
            <apex:outputText rendered="{!AllPnL.size==0}">
                <table width="100%">
                    <tr><td colspan="3" style="text-align:center;">No Records</td></tr>
                </table>
            </apex:outputText>            
        </apex:pageBlock>
    <script>
        document.getElementsByClassName('t1')[0].innerHTML = '{!i}';

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

Apex Controller:

public class PNLSplitController { 

    public String AccId = ApexPages.currentPage().getParameters().get('id');
    public Decimal Total {get;set;}
    public Decimal Amount {get; set;}

    public List<PnL_Split__c> AllPnL = new List<PnL_Split__c>();

    public void onloadmethod(){
        AllPnL = [SELECT Account__c, PnL_Split_Perc__c, Sales_Manager__c, Start_Date__c, Id, End_Date__c FROM PnL_Split__c WHERE End_Date__c = null AND Account__c =:AccId];
        for (PnL_Split__c p : AllPnL){
            Amount = p.PnL_Split_Perc__c;
        }  
    }    

    public List<PnL_Split__c> getAllPnL(){
        return AllPnL;
        }

    public void addPnL(){
        PnL_Split__c pnl = new PnL_Split__c();
        pnl.PnL_Split_Perc__c = 0.00;
        AllPnL.add(pnl);
        }    

    public pageReference Save(){
        for(PnL_Split__c p : AllPnL){
            system.debug('AllPnL='+AllPnL);
            if(AllPnL.size() > 1){
                Total += p.PnL_Split_Perc__c;
            }
            system.debug('Total='+Total);
        }
        if(Total > 100){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'The total % cannot be more than 100%')); 
        }
        return null;
    } 
}

So there two problems: 1) I can't get sum of values that user types dinamic 3) When user press "Save" It throws null pointer exception (why? We don't have any null record or else, even we edit the existing record)

Please help

0

As you are doing your calculation JavaScript so you need to change your approach a bit

<apex:inputField value="{!pnl.PnL_Split_Perc__c}" styleclass="splper">
     <apex:actionSupport event="onchange" immediate="true" reRender="t" oncomplete="calculatesum"/>                        
</apex:inputField> 

<script>
  function calculatesum() {
      var insum = document.getElementsByClassName('splper');
      var total = 0;
      for(var i =0; i < insum; i++) {
          total += parseFloat(insum[i].value);
      }
      document.getElementsByClassName('t1')[0].innerHTML = total ;

}
</script>

And you are getting null pointer because you didn't initialize Total. Initially it was null. So initialize it with 0.

Total = 0;
for(PnL_Split__c p : AllPnL){
        system.debug('AllPnL='+AllPnL);
        if(AllPnL.size() > 1){
            Total += p.PnL_Split_Perc__c;
        }
        system.debug('Total='+Total);
}
  • Hi @Tushar! The class parameter is not supported in inputField, so I was change it to: <apex:inputField value="{!pnl.PnL_Split_Perc__c}" id="splper"> And Also changed the function to: document.getElementsById('t1')[0].innerHTML = total ; but it doesn't work( – Viktor Dec 19 '17 at 9:37
  • @Viktor you can use styleclass check my updated answer. – Tushar Sharma Dec 19 '17 at 10:00

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.