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
Paul.FoxPaul.Fox 

How to send event invitation to event owner through apex

I have a list of events that I want to insert. The invitations should just go to the Owner (who is a User). 

When I create an Event through the user interface I can click Save & Send Invitation. Is there a way to replicate that functionality in Apex?

I have a list called EventsToInvite.

 

This is what I've tried:

Database.DMLOptions options = new database.DMLoptions();
options.EmailHeader.TriggerUserEmail = true;
options.EmailHeader.TriggerOtherEmail = true;
Database.insert(EventsToInvite,options);

Temporarily I have it working through a workflow rule and email notification, but I'd like to just use the Salesforce functionality if possible so I don't have to style email templates.

Anupam RastogiAnupam Rastogi
Hi Paul,

For any event for which you wish to send an invitation to a user, you need to create a record in the EventRelation object. The code should look something similar to the below lines.

for (Event ee : EventsToInvite)
{

    EventRelation er = new EventRelation();

    er.EventId = XXX;
//---replace with the Id for the Event that is already created. Like ee.Id
    er.RelationId = XXX;
//---replace with the User Id, to whom the invite needs to be sent. Like ee.OwnerId
    insert er;
}


Thanks
Anupam

PS: If the answer solves your query, please mark it as Best Answer.
Karthikeyan MBKarthikeyan MB

some more input,
When an event is created, the event relation records are created automatically. The IsInvitee field on the event relation object is set to False during the event relation record creation. This needs to be set as True for the parties (in event relation record) to be considered as invitees. This will ensure the IsGroupEvent field on the corresponding event record to be set as True. This ensures that the event is categorized as multi-party event (an indicator in the UI).
You need to fetch the event relation records (for the corresponding event) and the IsInvitee field needs to be set to True. If we use Database.insert with the DML option, still the email are not getting triggered (i believe its a bug).
Alternate option is to delete the event relation records and insert them using the Database.insert and DML option. The invite email gets triggered only on using Database.insert.