I'm a begginer in programming and Apex. Just wrote a batch class to update the field "Aging__c" in the object WorkOrder. If EndDate is null, the class does today date - StartDate, or EndDate - StartDate when EndDate is not null.
global class Atualizar_Aging_OS_Batch implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful {
global final String query;
global Atualizar_Aging_OS_Batch(String q){
System.debug(LoggingLevel.ERROR, '___ [cls Atualizar_Aging_OS - constrautor()] - Inicio... ');
query = q;
}
/*
Metodo start do batch
*/
global Database.QueryLocator start(Database.BatchableContext BC) {
System.debug(LoggingLevel.ERROR, '___ [cls Atualizar_Aging_OS - start()] - Inicio... ');
return Database.getQueryLocator(query);
}
/*
Metodo execute, roda quando iniciar o Batch
*/
global void execute(Database.BatchableContext BC, List<WorkOrder> scope) {
System.debug(LoggingLevel.ERROR, '___ [cls Atualizar_Aging_OS - execute()] - Inicio... ');
List<WorkOrder> listOrdemServico = new List<WorkOrder>();
try {
for(sObject so : scope){
WorkOrder OS = (WorkOrder) so;
if(OS.EndDate == NULL){
DateTime hojeDateTime = System.now();
Date hoje = date.newinstance(hojeDateTime.year(), hojeDateTime.month(), hojeDateTime.day());
Date dataInicial = date.newinstance(OS.StartDate.year(), OS.StartDate.month(), OS.StartDate.day());
OS.Aging__c = dataInicial.daysBetween(hoje);
} else {
DateTime hojeDateTime = System.now();
Date dataFinal = date.newinstance(OS.EndDate.year(), OS.EndDate.month(), OS.EndDate.day());
Date dataInicial = date.newinstance(OS.StartDate.year(), OS.StartDate.month(), OS.StartDate.day());
OS.Aging__c = dataInicial.daysBetween(dataFinal);
}
listOrdemServico.add(OS);
}
if(listOrdemServico != null && listOrdemServico.size() > 0) update listOrdemServico;
} catch(Exception e){
System.debug(LoggingLevel.ERROR, '___ [cls Atualizar_Aging_OS - execute()] - ERROR: '+e.getMessage());
}
}
global void finish(Database.BatchableContext BC) {}
}
I'm not sure about the testclass since it's just my second testclass I'm writing. I'm getting the Error: Constructor Not Defined here:
@isTest
public class Teste_Atualizar_Aging_OS_Batch {
@testSetup
static void setup() {
WorkOrder wo = new WorkOrder();
wo.Cliente__c = 'testeOS';
wo.Codigo_Externo__c = 1012145;
wo.StartDate = system.now() -1;
wo.EndDate = system.now();
insert wo;
}
static testMethod void testBatch() {
Test.startTest();
Atualizar_Aging_OS_Batch aaob = new Atualizar_Aging_OS_Batch();
Id batchId = Database.executeBatch(aaob);
Test.stopTest();
}
}
Can anyone find out what's wrong with my code?