You need to sign in to do that
Don't have an account?
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 : trigger.new) //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);
}
}
thanks
Jon
jbroquist said: (though the post is gone now)
Try:
for(List<Lead> theLead : trigger.new)
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 trigger.new list.
Correct?
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.
Thanks
Jon
One final rewrite and I have it working.
I simply copied the loop and internal code into a public static class method, then passed trigger.new 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.
Jon
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.
https://sites.secure.force.com/appexchange/listingDetail?listingId=a0N300000024tT3EAI
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.
Jon