• Jonas_24056
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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!
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!