0

I'm trying to write a scheduler Apex class to create an Accounting Period every month and am getting an error when compiling

Method does not exist or incorrect signature: void schedule(String, String, AccountingPeriodCreationService) from the type System

AccountingPeriodCreationService.cls

public with sharing class AccountingPeriodCreationService  {

public static List <AcctSeed__Accounting_Period__c> accountingPeriod;
public static Date currentDate;
public static Date firstDayOfCurrentMonth;
public static Date lastDayOfCurrentMonth;

public void createdAPLogic(){
    accountingPeriod = new List <AcctSeed__Accounting_Period__c>();
    currentDate = Date.today();
    firstDayOfCurrentMonth = currentDate.toStartofMonth();
    lastDayOfCurrentMonth = currentDate.addMonths(1).toStartofMonth().addDays(-1);


    List<AcctSeed__Accounting_Period__c> checkAccountingPeriodAlreadyExist = [SELECT Id, AcctSeed__Start_Date__c, AcctSeed__End_Date__c FROM AcctSeed__Accounting_Period__c where AcctSeed__Start_Date__c = :firstDayOfCurrentMonth];

        if(checkAccountingPeriodAlreadyExist.isEmpty()){
            accountingPeriod.add(
                new AcctSeed__Accounting_Period__c(
                Name = 'Name',
                AcctSeed__Status__c = 'Status',
                AcctSeed__Start_Date__c = firstDayOfCurrentMonth,
                AcctSeed__End_Date__c = lastDayOfCurrentMonth
                )
            );
        }
   } 
}

CreateAccountingPeriods.cls

global with sharing class CreateAccountingPeriods implements Schedulable {
/**
 * @description Executes the scheduled Apex job. 
 * @param sc contains the job ID
 */ 

global void execute(SchedulableContext sc) {
    //Non static method cannot be referenced from a static context
     AccountingPeriodCreationService apc = new AccountingPeriodCreationService();
    String sch = '0 1 0 1 1/1 ? *';
    String jobID = system.schedule('Create Accounting Period', sch, apc);
   }


}

With the line below marked up

String jobID = system.schedule('Create Accounting Period', sch, apc);

I've spent the past few hours trying to figure out why the error is there and how to fix it?

2

CreateAccountingPeriods is your scheduled job. You don't need to schedule again. Instead, just call the appropriate method from your AccountingPeriodCreationService class:

global with sharing class CreateAccountingPeriods implements Schedulable {
  global void execute(SchedulableContext sc) {
    new AccountingPeriodCreationService().createdAPLogic();
  }
}

P.S. it looks like you could just make the method static, too, which would simplify your code:

public static void createdAPLogic() { //...

global with sharing class CreateAccountingPeriods implements Schedulable {
  global void execute(SchedulableContext sc) {
    AccountingPeriodCreationService.createdAPLogic();
  }
}
1

Your problem is that you are trying to schedule the class, that can't be scheduled. To schedule a class, it has implements Schedulable interface. So, in your particular case, CreateAccountingPeriods has to be scheduled not the AccountingPeriodCreationService.

Try to invoke createdAPLogic from CreateAccountingPeriods and then schedule the last via "execute anon. windows".


Oh, and there is something else. You are adding AcctSeed__Accounting_Period__c record to list, but it will never be inserted as you are not calling insert operator

0

create new class to make AccountingPeriodCreationService as scheduler.

  global class AccountingPeriodCreationServiceScheduler implements Schedulable {
        global void execute(SchedulableContext ctx) { 
           new AccountingPeriodCreationService().createdAPLogic();
        }    
    }

You can use this scheduler class in apex scheduler to schedule at particular time.

You can test AccountingPeriodCreationServiceScheduler using test class like below.

 @istest
public class AccountingPeriodCreationServiceSchedulerTest {
    @istest

    public static void  testschedule() {

        Test.StartTest();
        AccountingPeriodCreationServiceScheduler sh1 = new AccountingPeriodCreationServiceScheduler();      
         String sch = '0 0 23 * * ?';
                system.schedule('Test check', sch, sh1);
        Test.stopTest();
    }
}
  • Scheduling CreateAccountingPeriods will result in errors, because every time past the first it will try to schedule a job that does not have a unique name. – sfdcfox Dec 12 '18 at 13:36
  • What is error? Can you please post here? – Dhanik Lal Sahni Dec 12 '18 at 13:38
  • 1
    CreateAccountingPeriods is redundant, because all it does is create a new job. However, since the first parameter is a fixed string, this will result in the scheduled job failing every time past the first that it runs. The Scheduler class you presented by itself would be fine. – sfdcfox Dec 12 '18 at 13:42
  • Thank you @sfdcfox. You are right. I have updated it. – Dhanik Lal Sahni Dec 12 '18 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.