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
Shraddha D GuptaShraddha D Gupta 

System.LimitException: Too many SOQL queries: 101 Trigger

Hi All,

I am not a developer and in our Org few triggers are written by previous developers and recently we started getting SOQL error a lot and I am not sure how I can fix it. 

In one of the error log, I am seeing the name of this trigger and hence thought of getting reviewed it with the community -

rigger leadAutomaticAssignmentToCampaign on Lead (after insert, after update) {
    // This trigger assigns automatically all BrigthTalk leads to the BrightTalk parent campaign in sfdc with the member status "Subscriber"
    Map<Id,Lead> leadsToAssign = new Map<Id,Lead>();
    for (Lead newLead :{
        if ( newLead.Lead_Gen_Event__c != null && newLead.Lead_Gen_Event__c.compareTo('online_brighttalk_2015') == 0) {
    // Get the BrightTalk parent campaign in sfdc
    // CHANGE THE CAMPAING ID to run leadAutomaticAssignmentToCampaignTest or TO DEPLOY TRIGGER IN PRO
    // DEV6 Campaing ID = 7018A0000006QyU
    // PRO Campaing ID = 70170000001Cx0Y
    List<Campaign> parentBrightTALKCampaign = [SELECT Id, ParentId, Type, Status, Lead_Gen_Event__c FROM Campaign WHERE Id = '70170000001Cx0Y'];
    if (parentBrightTALKCampaign.size() == 1){
        List<CampaignMember> newMembers = new List<CampaignMember>();
        List<CampaignMember> currentMembers = [SELECT LeadId
                                               FROM CampaignMember 
                                               WHERE CampaignId = :parentBrightTALKCampaign.get(0).id AND LeadId IN :leadsToAssign.keyset()];
        // Check if the Lead is already a campaing member
        for (CampaignMember currentMember : currentMembers){
            if (leadsToAssign.containsKey(currentMember.leadId)){
        for(Lead lead : leadsToAssign.values()){
            newMembers.add(new CampaignMember(CampaignId = parentBrightTALKCampaign.get(0).id, LeadId = lead.Id, Status = 'Subscriber'));
        insert newMembers;

Also, a lot more errors are comming in leadspace trigger to but Leadspace trigger is the managed package trigger and I heard that managed packgae do not count for the limits but when I checked the Installed package list I found that limit checkbox is TRUE against the Leadspace package. 

I have also read few people saying that PB can also trigger SOQL isuse , is that true? 

Can anyone guide me how I can fix SOQL issues? 

Any help would be much appreciated. 

SwethaSwetha (Salesforce Developers) 
HI Shraddha,
I'm afraid you will need to loop in a member of the development team to make changes in the code. Basically,you need to modify the trigger by enabling the debug logs and check where the SOQL limits are being consumed and try reducing the count to avoid the error.
Sample debug statement: System.debug('Total Number of SOQL Queries allowed in this Apex code context: ' + Limits.getLimitQueries());

Details on how to debug this error are mentioned in this post- which should help you get started

Best practices to be followed are:
-Avoid SOQL queries that are inside FOR loops.
-Make sure you don't have a recursive loop calling a SOQL.
-Do not do any DML/CRUD inside a for loop.
-Avoid more than one DML on single object in single transaction - because each DML invokes trigger of related object can cause addition of SOQL count.
-Avoid too many field updates in process builders in different conditional branches. Each branch runs a DML and initiate trigger execution.
-If there are lot of business processes and logic in your trigger then design and isolate processes with help of future and batch if needed.
-Recursion Handling - To ensure a trigger is not running recursively.
Minimize the No.of SOQLs by merging the queries


If this information helps, please mark the answer as best.Thank you