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
davidesidavidesi 

Too Many SOQL 'l 101 error outside a loop

Hi to all.

I'm getting a SOQL error saying that there are too many SOQL Queries . I have been searching that error in forums, and the solution that everybody provides is that that query must be outside a loop.

But in my code the query is not IN any loop.

 

Anybody can help me?

 

 

Here is the code.

 

trigger ownerUpdateChooseRate on Choose_Rate__c (before Insert , before Update) {
set<id> opids =new set<id>();
set<id> ids=new set<id>();
for(Choose_Rate__c o:Trigger.new)
{
system.debug('ANEG=============='+o.ANEG__c);
system.debug('ids=============='+o.Opportunity_c__c);
opids.add(o.Opportunity_c__c);
if(o.ANEG__c==true && o.Opportunity_c__c!=null )
ids.add(o.Opportunity_c__c);
}
if(ids.size()>0)
{
Map<Id,Opportunity> op=new Map<Id,Opportunity>([select id,ownerid from opportunity where id in:ids]);

for(Opportunity o:op.values())
{
system.debug('mapids========================='+o.id);

}
for(Choose_Rate__c o:Trigger.new)
{
if(o.ANEG__c==true)
{
system.debug('id======='+o.Opportunity_c__c);
system.debug('ID==============='+op.get(o.Opportunity_c__c).ownerid);
o.ownerid=op.get(o.Opportunity_c__c).ownerid;
o.ANEG__c=false;
}

}
}
if(trigger.isInsert )
{
system.debug('Inside the Process for removing old rates');
List<Choose_Rate__c> oldList=new List<Choose_Rate__c>([Select id,Opportunity_c__c from Choose_Rate__c where Opportunity_c__c in:opids]);
if(oldList.size()>0)
{
for(Choose_Rate__c r:oldList)
{
system.debug('oldlist==========================='+r.id);
r.Opportunity_c__c=null;
r.ANEG__c=false;
}
try{update oldList;}catch(Exception e){}
}
}

}

Sridhar BonagiriSridhar Bonagiri

Hi,


Can you tell me when you are getting this error, while creating a single record or bulk insert. Can you pl. post the indented trigger code.

 

Regards,

Sridhar Bonagiri

If this post is your solution, kindly mark this as the solution and give Kudos.

davidesidavidesi

Thank you for your quick answer.

 

I get that error (not allways) when creating a single record.

 

The idented code is following.

 

trigger ownerUpdateChooseRate on Choose_Rate__c (before Insert , before Update) {
set<id> opids =new set<id>();
set<id> ids=new set<id>();
for(Choose_Rate__c o:Trigger.new)
{
    system.debug('ANEG=============='+o.ANEG__c);
    system.debug('ids=============='+o.Opportunity_c__c);
    opids.add(o.Opportunity_c__c);
    if(o.ANEG__c==true && o.Opportunity_c__c!=null )
    ids.add(o.Opportunity_c__c);
}
if(ids.size()>0)
{
Map<Id,Opportunity> op=new Map<Id,Opportunity>([select id,ownerid from opportunity where id in:ids]);

for(Opportunity o:op.values())
    {
    system.debug('mapids========================='+o.id);

    }
for(Choose_Rate__c o:Trigger.new)
    {
    if(o.ANEG__c==true)
    {
        system.debug('id======='+o.Opportunity_c__c);
        system.debug('ID==============='+op.get(o.Opportunity_c__c).ownerid);
        o.ownerid=op.get(o.Opportunity_c__c).ownerid;
        o.ANEG__c=false;
    }

    }
}
if(trigger.isInsert )
    {
    system.debug('Inside the Process for removing old rates');
    List<Choose_Rate__c> oldList=new List<Choose_Rate__c>([Select id,Opportunity_c__c from Choose_Rate__c where Opportunity_c__c in:opids]);
    if(oldList.size()>0)
    {
    for(Choose_Rate__c r:oldList)
    {
    system.debug('oldlist==========================='+r.id);
    r.Opportunity_c__c=null;
    r.ANEG__c=false;
    }
    try{update oldList;}catch(Exception e){}
    }
    }

}

 

Thanks

Vinit_KumarVinit_Kumar

Can you check debug logs ,I am sure some other actions (I mean some other Apex code) running in the same context.