function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
andyaldis1.3969086903835708E12andyaldis1.3969086903835708E12 

Constructor not defined: error when I try to add values to a wrapper class

I am trying to write a class to create a combined list of events and tasks in descending order of created dates.  I need to limit the list to the last 50 records.  I am using a for loop to compare the created dates of tasks and events then trying to assign the record the wrapper class to get the correct order.  When I try to assign the task or activity I get an error that states. "undefinedConstructor not defined: [CTRL_MCContacActivities.activityWrapper].(Event)undefined" and  "undefinedConstructor not defined: [CTRL_MCContacActivities.activityWrapper].(Task)undefined".  I have found mulitple examples and have not been able to find out what is wrong.  My code is below.

public with sharing class CTRL_MCContactActivities {

    private Contact theContact;
    public Map<String, String> params {get;set;}
    public Integer getRetailActivitesCount {get;set;}
    public String conId;
    public List<Activity_Contact__c> activities {set;get;}
    Public List<Task> taskList {get; set;}
    Public List<Event> eventList {get; set;}
    Public integer tc = 0;
    Public integer ec = 0;
    public List<activityWrapper> activityList {get; set;}

    public CTRL_MCContactActivities(ApexPages.StandardController controller) {
        theContact = (Contact) controller.getRecord();
        If (theContact != null) {
          activityList = new List<activityWrapper>();
          taskList = [SELECT Id, Activity_Type__c, CreatedDate, Meeting_Type__c, Subject, ActivityDate, Description, Activity_Status__c, Owner.Name
                      FROM task
                      WHERE Id IN (Select taskId from taskRelation where Relationid = :theContact.Id) order by CreatedDate DESC] ;
          eventList = [SELECT Id, Activity_Type__c, CreatedDate, Meeting_Type__c, Subject, ActivityDate, Description,Activity_Status__c, Owner.Name
                      FROM event
                      WHERE Id IN (Select eventId from eventRelation where Relationid = :theContact.Id) order by CreatedDate DESC] ;
          IF(taskList.size() > 0 && eventList.size() > 0){
            FOR(integer i = 0; i < 50; i++){
              system.debug(i+' ***** task CreatedDate '+taskList[tc].CreatedDate+' event CreatedDate '+eventList[ec].CreatedDate);
              IF(taskList[tc].CreatedDate  > eventList[ec].CreatedDate){
                activityList.add(new activityWrapper(taskList[tc]));
                tc++;
              }else{
                activityList.add(new activityWrapper(eventList[ec]));
                ec++;
              }
            }

    }
  }
//Wrapper Class
    public class activityWrapper{
Task t {get; set;}
Event e {get; set;}

      }


  
Best Answer chosen by andyaldis1.3969086903835708E12
Wilfredo Morillo 20Wilfredo Morillo 20
You need a constructor method in your wrapper class: 
public class activityWrapper{
Task t {get; set;}
Event e {get; set;}
  
    public activityWrapper(Task awTask){

      t = awTask;
    }
    public activityWrapper(event awEvent){
      e = awEvent;
    }
  }

Ref: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_constructors.htm

All Answers

Wilfredo Morillo 20Wilfredo Morillo 20
You need a constructor method in your wrapper class: 
public class activityWrapper{
Task t {get; set;}
Event e {get; set;}
  
    public activityWrapper(Task awTask){

      t = awTask;
    }
    public activityWrapper(event awEvent){
      e = awEvent;
    }
  }

Ref: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_constructors.htm
This was selected as the best answer
andyaldis1.3969086903835708E12andyaldis1.3969086903835708E12
That got rid of the error but it is only adding a single record, then giving me a null pointer exception.  How do I create a list to add to the wrapper class so I can diplay a combined list on a vf page.
Wilfredo Morillo 20Wilfredo Morillo 20
Remove this line from inside the IF(tehContact1=null){}
activityList = new List<activityWrapper>();
you already declared it outside the constructor. 

wil,