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

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;
}
}
}
}
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
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); } }