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
Jonas_24056Jonas_24056 

Record Update hits Apex CPU time limit

I am pretty new to Apex and I am trying to get the hang of it, so please bear with me. 

I simply want to update the Owner of all Contacts according to speciic conditions. I am using the Execute Anonymous to do this. However, I always hit the "Execute Anonymous Error: System.LimitException: Apex CPU time limit exceeded".

Could someone explain to me what the problem is? I have used the two following ways, both hitting the same issue. 

First attempt:

list<Contact> conList = new list <Contact>();
conList = [SELECT 
           Id,
           Account.Region__c,
           Account.Subregion__c,
           Account.Subdivision__c,
           Country__c,
           OwnerId
           FROM Contact
                          ];

for(Contact con : conList){ 
    if (con.Account.Subregion__c == 'a' && con.Account.Subdivision__c != 'b'){
        con.OwnerId = 'abc'; 
    }
    else if (con.Account.Subregion__c == 'a' && con.Account.Subdivision__c == 'b'){
        con.OwnerId = 'def'; 
    }
    else if (con.Account.Subregion__c == 'c'){
        con.OwnerId = 'ghi';
    }
    else if (con.Account.Subregion__c == 'd'){
        con.OwnerId = 'klm';
    } 
    else if (con.Country__c == 'g' || con.Country__c =='h' ){
        con.OwnerId = 'nop'; 
    }    

//and so on
    
update conList;
        }


Second attempt I tried to split my query into several SOQL queries but also hit the same error:

//abc Contact's 
list<Contact> conAbc = new list <Contact>();
conAbc = [SELECT 
           Id,
           OwnerId
           FROM Contact
           WHERE Account.Subregion__c='c' OR (Account.Subregion__c='a' AND Account.Subdivision__c='b')
                          ];
for(Contact a : conAbc){ 
            a.OwnerId = 'abc';
    update conAbc;
        }

//def Contacts
list<Contact> conDef = new list <Contact>();
conDef = [SELECT
            Id,
            OwnerId
         FROM Contact
         WHERE Account.Subregion__c='a' AND Account.Subdivision__c != 'b'
        ];
for(Contact d : conDef){
    d.OwnerId = 'def';
  update conDef;
    }


//ghi Contacts
list<Contact> conGhi = new list <Contact>();
conGhi = [SELECT
        Id,
        OwnerId
        FROM Contact
        WHERE Account.Subregion__c = 'c'
        ];
for(Contact g : conGhi){
    g.OwnerId = '0051t000004T8KJAA0';
   update conGhi;
}
//and so on

Could anyone explain me what I am doing wrong and help me with my first steps in coding? Thanks a lot!
Sandeep Kumar 2195Sandeep Kumar 2195
Hi @Jonas,
  You should write dml outside of the for-loop. So your code should like this- 
 list<Contact> conList = [SELECT 
           Id,
           Account.Region__c,
           Account.Subregion__c,
           Account.Subdivision__c,
           Country__c,
           OwnerId
           FROM Contact];

for(Contact con : conList){ 
    if (con.Account.Subregion__c == 'a' && con.Account.Subdivision__c != 'b'){
        con.OwnerId = 'abc'; 
    }
    else if (con.Account.Subregion__c == 'a' && con.Account.Subdivision__c == 'b'){
        con.OwnerId = 'def'; 
    }
    else if (con.Account.Subregion__c == 'c'){
        con.OwnerId = 'ghi';
    }
    else if (con.Account.Subregion__c == 'd'){
        con.OwnerId = 'klm';
    } 
    else if (con.Country__c == 'g' || con.Country__c =='h' ){
        con.OwnerId = 'nop'; 
    }    

//and so on
    

}       
update conList;

 
Jonas_24056Jonas_24056
Hi @Sandeep

many thanks for your help. I followed your suggestion, yet, I still hit the same error. Might there be any record limits I have to mind? I first limited to 4000, then to 1000 records, but the error is still the same. It reads: "Line: 79, Column: 1
System.LimitException: Apex CPU time limit exceeded", even though I am only using 30 lines of code for this.