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 CannonOlivia Cannon 

Help with: Invalid initial expression type for field Event.ActivityDate Error

Hi,

I'm writing a trigger to create an event whenever a campaign member is created, but I've hit an issue I don't understand.

This is the trigger:

trigger CreateEventforCampaignMember on CampaignMember (after insert) {

    // 1. create a list of events
    List <Event> eventsToInsert = new List <Event>();
    
    User usr = [SELECT Id FROM User WHERE FirstName =: 'Mark' AND LastName =: 'Alger' AND IsActive = TRUE LIMIT 1];
    // This is to ensure that all events belong to Mark.
    
    // 2. for every campaign member being inserted, add to the list of events (this is so that the trigger can handle bulk inserts)
    for (CampaignMember cm : trigger.new) {
        eventsToInsert.add(
                            new Event    (
                                            EndDateTime = cm.End_Date_of_Event__c,
                                            IsPrivate = false,
                                            IsRecurrence = false,
                                            IsReminderSet = false,
                                            OwnerID = usr.Id,
                                            ActivityDate = cm.Date_of_Event__c,
                                            StartDateTime = cm.Date_of_Event__c,
                                            Subject = cm.Subject__c,
                                            WhoID = cm.LeadId
                                        )
        );
    }
    
    // 3. insert the list of events
    insert eventsToInsert;
}

This is the error I get when I try to save the trigger:

Error: Compile Error: Invalid initial expression type for field Event.ActivityDate, expecting: Date at line 18 column 60

Any help would be very much appreciated!
Best Answer chosen by Olivia Cannon
Vishant ShahVishant Shah
Hi

Check the data type of the field cm.Date_of_Event__c if it is datetime, you just need the date part of it, simply use 

cm.Date_of_Event__c.Date();

Ta
Vish

All Answers

Vishant ShahVishant Shah
Hi

Check the data type of the field cm.Date_of_Event__c if it is datetime, you just need the date part of it, simply use 

cm.Date_of_Event__c.Date();

Ta
Vish
This was selected as the best answer
Olivia CannonOlivia Cannon
Brilliant, thank you!