+ Start a Discussion
AnnaTAnnaT 

System.LimitException: Too many code statements: 200001

Please help me...

 

When I update over 1000 items at the same time, "System.LimitException: Too many code statements: 200001" error occurs.

I know the meaning of this error but I don't know how to modify my code...

Hereis a part of my Trigger;

if (Trigger.isInsert || Trigger.isUpdate) {
  List<ParentObject__c> pList = new List<ParentObject__c>();
  for (ChildObject__c chld : Trigger.new) {
    ParentObject__c pObj = new ParentObject__c();
    pObj.Id=chld.ParentLookupField__c;
    Boolean flag = false;
    if(pList.size()==0){
      pList.add(pObj);
    }
    for(integer i=0;i<pList.size();i++){
      if(pList[i].id==pObj.Id){
        flag=true;
      }else if(flag==false){
        pList.add(pObj);
      }
    }    
  }
  update pdList; 
}

 

Thanks in advance for your support...

 

Anna

 

Tim BarsottiTim Barsotti

You are iterating over each record in the pList for every record in the Trigger.New list. You could rework your code to avoid this. Something like below should help reduce the total number of lines being called: 

if (Trigger.isInsert || Trigger.isUpdate) {
  Map<Id, String> pMap = new Map<Id, String>();
  List<ParentObject__c> pList = new List<ParentObject__c>();
  for (ChildObject__c chld : Trigger.new) {
    if(pMap.get(chld.ParentLookupField__c) != null) {
		ParentObject__c pObj = new ParentObject__c();
		pObj.Id=chld.ParentLookupField__c;
		pList.add(pObj);
		pMap.put(chld.ParentLookupField__c, 'Found Once Already');
	}
  }
  update pList; 
}