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
Tony GarandTony Garand 

Apex CPU limit exceeded when running apex code

Hello Developer Community!

I was running this code

List<contact> contactsToChangeOwner = new List<contact>();
for(contact c:[SELECT Id, AccountId, Ownerid, Account.ownerid from contact where  AccountId != null ]){
 
    if(c.ownerid!=c.Account.ownerid){
        c.ownerid = c.Account.ownerid;
        contactsToChangeOwner.add(c);
    }
}

update contactsToChangeOwner;

for 7,780 records to change the contact owners to match the Account Owners and I recieved this error "Apex CPU limit exceeded" what changes can I make to this code to fix this? A map?
Amit Chaudhary 8Amit Chaudhary 8
Can you please post your full code. This kind of issue used to come when you are using for loop inside the for loop.

 
Tony GarandTony Garand
That is my full code Amlt Chaudhary!
Amit Chaudhary 8Amit Chaudhary 8
are you running below code from develor console ?
List<contact> contactsToChangeOwner = new List<contact>();
for(contact c:[SELECT Id, AccountId, Ownerid, Account.ownerid from contact where  AccountId != null ]){
 
    if(c.ownerid!=c.Account.ownerid){
        c.ownerid = c.Account.ownerid;
        contactsToChangeOwner.add(c);
    }
}

Do you have any Trigger on contact as well ?
Can you please share screen shot of error
 
Tony GarandTony Garand
I ran the code through the Execute Anonymous Window in the developer console.

User-added image

There is the error and log screen.



 
Amit Chaudhary 8Amit Chaudhary 8
Can you please try to write Batch job. You will get more limit.
global class RegisterJob implements Database.Batchable<sObject> {

    global Database.QueryLocator start(Database.BatchableContext BC){
        String profileName = Label.CustomerCommunityProfile;
        String query = 'SELECT Id, AccountId, Ownerid, Account.ownerid from contact where  AccountId != null';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Contact> scope){
        List<contact> contactsToChangeOwner = new List<contact>();
        for(Contact cnt : scope)
        {
			if(c.ownerid!=c.Account.ownerid){
				c.ownerid = c.Account.ownerid;
				contactsToChangeOwner.add(c);
			}
		}
		if(contactsToChangeOwner.size() > 0 )
		{
			update contactsToChangeOwner;
		}
    }
	
	
    global void finish(Database.BatchableContext BC) {
    }
}
Execute above class with below code
Database.executeBatch(new RegisterJob () );
 
Tony GarandTony Garand
So I would run this in the Execute Anonymous Window?

global class RegisterJob implements Database.Batchable<sObject> {

    global Database.QueryLocator start(Database.BatchableContext BC){
        String profileName = Label.CustomerCommunityProfile;
        String query = 'SELECT Id, AccountId, Ownerid, Account.ownerid from contact where  AccountId != null';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Contact> scope){
        List<contact> contactsToChangeOwner = new List<contact>();
        for(Contact cnt : scope)
        {
            if(c.ownerid!=c.Account.ownerid){
                c.ownerid = c.Account.ownerid;
                contactsToChangeOwner.add(c);
            }
        }
        if(contactsToChangeOwner.size() > 0 )
        {
            update contactsToChangeOwner;
        }
    }
    
    
    global void finish(Database.BatchableContext BC) {
    }
}

Database.executeBatch(new RegisterJob () );
Amit Chaudhary 8Amit Chaudhary 8
Save the above class in salesforce then try to execute below code form Execute Anonymous Window
 
Database.executeBatch(new RegisterJob () );

Let us know if this will help you