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
AnnaTAnnaT 

System.LimitException: Too many code statements: 200001

When I use apex dataloader to insert multiple data, the following error occurres.

"System.LimitException: Too many code statements: 200001"

Is it because I use List ?

trigger RFTSetPDStatus on RFT_Category__c (before insert, before update) {
 List<Ichthys_RFT__c > msList= [SELECT RFT_Category__r.Id,Status__c FROM Ichthys_RFT__c ];
  if (Trigger.isInsert)
  { 
    for(RFT_Category__c pd:Trigger.new)
    {
        pd.PD_Status__c='Open';      
    }
  }
  else if (Trigger.isUpdate)
  {
    Integer msExist=0;
    Integer msCnt=0;  
    for(RFT_Category__c pd:Trigger.new)
    {
      for( Ichthys_RFT__c ms :msList){
        if( ms.RFT_Category__r.Id == pd.Id ){
          msExist +=1;
        }
      }
      if(msExist==0)
      {
        pd.PD_Status__c ='Open';
      }
      else
      {
        for( Ichthys_RFT__c ms :msList){
          if( ms.RFT_Category__r.Id == pd.Id && ms.Status__c <>'Completed' &&  ms.Status__c <>'Not Applicable'){
            msCnt +=1;
          }
        }
          if(msCnt==0)
          {
            pd.PD_Status__c ='Close';
          }
          else if(msCnt>0)
          {
            pd.PD_Status__c ='Open';
          }
        }
    }
  }
}

Thank you,

Anna

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
You are querying the entire database each time you do an upload. This will cause all sorts of problems. Make sure the filter on the list is only for current records, not the entire database.

All Answers

sfdcfoxsfdcfox
You are querying the entire database each time you do an upload. This will cause all sorts of problems. Make sure the filter on the list is only for current records, not the entire database.
This was selected as the best answer
AnnaTAnnaT
Hello again sfdcfox,
Thank you very much for your quick reply!!
With your support, I could update multiple data finally!
I really appreciate your support.
AnnaTAnnaT

I'm sorry but could you help me again?

I'm trying to modify another trigger because of the same issue.

But I don't know how to filter the SOQL in this case...

Especially the following code,

" WHERE Object_No__c in :Trigger.new.Objective_No__c "

 

trigger RFT_ObjectChampion on Ichthys_RFT__c (before insert, before update) {
    List<Object_Champion__c > ocList= [SELECT Champion_Name__c,Location__c,Champion_email__c,Object_No__c,Champion_Contact_No__c 
FROM Object_Champion__c WHERE Object_No__c in :Trigger.new.Objective_No__c ];
    for (Ichthys_RFT__c rft : Trigger.new) {
        for( Object_Champion__c oc :ocList)
        {
            if( oc.Object_No__c == rft.Objective_No__c && oc.Location__c ==rft.Location__c )
            {
                rft.Champion_Name__c = oc.Champion_Name__c;
                rft.Champion_email__c = oc.Champion_email__c;
                rft.Champion_Contact_No__c= oc.Champion_Contact_No__c;
                break;
            }            
            else{
                rft.Champion_Name__c = '';
                rft.Champion_email__c = '';
                rft.Champion_Contact_No__c= '';
            }

       }
   }
}

 Thanks in advance for your help...

Anna

 

sfdcfoxsfdcfox

You can't do a query that way. You'll have to follow the aggregate-query-update pattern:

 

Aggregate: Collect the values you need to query against.

Query: Query all the matching records.

Update: Operate on the queried data.

 

Here's how I might implement your trigger using this method:

 

trigger RFT_ObjectChampion on Ichthys_RFT__c (before insert, before update) {
  Map<String,Map<String,Object_Champion__c>> obChamps =
    new Map<String,Map<String,Object_Champion__c>>();
  Set<String> locations = new Set<String>(), objNos = new Set<String>();
  for(Ichthys_RFT__c record:trigger.new) {
    locations.add(record.Location__c);
    objNos.add(record.Objective_No__c);
  }
Set<String> emptyStrings = new Set<String>{null,''};
locations.removeAll(emptyStrings);
objNos.removeAll(emptyStrings); for(Object_Champion__c record:[SELECT Id, Champion_Name__c, Location, Champion_Email__c, Object_No__c, Champion_Contact_No__c FROM Object_Champion__c WHERE Location__c in :locations AND Object_No__c IN :objNos]) { if(!obChamps.containsKey(record.Object_No__c)) { obChamps.put(record.Object_No__c,new Map<String,Object_Champion__c>()); } obChamps.get(record.Object_No__c).put(record.Location__c,record); } for(Ichthys_RFT__c record:Trigger.new) { if(obChamps.containsKey(record.Objective_No__c) && obChamps.get(record.Objective_No__c).containsKey(record.Location__c)) { Object_Champion__c tmp = obChamps.get(record.Objective_No__c).get(record.Location__c); record.Champion_Name__c = tmp.Champion_Name__c; record.Champion_Email_-c = tmp.Champion_Email__c; record.Champion_Contact_No__c = tmp.Champion_Contact_No__c; } else { record.Champion_Name__c = record.Champion_Email__c = record.Champion_Contact_No__c = ''; } } }

You'll want to read more about using Set and Map data types.

AnnaTAnnaT

Thank you very much for your kind support!!

It works with no error !

 

I need to study about Map...