I wrote a trigger to find the largest amount value for all Opportunities under a particular Account and then update a custom field on Account with the name of the Opportunity. It works fine with single records but as you can see it doesn't take bulk processing into account and fails because it calls too many SOQL queries.
I've tried putting the queries into maps but I generally end up with null values. If someone could point me in the right direction, I think I could figure it out, but I'm kind of stuck right now.
trigger UpdateLoanName on Opportunity (before update) {
for (Opportunity opp : Trigger.new){
if(opp.AccountId != null){
Opportunity oppname = [SELECT name, amount FROM Opportunity WHERE accountid = :opp.AccountId ORDER BY amount DESC LIMIT 1];
Account acc = [SELECT loan_name__c FROM Account WHERE id = :opp.AccountId];
acc.loan_name__c = oppname.name;
if(acc.loan_name__c != null){
update acc;
}
}
}
}