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
Nishigandha JadhavNishigandha Jadhav 

Error: System.LimitException: Too many SOQL queries: 101 Trigger using data loader

Helper Class:
public class test{
public static void addContactRole(List<Opportunity> opps) {
List<OpportunityContactRole> oppCR=new List<OpportunityContactRole>();
        for(Opportunity opp : opps ){
           
            if(opp.StageName != 'Prospecting' && opp.StageName != 'Qualification' ){
           
            
              oppCR= [Select Id, opportunityId FROM OpportunityContactRole WHERE opportunityId =:opp.Id];
               
                if(oppCR == null || oppCR.size() == 0){
                   
                    opp.addError(' Opportunity must have at least one Contact Role assigned for any stages beyond 1');
                }
            }
        }
    }

Trigger:
trigger updatecontactrolecount on Opportunity (before insert, before update)
{
test.addContactRole(Trigger.New);
 
}

Note: If opportunity stage is other than Prospecting and Qualifiation then opportunity must have at least one contact role.(working fine but getting to many soql query error for bulk update or insert)
 
Best Answer chosen by Nishigandha Jadhav
Raj VakatiRaj Vakati
My understanding of this trigger .. 

You dnt need before insert .. only before an update is sufficient ..  use the below code 
 
trigger updatecontactrolecount on Opportunity (before update)
{
test.addContactRole(Trigger.New ,  Trigger.newmap);
 
}

public class test{
public static void addContactRole(List<Opportunity> opps ,Map<Id , Opportunity> oppsMap) {
	
	List<OpportunityContactRole> ocrDara = [select Id, opportunityId FROM OpportunityContactRole WHERE opportunityId IN :oppsMap.keySet()];
	
	Set<Id> oppIdsFromOCR = new Set<Id>() ; 
	for(OpportunityContactRole cc :ocrDara){
		oppIdsFromOCR.add(cc.opportunityId);
	}
	
	

        for(Opportunity opp : opps ){
            if(opp.StageName != 'Prospecting' && opp.StageName != 'Qualification' ){
                if(!oppIdsFromOCR.contains(opp.Id){
                    opp.addError(' Opportunity must have at least one Contact Role assigned for any stages beyond 1');
                }
            }
        }
    }


 

All Answers

ranbir das 1ranbir das 1
Hi Shubhada,
please do not use soql inside for loop.
List<OpportunityContactRole> OpportunityContactRoleData = [select Id, opportunityId FROM OpportunityContactRole WHERE opportunityId IN :Trigger.newMap.keySet()];
for(OpportunityContactRole obj:OpportunityContactRoleData){
      if(obj == null || obj.size() == 0){
         opp.addError(' Opportunity must have at least one Contact Role assigned for any stages beyond 1');
      }
}
 
Raj VakatiRaj Vakati
My understanding of this trigger .. 

You dnt need before insert .. only before an update is sufficient ..  use the below code 
 
trigger updatecontactrolecount on Opportunity (before update)
{
test.addContactRole(Trigger.New ,  Trigger.newmap);
 
}

public class test{
public static void addContactRole(List<Opportunity> opps ,Map<Id , Opportunity> oppsMap) {
	
	List<OpportunityContactRole> ocrDara = [select Id, opportunityId FROM OpportunityContactRole WHERE opportunityId IN :oppsMap.keySet()];
	
	Set<Id> oppIdsFromOCR = new Set<Id>() ; 
	for(OpportunityContactRole cc :ocrDara){
		oppIdsFromOCR.add(cc.opportunityId);
	}
	
	

        for(Opportunity opp : opps ){
            if(opp.StageName != 'Prospecting' && opp.StageName != 'Qualification' ){
                if(!oppIdsFromOCR.contains(opp.Id){
                    opp.addError(' Opportunity must have at least one Contact Role assigned for any stages beyond 1');
                }
            }
        }
    }


 
This was selected as the best answer