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
blake.tanonblake.tanon 

(Not in Loop?) System.LimitException: Too many SOQL queries: 101

I'm getting the too many SOQL limit on the start of a trigger, the query isn't in a loop but the set of IDs is retrieved in a loop - maybe that is why?  If so how could I get around this?

 

trigger eventTrigger on Event (before insert, before update) {

    set<ID> owner = new Set<ID>();
        for(event t:trigger.new)
        owner.add(t.ownerid);

    List<User> u = [SELECT Id, UserRole.Name
                           FROM USER
                           WHERE id in: owner
                           LIMIT 1];

//logic below

}

 

bob_buzzardbob_buzzard

That shouldn't give you a problem - you only have a single SOQL query there.

 

Are there other triggers firing in this transaction?  If not, can you post the rest of your trigger code?

kiranmutturukiranmutturu

what is the logic below? is there any other queries? or this may be recursive trigger?

blake.tanonblake.tanon

I just realized I have two triggers on Event that are running an query to users, although they are both outside of a loop and run on a set of IDs.  When I disable either they other will pass, I will try to combine them into one trigger and see how it goes.

blake.tanonblake.tanon

Here is the full trigger

 

trigger eventTrigger on Event (before insert, before update) {
// Bulk Apex Support
// Adds Account to WhatId and sets the Last Meeting Date on Contact if Task is Meeting Occurred and a greater date.

// Verion 2

    Set<ID> who = new Set<ID>();
    set<ID> owner = new Set<ID>();
        for(event t:trigger.new)
        owner.add(t.ownerid);

    List<User> u = [SELECT Id, UserRole.Name
                           FROM USER
                           WHERE id in: owner
                           LIMIT 1];
                           
                           for(Integer i = 0; i < trigger.new.size(); i++)
                               if(u.size() > 0){
                                   for(User user: u){
                                       if(Trigger.new[i].ownerid == user.id){
                                       if(user.userrole.name == 'Admin' ||
                                          user.userrole.name == 'Internal Wholesaler' ||
                                          user.userrole.name == 'External Wholesaler'){
                                       trigger.new[i].xOwnerWS__c = true;
                                       who.add(trigger.new[i].whoid);
                                       }
                                       }
                                   }
                               }
    
    if( who != null){
    
        List<Contact> c = [SELECT Phone, MailingState, Territory__c, AccountId, xLastMeetDate__c
                           FROM Contact
                           WHERE id in:who
                           LIMIT 1];
                           
        for(Integer i = 0; i < trigger.new.size(); i++)
            if(c.size() > 0){
                for(Contact cont: c){
                    if(Trigger.new[i].whoid == cont.id){
                        Trigger.new[i].Territory__c = cont.territory__c;
                        Trigger.new[i].State__c = cont.MailingState;
                        Trigger.new[i].Rep_Phone__c = cont.Phone;
                        Trigger.new[i].WhatId = cont.AccountId;
                    // Contact Dates

                    if(Trigger.new[i].Outcome__c == 'Meeting Occurred'){
                        
                        if(cont.xLastMeetDate__c == null){
                            cont.xLastMeetDate__c = Trigger.new[i].EndDateTime;
                        }
                        if(cont.xLastMeetDate__c < Trigger.new[i].EndDateTime){
                            cont.xLastMeetDate__c = Trigger.new[i].EndDateTime;
                        }
                        
                        if(Trigger.new[i].Time_Completed__c == null){
                            Trigger.new[i].Time_Completed__c = Trigger.new[i].EndDateTime;
                        }
                    }
                    update cont;
                    }
                }
            }
            
    
    
    }









}

 

kiranmutturukiranmutturu

first change should be you should update the contact after all the loops end....and also you can update the looping object so you need to declare a list object and add the loop object to that and do the update on the list