我在编写的Batchable和Schedulable Apex类方面遇到了麻烦。基本上,Schedulable类不会调用Batchable类在其中运行代码。这是我写的:
rw_BillingInvoiceCreationSchedulable.apxc
global class rw_BillingInvoiceCreationSchedulable implements Schedulable {
global void execute(SchedulableContext sc) {
System.debug('Batchable queued for execution');
rw_BillingInvoiceCreationBatchable b = new rw_BillingInvoiceCreationBatchable();
Id batchId = Database.executeBatch(b);
System.debug('Batchable was executed: ' + JSON.serialize(b));
}
}
rw_BillingInvoiceCreationBatchable.apxc
global class rw_BillingInvoiceCreationBatchable implements Database.batchable<SObject> {
global Database.QueryLocator start(Database.BatchableContext BC) {
System.debug('Batch Apex called');
Date closingTime = date.today().toStartOfWeek();
return Database.getQueryLocator([SELECT Id, Name, rw_Batch_Sequence__c, rw_End_Date__c, rw_Closed__c, rw_Customer__c, rw_Start_Date__c, rw_Warehouse__c
FROM rw_Invoice_Batch__c
WHERE rw_Closed__c = FALSE]);
// AND rw_End_Date__c < :closingTime
}
global void execute(Database.BatchableContext info, List<rw_Invoice_Batch__c> scope) {
System.debug('Invoices about to be updated');
List<rw_Invoice_Batch__c> invoices = new List<rw_Invoice_Batch__c>();
List<Account> activeCustomers = [SELECT Id, Name
FROM Account
WHERE Inventory_Management__c = TRUE OR Order_Management__c = TRUE];
for(rw_Invoice_Batch__c i : scope) {
i.rw_Closed__c = TRUE;
invoices.add(i);
rw_InvoiceGenerator.generateInvoice(i);
}
update invoices;
System.debug('Invoices updated to closed');
}
global void finish(Database.BatchableContext BC) {
}
}
就测试类而言,我可以确认Batchable类正在运行。我运行了它的测试类,它总是返回成功,但是对于Schedulable类,测试类或实际运行它总是会得到相同的结果:什么都没有。我不确定是否有任何限制或调整可以允许代码运行,但我对它的含义持开放态度。