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
SeanCenoSeanCeno 

Sum Total Always Rendered In Footer

This should be a simple fix, but I'm not sure how to do it. My footer only displays the calculated total if I make a change to the picklist in the column. Once I hit save or reopen the record, the total is gone until I make a change again (using onchange of course). Is there a way to always have the total rendered? Like have it say 0% before any selections, then show the total even after a save?

I've tried using rendered and adding the calculation the save button, still not displaying the total. Here's what it looks like after a save and I revisit the record, the Total value is blank.

User-added image

Here's my code if that helps.
<apex:actionFunction name="calculateAllocation" action="{!calculateAllocation}" reRender="Split, totalsPanel"/>
        <apex:actionFunction name="calculateSplit" action="{!calculateSplit}" reRender="totalsSplit" />         


   <apex:pageBlockSection id="Allocation" title="Allocate Expenses" Columns="1"  collapsible="false" rendered="{!(expenseLineItem.AllocationCheckbox__c == true)}">
                <apex:outputLabel value="Please select the Managed Company and the percentage amount you wish to allocate to it. Make sure it sums to 100%. Press Save in order to see the Allocation Split value." />
                <apex:variable var="rowNumber" value="{!0}"/>
                <apex:pageBlockTable id="allocationTable" var="allocation" columns="4" value="{!expenseLineItemAllocationList}">
                    <apex:column headerValue="Managed Company">
                        <apex:inputField value="{!allocation.Product__c}" required="{!IF(expenseLineItem.AllocationCheckbox__c == true, True, False)}"/>
                        <apex:facet name="footer">
                            <apex:outputPanel layout="block" style="text-align: right;" >
                                <apex:outputText value="Total" />
                            </apex:outputPanel>
						</apex:facet>
                    </apex:column>
                    <apex:column headerValue="Allocation %">
                        <apex:inputField value="{!allocation.Allocation__c}" onchange="calculateAllocation(); calculateSplit();" required="{!IF(expenseLineItem.AllocationCheckbox__c == true, True, False)}"/>
                        <apex:facet name="footer">
                            <apex:outputPanel layout="block" id="totalsPanel">
                                <apex:outputText value="{!totalAllocation}" rendered="{!IF(expenseLineItem.AllocationCheckbox__c == true, True, False)}" />
                            </apex:outputPanel>
						</apex:facet>
                    </apex:column>
                    <apex:column headerValue="Allocation Split">
                        <apex:outputField value="{!allocation.Allocation_Split__c}" />
                        <apex:facet name="footer2">
                            <apex:outputPanel layout="block" id="totalsSplit"  >
                                <apex:outputText value="{!totalSplit}" />
                            </apex:outputPanel>
                        </apex:facet>
                    </apex:column>
                    <apex:column headerValue="Action">
                        <apex:commandButton value="Delete" action="{!deleteRow}" rerender="Allocation, footer, totalsPanel" oncomplete="calculateAllocation(); calculateSplit();" >
                            <apex:param name="Allocation" value="{!FLOOR(rowNumber)}" assignTo="{!rowIndex}"/>
                            <apex:param name="deleteId" value="{!allocation.Id}" assignTo="{!deleteId}"/>
                        </apex:commandButton>
                        <apex:variable var="rowNumber" value="{!rowNumber+1}" />
                    </apex:column>
                </apex:pageBlockTable>
                <apex:commandButton value="Add Row" action="{!addRow}" rerender="allocationTable"/>
            </apex:pageBlockSection>
//New Object For Custom Allocation
    public Expense_Line_Item_Allocation__c expenseLineItemAllocation {get; set;} //insert field from allocation
    public List<Expense_Line_Item_Allocation__c> expenseLineItemAllocationList {get {
        if (ExpenseLineItemAllocationList != null)
            return ExpenseLineItemAllocationList;
        if (controller.getId() != null) try {
            ExpenseLineItemAllocationList = [
                select Allocation__c
                , Product__c
                , Expense_Split_Amount__c
                , Expense_Line_Item_Detail__c
                , Share_Of_Total_Allocation__c
                , Allocation_Split__c
                
                from Expense_Line_Item_Allocation__c
                where Expense_Line_Item_Detail__c = :expenseLineItem.Id
                order by Product__c desc
            ];
            
        } catch (System.DmlException dmlException) {
            ApexPages.addMessages(dmlException);
        }
        if (ExpenseLineItemAllocationList == null || ExpenseLineItemAllocationList.size() == 0) {
            ExpenseLineItemAllocationList = new List<Expense_Line_Item_Allocation__c>();
            if(expenseLineItem != null && !String.isBlank(expenseLineItem.Managed_Company__c)) {
                string[] parsedCompanies = expenseLineItem.Managed_Company__c.split(';');
                for(string s: parsedCompanies) {
                    ExpenseLineItemAllocationList.add(new Expense_Line_Item_Allocation__c(product__c = s));
                }
            }
        }
        return ExpenseLineItemAllocationList;
    } set;} //insert list of fields in allocation
    
    public void calculateAllocation() {
        Decimal dTotAllocation = 0;
        for(Expense_Line_Item_Allocation__c elia : expenseLineItemAllocationList) {
            if(!String.isBlank(elia.Allocation__c)) {
                dTotAllocation += Decimal.valueOf(elia.Allocation__c.replace('%',''));
            }
        }
        totalAllocation = dTotAllocation + '%';
    }
    public void calculateSplit() {
        Integer SplitTotal = 0;
        for(Expense_Line_Item_Allocation__c elia : expenseLineItemAllocationList) {
            if((elia.Allocation_Split__c) != null) {
                SplitTotal += Integer.valueOf(elia.Allocation_Split__c);
            }
        }
        totalSplit = '$' + SplitTotal;
    }
    
    public void saveCompany(){
        for(Expense_Line_Item_Allocation__c ela: expenseLineItemAllocationList){ 
            ela.Expense_Line_Item_Detail__c = expenseLineItem.Id;
            if(expenseLineItem.AllocationCheckbox__c == True) {
                if(!String.isBlank(totalAllocation)) {
                    if(Decimal.valueOf(totalAllocation.replace('%','')) != 100) {
                        addMessage(ApexPages.Severity.ERROR,'Please ensure the percentages add to 100%.');
                    }
                }
            }
            upsert ela;
        }
    }
    
    public void addRow(){
        expenseLineItemAllocationList.add(new Expense_Line_Item_Allocation__c());
        calculateAllocation();
        calculateSplit();
    }
    
    public integer rowIndex {get; set;}
    public Expense_Line_Item_Allocation__c del;
    public String deleteId {get; set;}
    public String totalAllocation {get; set;}
    public String totalSplit {get; set;}
    //public List<Id> deleteItems {get;set;}
    public void deleteRow(){
        calculateAllocation();
        calculateSplit();
        del = new Expense_Line_Item_Allocation__c();
        del = expenseLineItemAllocationList.remove(rowIndex);
        if(deleteId != null){
            try{
                Expense_Line_Item_Allocation__c elia = new Expense_Line_Item_Allocation__c(id=deleteId);
                delete elia;
            }catch(exception e){
                system.debug(string.valueof(e));//unable to be deleted
            }
        }
    }



 
Suneel#8Suneel#8
Can you call calculateAllocation function upon page load with action attribute in apex:page markup