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
rajesh_yrajesh_y 

Too many SOQL queries: 101

Hi  I i am getting following error when try to update bulk of records  from system log

Too many SOQL queries: 101

trigger updatetrigg on Lead_Categories__c (before update)
{
list<Related_Team_Member__c > rtmlist=new list<Related_Team_Member__c >();

list<Related_Team_Member__c > tlist=new list<Related_Team_Member__c >();
list<teammember__c> teamlist=new list<teammember__c>();
list<teammember__C> tmlist=new list<teammember__C>();
for(Lead_Categories__c o:trigger.old)
{
for(Lead_Categories__c l:trigger.new)
{
teamlist=[select Access_Category__c,Access_Item__c,Access_Level__c from teammember__c where Access_Level__c=:l.Access_Level__c and  Access_Item__c=:l.Access_Item__c and Access_Category__c=:l.Access_Category__c limit 1];

if(o.accepted__c==false)
{
if(l.accepted__c==true && l.type__c=='Add')
{
//list<teammember__c> tlist1=[select Access_Category__c,Access_Item__c,Access_Level__c from teammember__c where Access_Level__c=:l.Access_Level__c and  Access_Item__c=:l.Access_Item__c and Access_Category__c=:l.Access_Category__c limit 1];

/*for(teammember__c team:tlist1)
{
teamlist.add(team);
}*/
if(teamlist.size()>0)
{
for(teammember__c team:teamlist)
{
Related_Team_Member__c rtm1=new Related_Team_Member__c(); 
rtm1.team__C=l.team__C;
rtm1.teammember__C=team.id;
l.teammember__c=team.id;

rtmlist.add(rtm1);
}
}
else
{
teammember__c t=new teammember__c();
t.Access_Category__c=l.Access_Category__c;
t.name=t.id;
t.Access_Item__c=l.Access_Item__c;
t.Access_Level__c=l.Access_Level__c;
t.team__c=l.team__c;
//insert t;
tmlist.add(t);
l.teammember__c=t.id;


}
}
if(l.accepted__c==true && l.type__c=='Remove')
{
for(Related_Team_Member__c tt:[select id from Related_Team_Member__c where teammember__c=:l.teammember__C and team__c=:l.team__C])
{
tlist.add(tt);

}
}
}
}
}
if(tlist.size()>0)
delete tlist;
if(tmlist.size()>0)
insert tmlist;
if(rtmlist.size()>0)
insert rtmlist;
}

 Whats wrong in my please let me know

 

 

thank you in advance

 

 

MiddhaMiddha

Can you descibe the requirement you are trying to implement. Looking at your code, you should not have queries within the for loops and same applies for DML statements as well.

 

You need NOT have an inner loop to compare old values with the new values it can simply be done as:

 

for(Lead_Categories__c l: Trigger.new)
{
 if(l.accepted__c && !Trigger.oldMap.get(l.Id).accepted__c)
   {
        ......
   }
}

 If you need to query some records, collect the filters for where clause in a set, while you are in a loop and query outside loop.

rajesh_yrajesh_y

Hi Thank for your quick response,

 when I update all lead categories by checking accepted=true,then it will create temmembers for lead categories .and there is another trigger on temmember object that will create related teammebers for teamembers.

 

 

this is the requirement.

 

thank you in advance

Chamil MadusankaChamil Madusanka

Do not query in for loops. IT might be cause to exceed the SOQL limit.

 

Refer following linkand see Best Practice #2: Avoid SOQL Queries inside FOR Loops

http://wiki.developerforce.com/page/Apex_Code_Best_Practices

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.