[SalesForce] Problem with executing batch apex class in executing anonymous window

global class AccountkeepingBatch implements Database.Batchable<Sobject>, Database.stateful {

    global String query;
    global Keeping__c pkRecord;
    global Integer numRecords;

    global AccountkeepingBatch(Keeping__c pk) {
        this.pkRecord = pk;
        System.debug('pk -->' + pk);
        this.numRecords = 0;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {

        Id BusinessAccount = getRecordTypeIdByDeveloperName(Utils.BUSINESSACCOUNT_READONLY_RT);
        Id PersonAccount = getRecordTypeIdByDeveloperName(Utils.PERSONACCOUNT_READONLY_RT);
        return Database.getQueryLocator('SELECT Id,IsPersonAccount,RecordTypeId FROM Account WHERE LastModifiedDate < ' + pkRecord.Job_Start_Date_Time__c.formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'') +
                'AND RecordTypeID IN (\'' + BusinessAccount + '\', \'' + PersonAccount + '\')');
    }

    global void execute(Database.BatchableContext bc, List<Account> scope) {
        this.numRecords += scope.size();
        System.debug('scope -->' + scope);

        Id deactivatedBusinessAccount = getRecordTypeIdByDeveloperName(Utils.BUSINESSACCOUNT_DEACTIVATED_RT);
        Id deactivatedPersonAccount = getRecordTypeIdByDeveloperName(Utils.PERSONACCOUNT_DEACTIVATED_RT);

        System.debug('deactivatedBusinessAccount RT ->' + deactivatedBusinessAccount);
        System.debug('deactivatedPersonAccount RT ->' + deactivatedPersonAccount);

        DateTime deactivationTime = System.now();
        for (Account acc : scope) {
            System.debug('acc -->' + acc);
            acc.IsDeactivated__c = true;
            acc.DeactivatedDateTime__c = deactivationTime;
            if (acc.IsPersonAccount) {
                acc.RecordTypeId = deactivatedPersonAccount;
            } else {
                acc.RecordTypeId = deactivatedBusinessAccount;
            }
        }
        update scope;
    }

    global void finish(Database.BatchableContext bc) {

    }

    private Id getRecordTypeIdByDeveloperName(String name){
        return Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get(name).getRecordTypeId();
    }
}

I try to run the batch class in the execute anonymous window

Database.executeBatch(new AccountkeepingBatch(Keeping__c pk),200);

I get the error as

Line: 1, Column: 22 Unexpected token '('.

Could you please tell me what I did wrong here?

Best Answer

You need to pass an instance of Keeping__c into the constructor:

Keeping__c pk = new Keeping__c(...);
Database.executeBatch(new AccountkeepingBatch(pk), 200);

The argument type (Keeping__c here) is not supplied when invoking a method or constructor.

Related Topic