You need to sign in to do that
Don't have an account?
LimitException: Too many code statements: 200001
Hi,
I am getting too many code statements:200001 while inserting the Records on the Custom Object.on the custom object one trigger is there.i am calling one class when the criteria is met in the Trigger.This is mmy class and Trigger code..please help on this.
trigger updateToUser on Export_Object__c (after insert,after update)
{
for(Export_Object__c e: Trigger.New)
{
if(e.Alias__c=='test'){
getUnupdatedUsers gu = new getUnupdatedUsers();
gu.mymethod();
}
}
}
class:
public class getUnupdatedUsers
{
List<Export_Object__c> eo_list = new List<Export_Object__c>();
set<String> eoid = new set<String>();
List<User> user_list = new List<User>();
List<User> updatelist = new List<user>();
List<User> updatefailedlist = new List<User>();
public void mymethod()
{
eo_list = [select id,Alias__c,City__c,Company__c,Country_Region__c,Department__c,FirstName__c,Mobile__c,state__c,Address__c,Telephone__c,Title__c from Export_Object__c];
for(Export_Object__c e: eo_list)
{
if(e.Alias__c!=null)
eoid.add(e.Alias__c);
}
user_list = [select id,adid__c from user where adid__c in: eoid];
for(Export_Object__c e: eo_list)
{
for(User u:User_List)
{
if(u.ADID__c == e.Alias__c)
{
if(e.City__c!=null)
u.City = e.City__c;
if(e.Company__c!=null)
u.CompanyName = e.Company__c;
if(e.Country_Region__c!=null)
u.Country = e.Country_Region__c;
if(e.Department__c!=null)
u.Department = e.Department__c;
if(e.FirstName__c!=null)
u.FirstName = e.FirstName__c;
if(e.Mobile__c!=null)
u.MobilePhone = e.Mobile__c;
if(e.state__c!=null)
u.State = e.state__c;
if(e.Address__c!=null)
u.Street = e.Address__c;
if(e.Telephone__c!=null)
u.phone = e.Telephone__c;
if(e.Title__c!=null)
u.Title = e.Title__c;
//u.available__c = true;
updatelist.add(u);
}
}
}
update updatelist;
updatefailedlist = [select id,ADID__c,name,email from user where id !=: updatelist];
system.debug('updatefailedlist '+updatefailedlist.size());
if(updatefailedlist.size()>0){
system.debug('mismatchlist size');
mismatchlist();
}
}
public void mismatchlist()
{
string header = 'Name , ADID, Email \n';
String finalstr = header;
for(user eo :updatefailedlist)
{
string recordString = '"'+eo.name+'","'+eo.adid__c+'","'+eo.email+'"\n';
finalstr = finalstr +recordString;
}
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.setFileName('Employee.csv');
attach.Body = Blob.valueOf(finalstr);
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] toAddresses = new list<string> {'work@gmail.com'};
String subject ='Export CSV';
email.setSubject(subject);
email.setToAddresses( toAddresses );
email.setHtmlBody('Export CSV ');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
}
Hello,
This error is usually caused by a combination of nested loops and large record sets.
Possible Workarounds:
1) @future method - Extract out some of the logic into @future methods so they can execute in a separate context.
2) Move statements out of For Loops - Move statements out of the For Loops so they aren't executed as frequently
3) Reduce/Combine statements - Do a single statement to get multiple values. Use lists to do a single update instead of individual updates.
4) Reduce the number of records processes - Reduce the batch size involved or artificially introduce a batch mechanism.
Troubleshooting:
1) Insert System.debug(Limits.getLimitScriptStatements()); at the beginning of the code section to see the current limit.
2) Insert System.debug(Limits.getScriptStatements()); at strategic areas of the code to identify the problem area. This will usually be just outside of major loops and inside suspect loops.
3) Look for areas where there's large statement usage and focus on clean that area up.
In your code I found that you are using DML and Soql in for loop. Which is bad parcities. So I think that why you are facing problem.
Thanks
Ashlekh
Can any one help on this how to use Map to reduce iterations for the above
code.Have no idea to use Map. Please help on this
I saw another post that states the exact same thing as you do. "placing SOQL inside a loop is bad practice". Even though it is a bad practice, what is it exactly that breaks it? Does Salesforce track this and prevent SOQL to be executed inside a loop?
Thanks.
=============================
Developer for www.voicent.com
Voicent provides CRM, auto dialer, predictive dialer, reminder, and call center solution that is suitable for anyone in any level
Voicent strives to meet customer demands to integrate Voicent software with CRM of their choice.
Hello,
Refer the section 'Using Collections, Streamlining Queries, and Efficient For Loops' in below link which explains with code snippet, how to get the query result in list and optimize For loops:
http://wiki.developerforce.com/page/Apex_Code_Best_Practices