0

I'm fairly new to development but I'm currently trying to move some triggers into classes to follow the one trigger per object design pattern. I'm getting the following error in the trigger:

Constructor not defined: [Event_CountEvents].(List, List)

The constructor is defined in the class with the exact name and arguments so not sure why I'm getting it, any insight?

Trigger:

trigger MasterEventTrigger on Event (
  before insert, after insert, 
  before update, after update, 
  before delete, after delete) {

  if (Trigger.isBefore) {
    if (Trigger.isInsert) {
      Event_CountEvents countUp = new Event_CountEvents(Trigger.new, Trigger.old);
      countUp.eventCounter();
    } 
    if (Trigger.isUpdate) {
    //Call class logic here
    }
    if (Trigger.isDelete) {
      // Call class logic here!
    }
  }

  if (Trigger.IsAfter) {
    if (Trigger.isInsert) {
      // Call class logic here!
    } 
    if (Trigger.isUpdate) {
      // Call class logic here!
    }
    if (Trigger.isDelete) {
      // Call class logic here!
    }
  }
}

Class:

public class Event_CountEvents {

    List<Event> eventsNew = new List<Event>();
    List<Event> eventsOld = new List<Event>();

    public void Event_CountEvents (List<Event> newEvents, List<Event> oldEvents){
        eventsNew = newEvents;
        eventsOld = oldEvents;      
    }


    public void eventCounter (){
        Set<Id> leadId = new Set<Id>();
        List<Lead> leadsToUpdate = new List<Lead>();

        if(Trigger.IsInsert || Trigger.IsUndelete){
            for(Event e: eventsNew) {
                if(String.valueOf(e.WhoId).startsWith('00Q'))
                leadId.add(e.WhoId);

            }

        }

        if(Trigger.IsDelete){
        for(Event e: eventsOld) {
            if(String.valueOf(e.WhoId).startsWith('00Q'))
            leadId.add(e.WhoId);

            }

        }

        if(leadId.size()>0){    

            for(Lead l : [SELECT l.Id, l.Number_of_Events__c,(SELECT Id FROM Events) FROM Lead l WHERE Id in : leadId])
            {
            leadsToUpdate.add(new Lead(Id = l.Id, Number_of_Events__c = l.Events.size()));

            }

        }
        update leadsToUpdate;
        }
}
2

Only regular methods have a "return type," while constructors do not. In other words, your constructor is being interpreted as a regular method:

public void Event_CountEvents (List<Event> newEvents, List<Event> oldEvents){

The "void" return type means it doesn't return a value, but this is still distinct from a constructor, which has no return type at all.

To make it a constructor, do not specify a return type:

public Event_CountEvents (List<Event> newEvents, List<Event> oldEvents){
  • That did it and I had no idea that void was still treated as having a return type. Thanks for teaching me something, sure that will come in handy in the future. – J. Rodden Oct 10 '18 at 10:55

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.