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
Gabe Rothman 8Gabe Rothman 8 

Can anyone help me optimize this class? (needs to perform better in bulk processing)

I have been trying to do some mass account owner updates (approx 2000 records) and I'm running into processing time-limit errors.  I'm pretty sure the following class is the prime offender, but as someone who is not a full-time developer, I'm not certain where I can optimize it.  Thanks in advance for help!
public class syncContactOwnerWithAccount {

    public static list<Account> filterAcc(map<id, Account> oldMap, list<Account> newList) {
        list<Account> temp=new list<Account>();
        for (Account acc : newList){
            if (oldMap.get(acc.id).OwnerId != acc.OwnerId){
                temp.add(acc);
            }
        }
        return temp;
    }
    public static void updateContacts(List<Account> Accounts) {
    	list<Account> alist = new list <Account>();
        for (Account a2 : Accounts){
            alist.add(a2);
        }
        list<Contact> clist = [SELECT AccountId, Account.OwnerId
                               FROM Contact
                               WHERE AccountId in: alist];
        list<Contact> updatelist = new list<Contact>();      
        for(Contact c : clist){
            c.OwnerId = c.Account.OwnerId;
            updatelist.add(c);
        }
        update updatelist;
    }        
    
}