You need to sign in to do that
Don't have an account?

code optimization needed
Hello Folks. I have four custom pick up fields in Opportunity. Every opportunity has a look up relationship with Campaign. My trigger will loop through all the opportunity records and if any of the custom pick lists is saved with a value 'Completed ' it updates the count in Campaign's custom field. I coded a trigger an it is working perfect. I need help in optimizing this code cause I feel like I have used too many try-catch blocks and also SOQL statements inside for loops. Thanks in Advance.
trigger Site_visit_update_in_campaign on Opportunity (after insert,after update,after delete) { public Boolean s1NullCheck=false; public Boolean s2NullCheck=false; public Boolean s3NullCheck=false; Public Boolean s4NullCheck=false; List <Campaign> campaigns=[select id from campaign]; Set<Id> keysForOpp=new Set<Id>(); Map<Id,List<Opportunity>> oppList=new Map<Id,List<Opportunity>>(); List<Id> filteredIds=new List<Id>(); Campaign[] campaign=new Campaign[]{}; Map<Id,Campaign> campaignToUpdate=new Map<Id,Campaign>([Select Id,Completed_Site_Visit_1__c,Completed_Site_Visit_2__c,Completed_Site_Visit_3__c,Completed_Site_Visit_4__c From campaign]); for(Opportunity oppor:Trigger.new) { for(Campaign c:campaigns) { keysForOpp.add(c.Id); } for(Opportunity o:[Select CampaignId,Site_Visit_1__c,Site_Visit_2__c,Site_Visit_3__c,Site_Visit_4__c From Opportunity Where CampaignId=:keysForOpp]) { try { List<Opportunity> temp=oppList.get(o.CampaignId); if(temp==null) { oppList.put(o.CampaignId, new List<Opportunity>{o}); filteredIds.add(o.CampaignId); } else { temp.add(o); } } catch(NullPointerException e) { System.debug('Exception at custom Map setting : '+e.getMessage()); } } System.debug('opportunity debug'+opplist); for(Id i:filteredIds) { Integer siteVisitOneCounter=0; Integer siteVisitTwoCounter=0; Integer siteVisitThreeCounter=0; Integer siteVisitFourCounter=0; for(Opportunity o:oppList.get(i)) { System.debug('Opportunity after calling key : '+o); try { if(o.Site_Visit_1__c !=null) { s1NullCheck=true; System.debug('S1check : '+s1NullCheck); } } catch(NullPointerException e) { System.debug('Exeeption at siteVisitOne : '+e.getMessage()); System.debug('S1check : '+s1NullCheck); } try { if(o.Site_Visit_2__c !=null) { s2NullCheck=true; System.debug('S2check : '+s2NullCheck); } } catch(NullPointerException e) { System.debug('Exeeption at siteVisitOne : '+e.getMessage()); System.debug('S2check : '+s2NullCheck); } try { if(o.Site_Visit_3__c !=null) { s3NullCheck=true; System.debug('S3check : '+s3NullCheck); } } catch(NullPointerException e) { System.debug('Exeeption at siteVisitOne : '+e.getMessage()); System.debug('S13heck : '+s3NullCheck); } try { if(o.Site_Visit_4__c !=null) { s4NullCheck=true; System.debug('S4check : '+s4NullCheck); } } catch(NullPointerException e) { System.debug('Exeeption at siteVisitOne : '+e.getMessage()); System.debug('S4check : '+s4NullCheck); } if(s1NullCheck==true) { if(o.Site_Visit_1__c=='Completed') { System.debug('Opportunity : '+o.Site_Visit_1__c); siteVisitOneCounter=siteVisitOneCounter+1; } s1NullCheck=false; } if(s2NullCheck==true) { if(o.Site_Visit_2__c=='Completed') { System.debug('Opportunity : '+o.Site_Visit_2__c); siteVisitTwoCounter=siteVisitTwoCounter+1; } s2NullCheck=false; } if(s3NullCheck==true) { if(o.Site_Visit_3__c=='Completed') { System.debug('Opportunity : '+o.Site_Visit_3__c); siteVisitThreeCounter=siteVisitThreeCounter+1; } s3NullCheck=false; } if(s4NullCheck==true) { if(o.Site_Visit_4__c=='Completed') { System.debug('Opportunity : '+o.Site_Visit_4__c); siteVisitFourCounter=siteVisitFourCounter+1; } s4NullCheck=false; } } System.debug('Site Visit One Total : '+siteVisitOneCounter); System.debug('Site Visit two Total : '+siteVisitTwoCounter); System.debug('Site Visit three Total : '+siteVisitThreeCounter); System.debug('Site Visit four Total : '+siteVisitFourCounter); Campaign cam=campaignToUpdate.get(i); cam.Completed_Site_Visit_1__c=siteVisitOneCounter; cam.Completed_Site_Visit_2__c=siteVisitTwoCounter; cam.Completed_Site_Visit_3__c=siteVisitThreeCounter; cam.Completed_Site_Visit_4__c=siteVisitFourCounter; campaign.add(cam); try { update campaign; System.debug('Campaign update Success'); } catch(Exception e) { System.debug('Failed to update campaig : '+e.getMessage()); } } } }

You might want to bulkify your code. Check this (https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code) article. Y