You need to sign in to do that
Don't have an account?

Error: Too many SOQL queries: 101??
Hi,
I have a lookup field on RR__c and Proposal__c for another object Employee__c. Based on the field value in Proposal object i need to update the RR__c record. I am using the following code..which is throwing an error
Too many SOQL queries: 101
trigger updateEmpinRR on Proposal__c (after insert,after update)
{
List < Id > rrIds = new List < Id >();
List < Id > proposalIds = new List < Id >();
for ( Proposal__c c: Trigger.New )
{
proposalIds.add( c.Id );
rrIds.add(c.Proposal_RR__c);
}
List<RR__c> opps = [select id, Selected_Employee__c from RR__c where id in :rrIds];
Map < Id ,RR__c > empmap = new Map < Id , RR__c >();
for ( RR__c a : opps )
{
empmap.put( a.Id, a);
}
List < RR__c > EmpToUpdate = new List < RR__c >();
for(Proposal__c c: Trigger.New)
{
RR__c ac = empmap.get( c.Proposal_RR__c);
if ( ac == null )
{
continue;
}
If (c.Status__c == 'Selected-New')
{
ac.Selected_Employee__c = c.RMG_Employee_Code__c;
EmpToUpdate.add( ac );
}
else
{
ac.Selected_Employee__c = null;
EmpToUpdate.add( ac );
}
}
upsert EmpToUpdate;
}
Can you please post the complete error as I think your trigger code is fine but the classes from where this trigger is triggered may have problem.
Thanks
Ankit Arora
Blog | Facebook | Blog Page
Hi Ankit,
I have tried changing the code.. this time i am not getting error.. but my field is not getting updated....
Please have a look at the below code...
trigger UpdateEmployeeinRR on Proposal__c (after insert,after update)
{
List<RR__c> rrlist = new List<RR__c>();
List<Proposal__c> proposallist = new List<Proposal__c>();
List<ID> masterIds = new List<ID>();
List<ID> childDetailMap = new List<ID>();
for(Proposal__c c: Trigger.new)
{
masterIds.add(c.Id);
childDetailMap.add(c.Proposal_RR__c);
}
rrlist= [select id, Selected_Employee__c from RR__c where id in :masterIds];
for(RR__c rr: rrlist)
{
List<ID> rrIds = new List<ID>();
rrIds.add(rr.id);
proposallist = [select id, Proposal_RR__c, RMG_Employee_Code__c from Proposal__c where ID in :rrIds];
for(Proposal__c proposal: proposallist)
{
if(proposal.Status__c == 'Selected-New')
{
rr.Selected_Employee__c = proposal.RMG_Employee_Code__c;
}
}
Update rrlist;
}
}
You this code is not correct, you are using a SOQL in for loop
i have changed my code.. and now i am able to update my master record as well... :)
but when i have multiple records to update.. i get an error Error: Too many SOQL queries: 101..
please help how can i bulkify my trigger...
trigger UpdateEmployeeinRR on Proposal__c (after insert, after update)
{
List<id> rrIds = new List<id>();
for (Proposal__c p : Trigger.new)
{
rrIds.add(p.Proposal_RR__c);
}
List<RR__c> rrs = [Select Id, Selected_Employee__c from RR__c Where Id in :rrIds];
Map<id, RR__c> rrsMap = new Map<id, RR__c>();
for(RR__c a : rrs)
{
rrsMap.put(a.Id,a);
}
for (Proposal__c p : Trigger.new)
{
RR__c thisRr = rrsMap.get(p.Proposal_RR__c);
If (p.Status__c == 'Selected-New')
{
thisRr.Selected_Employee__c = p.RMG_Employee_Code__c;
}
}
update rrs;
}
Now your code for this trigger is fine and it can not give you Too Many SOQL error. You are updating rrlist in this trigger, you must be having trigger on RR__c object as well so they will get fired. Could you confirm whether you have trigger on RR__c . If yes then chek those triggers are written for bulk handling. For more on complex situations for triggers you can see :
http://forceschool.blogspot.com/2011/05/writing-apex-trigger-issues-and_25.html
http://forceschool.blogspot.com/2011/05/writing-apex-trigger-issues-and_24.html
http://forceschool.blogspot.com/2011/05/writing-apex-trigger-issues-and.html
Please let me know if any issues in it.
Check in debug, for which code and at which line number it's throwing that error.