Our Salesforce users are deleting Events from Salesforce due to our integration with Outlook. When a Meeting from Outlook is deleted, we want the corresponding Event record in Salesforce to be not deleted but change the status field (a custom field on Activity) to be marked as "Cancelled" instead. Is it possible to not delete the Event from delete trigger?
2 Answers
You cannot convert the delete
event into an update
. Once the DML operation has been started, you can block it (and potentially roll back the transaction) by adding an error on the record. That's unlikely to be what you want to do here.
Instead, I'd suggest you build your delete
trigger to create a new Event (or Task), copying fields from the deleted recording and applying the transformation you mention to mark the event as cancelled. You can insert new Event records from the delete trigger.
-
Why not let the event delete and then undelete it in future method? Commented Nov 27, 2018 at 21:03
-
@PranayJaiswal (1) I didn't think of it (clever idea!); (2) do you need to worry about bulk data processes that would blow out the Recycle Bin storage? Commented Nov 27, 2018 at 21:04
-
2If deleting from outlook its still one delete at a time, even its bulk, the trigger handles it. The only blocker is, if its cascade delete , then delete triggers are not fired. Commented Nov 27, 2018 at 21:06
Yes you can do it using future / queuable APEX.
In the before delete trigger call the Future method which will undelete your event.
trigger EventTrigger on Event (before delete) {
MyEventUndeleterFutureClass.undeletEvent(JSON.serialize(Trigger.old));
}
Then future class
public class MyEventUnDeleterFutureClass {
@Future
public static void undeletEvent(String eventJson){
List<Event> EventList =(List<Event> ) JSON.deserialize(eventJson, List<Event> .class);
System.debug(EventList);
undelete EventList;
for(Event even : EventList ){
even.subject = even.subject+'Cancelled';
}
Update EventList;
}
}
-
thank you for the response. I will update here in case of issues. Commented Nov 28, 2018 at 2:14