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
renuamirenuami 

Help on Too many queries

 

 Hello -

 

My trigger below is causing error during Mass updates 

 

Error:System Exception:Too Many Soql Queries: 21

 

Can someone please guide me how to resolve this error.

 

Thanks in advance

 

trigger Opportunity_Trigger on Opportunity (before insert, before update)
{
for(Opportunity opp : Trigger.new)
{
if(opp.RecordTypeId == '0123000000002GxAAI' || opp.RecordTypeId=='012400000000kcUAAQ')
{
oppList.add(opp);
}
}

if(oppList.size() > 0)
{
Opportunity_Class.Manager_Update(oppList);
}
}

 

 

 

 

 

 

 

public class Opportunity_Class
{

public static void Manager_Update(Opportunity[] opp)
{
for(Opportunity op1: opp)
{
for(User u: [Select mng_name__c from User where Id=:op1.OwnerId])
{
op1.Manager_Name__c=u.mng_name__c;
}
}
}

}

 

 

 

Message Edited by renuami on 11-05-2009 08:52 AM
David VPDavid VP

Don't call your update statements in the for loop.

Read the article linked below to learn why and how to properly 'bulkify' your code :

 

http://wiki.developerforce.com/index.php/Best_Practice:_Bulkify_Your_Code

CaptainObviousCaptainObvious

In addition to bulkifying your trigger, don't hardcode the recordtype ids or you'll encounter problems when deploying the code to production. Instead, try something like this:

//Retrieve Record Types Map<String, Id> rTypes = new Map<String, Id>(); for(RecordType rType :[SELECT id, name FROM RecordType WHERE sObjectType = 'Opportunity']) { rTypes.put(rType.Name, rType.Id); } Id oppRecType1 = rTypes.get('Custom Record Type 1'); Id oppRecType2 = rTypes.get('Custom Record Type 2'); for(Opportunity opp : Trigger.new) { if(opp.RecordTypeId == oppRecType1 || opp.RecordTypeId==oppRecType2){ oppList.add(opp); } }