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);



            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:

public class Teste_Atualizar_Aging_OS_Batch {

    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() {
        Atualizar_Aging_OS_Batch aaob = new Atualizar_Aging_OS_Batch();
        Id batchId = Database.executeBatch(aaob);


Can anyone find out what's wrong with my code?

  • 1
    Note that it is usually clearer to have a hard coded static SOQL query just put in the start method rather than passing in a dynamic SOQL string as a constructor argument. – Keith C May 28 '18 at 19:52

You have a parameterized constructor in batch apex.

To resolve the issue you would have to modify your test class to pass the required query string as a parameter while doing the instantiation as:

Atualizar_Aging_OS_Batch aaob = new Atualizar_Aging_OS_Batch('query string');
| improve this answer | |
  • Thank you Raul. Now my test class is only with 22% of code coverage, Can you tell what else I should test. Sorry to bother. – Juliano Szlachta May 28 '18 at 20:35
  • 2
    please do not post follow up questions in the comments. If you have a new question, pelase open a new post, and if it is related to this question, you might want to incldue what you have tried to increase code coverage. If you are new to Apex, Trailheads might be better suited for you. – glls May 28 '18 at 20:44

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.