You need to sign in to do that
Don't have an account?
Tony 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?
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?
Do you have any Trigger on contact as well ?
Can you please share screen shot of error
There is the error and log screen.
Database.executeBatch(new RegisterJob () );
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 () );
Let us know if this will help you