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
Olivia Porter 1Olivia Porter 1 

Comparison arguments must be compatible types: List, Date

I am trying to compare the Campaign.StartDate to today's date. Because they are of different Types, APEX won't let me do this. Is there a way around this? Is it possible to convert a Date type into a CampaignMember type? Or vise versa? Please see below code:

public class RecordIdPass {
    Date val= Date.today();
    List<CampaignMember> StartDateTest = [SELECT Campaign.StartDate FROM CampaignMember WHERE LeadId = :leadId];
    
    public RecordIdPass() {
         //error- cannot compare two different Types of list objects
        if (StartDateTest != val) {    
            List<CampaignMember> cms= [SELECT CampaignId, LeadId, Status, Campaign.StartDate FROM CampaignMember WHERE LeadId = :leadId ORDER BY Campaign.StartDate DESC limit 1];
        }
    }
}
deepak balur 19deepak balur 19
Olivia:
CampaignMember CM = [SELECT Campaign.StartDate FROM CampaignMember WHERE LeadId = :leadId LIMIT 1];
Date StartDateTest =CM.StartDate;
Olivia Porter 1Olivia Porter 1
When I change that I get an error "Illegal assignment from List<CampaignMember> to Date". It only seems to want to accept <CampaignMember> as the type.

I have tried to convert the "List<CampaignMember> StartDateTest" into 'Date' a number of different ways but no avail. Which I find strange because the Campaign.StartDate is of type Date. 
Olivia Porter 1Olivia Porter 1
Update: I resolved it within the query:

public class RecordIdPass {
    public RecordIdPass() {  
            List<CampaignMember> cms= [SELECT CampaignId, LeadId, Status, Campaign.StartDate FROM CampaignMember WHERE LeadId = :leadId and Campaign.StartDate < TODAY ORDER BY Campaign.StartDate DESC limit 1];
    }
}