+ Start a Discussion
Justin StaugaitisJustin Staugaitis 

Apex trigger not summing fields

Im new to APEX and i have a trigger that is suppose to calculate a total score using 6 fields.  If any of the data in those 6 fields gets updated I want the Trigger to recalcuate the score.  But my current code is not updating.  I have two issues

1) After I deployed the code the APEX trigger didnt do the calculation.  Why is this? 
2) After I made a change to one of the 6 data points the Trigger still didnt execute.  Why is this?

Could someone help look at the code I have and provide some recommeneded changes.  (just note I am new so some of the terms I may not understand if you would make any code changes can you show me the before and after change).  

trigger Health_Score_Calculation_New on Account (before insert) {
  if(Trigger.isBefore)
  {
    if (Trigger.isInsert || Trigger.isUpdate)
     {
       for(Account cob : trigger.new)

         {
             if(cob.Days_With_No_Activity__c!=null){
            //   cob.Days_With_No_Activity__c= 100 ;
             
             
            cob.Score__c = (cob.Implementation__c * 0.15) + 
                           (cob.Migration_In_Progress__c * 0.20) + 
                           (cob.Days_With_No_Activity__c * 0.15) + 
                           (cob.Contact_New2__c * 0.15) + 
                           (cob.Contact_Left2__c * 0.15) + 
                           (cob.Issues__c * 0.20) ;
                           }
           }
       }
    }
}
nitin sharma 356nitin sharma 356
You ca do this using formula field .Create formula field,sum your six fields in formula field and that will be solve your problem
Justin StaugaitisJustin Staugaitis
Complier issue over 5000 so I had to create it in APEX the other 6 fields are refererened formaulas 

I added (before insert, before update) and that seems to resolve my issue with question 2 but I had to update the record for the change to take affect.  

So is there a way i can have all the records update so this trigger populates for all accounts.  
 
Justin StaugaitisJustin Staugaitis
Solved question 1 as well.  Using Data loader I updated the Account id with a checkbox.  This updated all the records and trigged my code.