+ Start a Discussion
StaciStaci 

running total in apex

I have some code iterating through a list according to who is logged in and it displays on the screen.  In this list, there is a subtotal and a total.  I'm wanting to get a total of all the subtotal items and a total of the total items.

How would I accomplish that?

Here's what I have, but the calculation is not working
aTotal.Annualized_Running_Total__c = aTotal.Annualized_Running_Total__c + a.X2016_Cost__c;  
public with sharing class LicenseList { 
private final License_Numbers__c ln; 
public user currentuser{get;set;} 
public id tobeEdited{get;set;}


public LicenseList(ApexPages.StandardSetController controller) { 
currentuser=new User(); currentuser=[Select Id, Business_Unit_new__c from User where Id=:userinfo.getuserId()]; 
this.ln = (License_Numbers__c)controller.getRecord(); 

} 

//------------------------------------------------------------------------------------

public List<License_Numbers__c> lntypes16 = new List<License_Numbers__c>();

public List <License_Numbers__c> getLicenseList(){ 
lntypes16 = [select id, Name, X2016_Cost__c, Business_Unit__c, Annualized_Running_Total__c, X2016_Starting_Amount__c, X2016_Subtotal__c, BMS_Code__c, License_Type__c, Monthly_Unit_Price__c, Running_License_Total__c, Org__c FROM License_Numbers__c where Business_Unit__c =:currentuser.Business_Unit_new__c AND License_Year__c = '2016' ORDER BY License_Type__c];
 
        License_Numbers__c aTotal;
        for(License_Numbers__c a:lntypes16){
                //aTotal.Annualized_Running_Total__c = aTotal.Annualized_Running_Total__c + a.X2016_Cost__c;   
                break;
            }
        

return lntypes16; 
} 


//-----------------------------------------------------------------------------
public List<License_Numbers__c> selectedTypes = new List<License_Numbers__c>();

public List <License_Numbers__c> getLicenseList17(){ 

selectedTypes=[select id, Name, Annualized_Running_Total__c, X2017_Cost__c, Business_Unit__c,X2017_Total_Needed__c, X2016_Starting_Amount__c, X2017_Subtotal__c, BMS_Code__c, License_Type__c, Monthly_Unit_Price__c, Org__c FROM License_Numbers__c where Business_Unit__c =:currentuser.Business_Unit_new__c AND License_Year__c = '2017' ORDER BY License_Type__c]; 

return selectedTypes; 
} 
    public void saveRecord(){ 
    License_Numbers__c tobeupdated;
    for(License_Numbers__c temp:selectedTypes){
        if(temp.id==tobeEdited){
            tobeupdated = temp;
            break;
         }
     }
        update selectedTypes;
        tobeEdited = null;
    } 
}