+ Start a Discussion
Mike CerrutiMike Cerruti 

System.Limit Exception: Too many SOQL queries: 101

I'm trying to create a class and method that will update a field on a record when a picklist value is changed.  I get the Too many SOQL queries error.

It is called by the following trigger:

trigger triggerUpdateTravelChange on Travel_Detail__c (after update, before update) {
    Travel_Detail__c[] travel_details = Trigger.new;
    If(Trigger.Isafter){
        //Updates the Change Travel Ticket Date and 
        //Increments the Change Travel Ticket Revision
        //on the Travel Ticket
        UpdateTravelChange.findTravelDetail(travel_details);
        updateTravelDetailStatus.updateTravelTicket(travel_details);

//TravelCalendarUpdate.TravelDetail(travel_details);

    }
    If(Trigger.Isbefore){
        //Updates the Change Revision and Change Date Fields
        // on the Travel Detail Record
        TravelDetailBooked.updateBooked(travel_details);
    }
}

Apex Class

trigger triggerUpdateTravelChange on Travel_Detail__c (after update, before update) {
    Travel_Detail__c[] travel_details = Trigger.new;
    If(Trigger.Isafter){
        //Updates the Change Travel Ticket Date and 
        //Increments the Change Travel Ticket Revision
        //on the Travel Ticket
        UpdateTravelChange.findTravelDetail(travel_details);
        updateTravelDetailStatus.updateTravelTicket(travel_details);

//TravelCalendarUpdate.TravelDetail(travel_details);

    }
    If(Trigger.Isbefore){
        //Updates the Change Revision and Change Date Fields
        // on the Travel Detail Record
        TravelDetailBooked.updateBooked(travel_details);
    }
}
Best Answer chosen by Mike Cerruti
ManojjenaManojjena
Hi Mike ,

It is for sure that trigger fall under recursion so try to create debug log and check how may times your trigger is executing .
Below is the link for sample code to control recursion ,Implement that your problem will resolve .
https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US)

Thnaks
Manoj
 

All Answers

Vinnie BVinnie B
You need to read up on 'bulkifying' a trigger.  Remember that this trigger could run as part of 100 Travel Details being updated at once.  It looks like that find* function (and others) is making a query.  You need to start by grabbing all of the data you need outside of the loop that will run one query that gets all of the data you need.

Also, it's generally good practice for the trigger to do nothing more than just call an Apex class that actually does all of the work.  That's not required but makes you're system easier to troubleshoot.
Mike CerrutiMike Cerruti
I pasted the trigger twice like a dummy.

The class is:


public class updateTravelDetailStatus {     
public static void updateTravelTicket(Travel_Detail__c[] travel_details){         
Integer i = 0;         
Integer c = 0;                  
List<Travel_Detail__c> travel_detail = new List<Travel_Detail__c>{};            
travel_detail = [Select Name                              
From Travel_Detail__c                              
Where detail_status__c = 'Cancel'                              
Order by Name                              ];              

For (Travel_Detail__c td :travel_detail){             
System.debug('Name: ' + td.Name +                          ' No: ' + i);                       i++;             

} // End List                 

List<Travel_Detail__c> detailToUpdate = new List<Travel_Detail__c>(); {         
For(Travel_Detail__c td :travel_detail){             
td.Change_Notes__c = 'Updated by Apex';             
detailToUpdate.add(td);                          
System.debug('Name: ' + td.Name +                         ' Notes: ' + td.Change_Notes__c);         
} // End For         
}     // End List         
update detailToUpdate;              
} // End Method
}  // End Class
 
Vinnie BVinnie B
The only thing I see is that the list passed from the trigger is travel_details but you refer to only travel_detail (no 's') in the rest of the class.  In other words, the trigger seems to work on a list of records generated from a query and not from the records in the trigger.  I'm not sure how this would generate a too many queries error but it just seemed odd to me.
Prasad PioneerPrasad Pioneer
Mike,
Please check if any Workflow rules defined on object "Travel_Detail__c", If so please deactivate and try to execute the trigger context. If you still see the same problem, check your trigger is calling recurssively. My strong feeling is trigger is being excuted recurssively. Please use static boolean to control the recurrsive execution of trigger. This will solves your problem.

Please mark solved/best answer. if this helps for you.
 
ManojjenaManojjena
Hi Mike ,

It is for sure that trigger fall under recursion so try to create debug log and check how may times your trigger is executing .
Below is the link for sample code to control recursion ,Implement that your problem will resolve .
https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US)

Thnaks
Manoj
 
This was selected as the best answer
Vinnie BVinnie B
I thought about the recursive angle as well.  Is it odd for a trigger to call an action on a beforeInsert AND an afterInsert.  My thought was that you would want one or the other but not both.  Can you try commenting one of those out of the trigger and see if that makes a difference?  In any event, that might help you see which of those two is causing the problem.
Mike CerrutiMike Cerruti
I'm new to this game and want to do things proper and I've been struggling with List<object>.  The recursive solution worked.  I did try all of them.  Thank you so much.  I can get on with my day!!!