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
Tarun NaiduTarun Naidu 

Too many SOQL queries: 101 when updating using dataloader due to Below Trigger


trigger OppPrimaryConRole on Opportunity (before update) {
for(Opportunity opp: trigger.new)
{
    
    try
    {
        string BRL_contactId = opp.BRL_Contact__c;
        string Renewal_contactid = opp.Renewal_Contact__c;
       
            List<OpportunityContactRole> primarycontactrole = [select contactid,contact.email from OpportunityContactRole where isprimary=true and opportunityid= :opp.id limit 1];
            if(primarycontactrole.size()>0)
            {
                if (BRL_contactid != primarycontactrole[0].Id )
                {
                //opp.put('Primary_contact__c',primarycontactrole[0].contactId);
                system.debug('primary id is'+ primarycontactrole[0].contactId);
                system.debug('renewal email is'+ primarycontactrole[0].contact.email);
                opp.Renewal_Contact__c = primarycontactrole[0].contactId;
                opp.BRL_Contact__c = primarycontactrole[0].contactId;
                opp.BRL_Renewal_Email__c = primarycontactrole[0].contact.email;
                system.debug('renewal contact id is'+ opp.Renewal_Contact__c);
                system.debug('BRL Contact is'+ opp.BRL_Contact__c);
            }
      }
    }
    catch(exception e)
    {
        system.debug('Error:'+e);
    }
}
}
GauravendraGauravendra
Hi Tarun,

Total number of SOQL queries allowed are 100. So, when you are updating the 101th opportunity its throwing error.
The solution is to put the SOQL query outside of for loop.
 
List<OpportunityContactRole> primarycontactrole = [select contactid,contact.email from OpportunityContactRole where isprimary=true and opportunityid in Trigger.New];
And use the loop for primarycontactrole.
for(Opportunity opp: primarycontactrole)
Hope this helps.