The user can select a group of Contacts
. At that point we create a single new Event
, as well as, an EventRelation
for each Contact
(marked as an Invitee
)
That is done through a controller extension (see code below) and seems to generate all the objects perfectly. The problem is I don't know why the Event
doesn't show up in the 'Activity History for the Invitee
.
We have 'shared activities' enabled for the organization and no errors or issues appear in the log.
public with sharing class CreateEvent_Controller
{
public Event eventNew {get; set;}
public List<Contact> selectedContacts { get; set; }
public CreateEvent_Controller(ApexPages.StandardSetController controller)
{
selectedContacts = [select id, Name from Contact where id IN :((List<Contact>)controller.getSelected())];
eventNew = new Event();
eventNew.OwnerId = UserInfo.getUserId(); //Assigned To
eventNew.GroupEventType = 2;
eventNew.IsAllDayEvent = true;
eventNew.IsGroupEvent = true;
if(selectedContacts.isEmpty() == true)
{
ApexPages.Message noContactsSelected = new ApexPages.Message(ApexPages.Severity.INFO, 'No Contact Records Selected: Press The Cancel Button To Return To The Contacts Page To Select Contacts');
ApexPages.addMessage(noContactsSelected);
}
}
public PageReference SaveEvent()
{
Database.saveresult eventInsertResult = database.insert(eventNew);
if(eventInsertResult.isSuccess())
{
List<EventRelation> contactEventLinks = new List<EventRelation>();
for(Contact ct: selectedContacts)
{
EventRelation newLink = new EventRelation(EventId=eventNew.ID, RelationId=ct.ID, Status='Accepted', IsInvitee=true);
contactEventLinks.add(newLink);
}
if (contactEventLinks.size() > 0)
{
try
{
insert contactEventLinks;
}
catch (Exception e)
{
delete eventNew;
}
}
}
PageReference pageRef = new PageReference(' HIDDEN FOR DEMO ');
pageRef.setRedirect(true);
return pageRef;
}
}