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

New Eyes on my Trigger

Can anyone tell me what is wrong with this simple trigger?  I'm sure I'm just missing something stupid, I'm just not seeing it.  Maybe I'm just completely off track.  I just need to update a single field on the triggering lead upon Insert or Update.


Eclipse doesn't give me any real error message, it just says that and unexpected error occured and to try again.



My code 



trigger Calculate_Lead_Qualification_Score on Lead (before insert, before update)

    for(Lead theLead :  //loop through each lead that is being inserted or updated

            //SFDC doesn't like the line below.  No error, it just won't save if this line is included       
            theLead.Lead_Qualification_Score__c = theLead.Application_Suitability_Score__c + theLead.Budget_Score__c + theLead.Role_Score__c + theLead.Timing_For_Purchase_Score__c;
            system.debug('Calculated Lead Qualification Score: ' + theLead.Lead_Qualification_Score__c);











jbroquist said:  (though the post is gone now)




for(List<Lead> theLead : 





I'm not sure I understand this.  Isn't this going to be creating a list of lists?  

The original code creates a list as part of the loop, adding this is going to create a single record list for each record in the list.







I have written this same trigger about 5 different ways, both before and after triggers, bulk non-bulk, DML, etc... 

No matter how I do it, I always get the same useless error on the one line of code that attempts to insert or update a record.



I have written many triggers in the past and have never had this problem where I can't update the original triggering record.







One final rewrite and I have it working.


I simply copied the loop and internal code into a public static class method, then passed to the method which did the work. 


I can't think of any reason why this code would fail in a trigger and work from within a class, but it does.





Sounds like it may have been a fluke error as I also can't see any problems with your initial code.  Is there a reason you can't use a formula field instead of doing the sum in a trigger?


btw- I just launched my free app on the AppExchange which might save you some coding.


It's pretty flexible and scales too hundreds of rules such as your examples.  




Thanks for taking a look. 


We had to resort to a trigger because the formula is based upon a number of formula fields, which in turn are based upon formula fields, the number of lines of formula code quickly became too great and Salesforce would not allow it.


I will take a look at your AppExchange, thanks.