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
vinni shreevinni shree 

Clone Event along with Event Relations

Hi I'm trying to clone Event of one custom object along with event relations.could someone please help me with the code, I'm able to clone event along with nly one contact(relationid) but culdn't clone all the contacts please suggest 
 
SwethaSwetha (Salesforce Developers) 
HI Vinni,

To clone all the contacts related to the event, you can modify the code to loop through all the EventRelation records related to the original Event and create copies of them for the cloned Event.

Here is an example code snippet :
// Get the original event and its related records
Event originalEvent = [SELECT Id, Subject, StartDateTime, EndDateTime, Location FROM Event WHERE Id = :originalEventId];
List<EventRelation> originalEventRelations = [SELECT Id, RelationId, IsParent FROM EventRelation WHERE EventId = :originalEventId];

// Create a new event and copy the original event fields
Event clonedEvent = originalEvent.clone(false, true);
clonedEvent.Subject = 'Cloned ' + originalEvent.Subject;

// Insert the cloned event
insert clonedEvent;

// Loop through the original event relations and create copies for the cloned event
List<EventRelation> clonedEventRelations = new List<EventRelation>();
for (EventRelation originalEventRelation : originalEventRelations) {
    EventRelation clonedEventRelation = originalEventRelation.clone(false, true);
    clonedEventRelation.EventId = clonedEvent.Id;
    clonedEventRelations.add(clonedEventRelation);
}

// Insert the cloned event relations
insert clonedEventRelations;

Related:
https://developer.salesforce.com/forums/?id=9062I000000DN7iQAG
https://help.salesforce.com/s/articleView?id=sf.basics_clone_records.htm&type=5
https://help.salesforce.com/s/articleView?id=sf.campaigns_clone_with_related.htm&type=5
https://techdicer.com/clone-record-by-apex-in-salesforce/
https://salesforce.stackexchange.com/questions/133825/copy-the-list-of-invitees-between-two-events-through-apex

If this information helps, please mark the answer as best. Thank you
vinni shreevinni shree
Hi Swetha,
This way It is throwing duplicate error when event Relation has multiple relationid, I want to insert multiple relationid for each event relation.Please help. Thank you