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
Hugo StoupeHugo Stoupe 

Trying to loop through a list in a controller to total a column, but I get Arithmetic Expression must use numeric arguments error

Hopeing someone can solve this for me.

I am trying to create variables to hold totals - in this case "Friday__c" hold either nothing or a decimal data type.  The query builds a list which I use in my visualforce page.  I would like to iterate through the list and add up all "Friday__c" fields into the variable totalFridayHours then display this field on my visualforce page.

I get the "Arithmetic Expression must use numeric arguments error in my "For" statement near the bottom of my code:
 
public class fetchTimeCardEntryDataController {

    public String datename{get;set;}
    public List<Time_Card_Entry__c> TEList{get;set;}
    public Decimal totalFridayHours {get;set;}
    public String query{get;set;}
    public dummyTable__c dum{get;set;} 


    public fetchTimeCardEntryDataController() {
        dum=new dummyTable__c();
    }

    public PageReference fetch(){
    
        Date sDate = dum.dateOne__c;
        Date eDate = dum.dateTwo__c;
    
        String SobjectApiName = 'Time_Card_Entry__c';
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
 
        String commaSepratedFields = '';
        for(String fieldName : fieldMap.keyset()){
            if(commaSepratedFields == null || commaSepratedFields == ''){
                commaSepratedFields = fieldName;
            }else{
                commaSepratedFields = commaSepratedFields + ', ' + fieldName;
            }
        }
      query = 'select ' + commaSepratedFields + ' from ' + SobjectApiName + ' Where Monday_Date__c >=:sDate AND Monday_Date__c <=:eDate  Limit 500';

     System.debug('query:'+query);

        TEList = Database.query(query);
         // Process the totals

//************ERROR IN THIS BLOCK *******************

         for(Time_Card_Entry__c tec1 : TEList) {
         
         if(TEList.Friday__c!=null) {
             totalFridayHours += time_card_entry__c.Friday__c;
           }
         
         }

        return null;
    }
}

 
KaranrajKaranraj
In the for loop you are assigning TEList with the new variable tec1, but in your code inside the for loop you are still refering the TEList variable. That is the problem,just replace the error block with the below code
 
for(Time_Card_Entry__c tec1 : TEList) { 
         if(tec1.Friday__c!=null) {
             totalFridayHours += tec1.Friday__c;
           }
}