I have an afterInsert trigger on Events. When the Script is finished, I would like to delete the event. I keep getting this error message.

DML statement cannot operate on Trigger.new or Trigger.old

If I cannot do this within Trigger.new, what is the best way to do this? If SOQL is involved, what field am I looking for? event.ID?

1 Answer 1


You cannot operate on records from the trigger context. You can take those same records and perform DML on them if you simply change the reference.

List<Event> nonTriggerEvents = new List<Event>();
for (Event triggerEvent : trigger.new)
    nonTriggerEvents.add(new Event(Id = triggerEvent.Id));
update nonTriggerEvents;

However, in this case you can take a simpler approach and call Database.delete on a List<Id>.

List<Id> eventIdsToDelete = new List<Id>();
for (Event newEvent : trigger.new)
  • well this is interesting as Database.delete documentation doesn't have any signature for passing a list of IDs . Way cool
    – cropredy
    Commented Aug 23, 2016 at 23:28
  • @crop broken linking system ftw... I do see the signature here fwiw.
    – Adrian Larson
    Commented Aug 23, 2016 at 23:28
  • list<sobject>, yes, not list<ID> _ I didn't realize they were interchangeable in this context until I read this answer. I knew it worked for bind expressions in soql
    – cropredy
    Commented Aug 23, 2016 at 23:31
  • @crop No, the signatures are just not all grouped together for some reason, but it's there. Look between deleteImmediate and emptyRecycleBin.
    – Adrian Larson
    Commented Aug 23, 2016 at 23:38
  • 1
    i'm an idiot :-) -- I never read down that far because they got out of alpha order and it was below the fold. I'll file a doc feedback item. The good news is I learned something
    – cropredy
    Commented Aug 23, 2016 at 23:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .