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
fredkafredka 

condense trigger?

I am running into soql limits for my triggers... I am trying to condense as much as I can.  Is there any way to condense the following trigger?  I was hoping to only have one select statement but not sure how to achieve this.. thanks so much!!!

 

Fred

 

trigger UpdateManager on Quote__c (before insert, before update) { //this trigger is used for two things: //1. update the record owners manager on the quote record //2. update the underwriter field on quote with the name of the underwriter... this is needed so // I can get the UW name as a user on the quote so I can use in automated emails (can't use workflow) //create list to hold quote owner id List<Id> quoteOwners = new List<Id>(); //create list to hold underwriter id List<Id> Underwriters = new List<Id>(); //loop through quotes and save owner id to quoteOwners list, save underwriter id to underwriters list for (Quote__c q : Trigger.new){ quoteOwners.add(q.ownerid); Underwriters.add(q.UnderwriterID__c); } //create map to hold the UserID and the Direct Manager Map<Id,User> userToDirectManagerMap = new Map<Id,User>([select Id, Direct_Manager__c from User where Id IN :quoteowners]); //create map to hold the UserID and User Name Map<Id,User> UserToUnderwriterMap = new Map<Id,User>([select Id, Name from User where Id IN :Underwriters]); //Loop through the quotes that caused the trigger for(Quote__c q : Trigger.new){ //update the manager field on quote based on the owner of the record q.Manager__c = userToDirectManagerMap.get(q.OwnerId).Direct_Manager__c; //update the Underwriter field on quote based on the underwriterID field on quote if(UserToUnderwriterMap.get(q.UnderwriterID__c) <> null) { q.UnderwriterToEmail__c = UserToUnderwriterMap.get(q.UnderwriterID__c).ID; } } }

 

InsertWittyNameInsertWittyName

Untested, but worth a go...

 

 

trigger UpdateManager on Quote__c (before insert, before update) { // this trigger is used for two things: // 1. update the record owners manager on the quote record // 2. update the underwriter field on quote with the name of the underwriter... this is needed so // I can get the UW name as a user on the quote so I can use in automated emails (can't use workflow) // create list to hold quote owner id List<Id> users = new List<Id>(); // loop through quotes and save owner id to quoteOwners list, save underwriter id to underwriters list for (Quote__c q : Trigger.new) { users.add(q.ownerid); users.add(q.UnderwriterID__c); } // create map to hold the UserID and the Direct Manager Map<Id,User> userMap = new Map<Id,User>([select Id, Name, Direct_Manager__c from User where Id IN :users]); // Loop through the quotes that caused the trigger for(Quote__c q : Trigger.new) { // update the manager field on quote based on the owner of the record q.Manager__c = userMap.get(q.OwnerId).Direct_Manager__c; // update the Underwriter field on quote based on the underwriterID field on quote if(userMap.get(q.UnderwriterID__c) <> null) { q.UnderwriterToEmail__c = userMap.get(q.UnderwriterID__c).ID; } } }

 

 

 

dmchengdmcheng

Which select statement is causing the error?

 

BTW - generally you want to use Set instead of List for IDs, since Set automatically prevents duplicates in the set whereas List does allow duplicate elements.

fredkafredka

Thanks for responding!!! I am actually getting the "too many soql statements" error on another trigger... But since it is really that I have too many soql statements, I am looking at all of them to see how I can fix the problem.

 

I just saw last night that it is probably a best practice to have only one trigger for each object... I just think it is going to look pretty confusing with everything jammed into one trigger... Any suggestions would be greatly appreciated.

 

Thanks again!!! 

 

Fred

dmchengdmcheng

The error is happening in another trigger?  It's hard to troubleshoot an error that is occurring somewhere else.

 

You should definitely combine the triggers into a single one.