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
JaimeBidJaimeBid 

Schedulable Apex class

Hi everyone!
I am a developer beginner. I have created the following class to convert all contacts into person accounts. To avoid interfere with our current implementations it will be a scheduled class.

This is one of the first classes I write, so I am still a bit lost. 
The error is "Variable does not exist" in both lines 30 and 37

My question is about the approach, being schedule class, shall I create different methods to keep on returning the values (to be reused) or shall I create a map to store everything and reuse it?
Any other approach you may consider?

Thanks a lot for your ideas... 
 
global class ConvertContactToPersonAccount Implements Schedulable{    
    public static final Id bizAccRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Cuenta_empresa').getRecordTypeId();
    public static final Id personAccRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('PersonAccount').getRecordTypeId();
    
    global void execute(SchedulableContext sc){
        ConvertContactToPersonAccount();
    }
    public void ConvertContactToPersonAccount() {
        // Query of all contacts not related to business or person account
        List <Contact> contactsToConvert = [SELECT Id, FirstName, Lastname, Birthdate, OwnerID, Email 
                                            FROM contact 
                                            WHERE IsPersonAccount = false 
                                            AND Account.Name = null];
        if (contactsToConvert.size() > 0){
            // Loop the list and generate the BIZ accounts
            List <Account> NewAccBiz = new List <Account>();
            for (Contact con : contactsToConvert){
                Account acc = new Account ();
                Acc.OwnerID = con.OwnerID;
                acc.Name = con.Firstname + ' ' + con.LastName;
                acc.RecordTypeId = bizAccRecordTypeId;
                acc.Fecha_de_nacimiento__c = con.Birthdate;
                NewAccBiz.add(acc);
            }
            Database.insert (NewAccBiz, false);    
            
            // Update the contacts with the data from the new BIZ acc cretaed
            List <Contact> associateCon = new List <Contact>();
            for (Contact con : contactsToConvert){            
                con.AccountId = acc.Id;
                associateCon.add(con);
            }
            Database.update (associateCon, false);  
            
            // Modify BIZ acc into Person Accounts
            List <Account> accBizToPaList = new List <Account>();                        
            List <Account> acctoUpdate = [SELECT Id FROM account WHERE ID = :acc.Id];
            for (Account accBizToPa : acctoUpdate){
                accBizToPa.RecordTypeId = personAccRecordTypeId;
                accBizToPaList.add(accBizToPa);
            }
            Database.update (accBizToPaList, false);             
        }
    }
}