You need to sign in to do that
Don't have an account?
sfdc-admin-sm
Please critique my trigger
Hello,
I wrote the trigger code below and it works fine. However I feel it can be optimized better to follow best practices. Can you please review it and let me know what enhancements I could have used?
I know 1 best practice is that I shouldn't write all this logic in Trigger but instead use a helper class and let the Trigger call that help class. Any other improvements? Please provide code samples if possible. I am trying to learn.
Thanks in advance...
I wrote the trigger code below and it works fine. However I feel it can be optimized better to follow best practices. Can you please review it and let me know what enhancements I could have used?
I know 1 best practice is that I shouldn't write all this logic in Trigger but instead use a helper class and let the Trigger call that help class. Any other improvements? Please provide code samples if possible. I am trying to learn.
Thanks in advance...
//Trigger that updates the AnnualAmount Account field based on the most recent Opportunity's AnnualAmount associated with the Account where the Opportunity Owner is not the test user and //AnnualAmount is > $0. trigger UpdateAnnualAmountAcct on Opportunity (after insert, after update) { list<Opportunity> lstOpp = new list<Opportunity>(); for(Opportunity o : Trigger.new){ if(o.AnnualAmount__c > 0 && o.OwnerId != '00580000003mBZb'){ //id of test user lstOpp.add(o); } } list<Opportunity> mostRecentOppty = new list<Opportunity>(); if(lstOpp != NULL){ mostRecentOppty = [SELECT Id, CloseDate, AnnualAmount__c, AccountId FROM Opportunity WHERE Id IN : lstOpp ORDER BY CloseDate DESC LIMIT 1]; } list<Account> acctToUpdate = new list<Account>(); for(Opportunity o : lstOpp){ Account a = new Account(); a.Id = o.AccountId; a.AnnualAmount__c = o.AnnualAmount__c; acctToUpdate.add(a); } update acctToUpdate; }
NOTE: This code has not been tested and may contain typographical or logical errors.
All Answers
NOTE: This code has not been tested and may contain typographical or logical errors.