+ Start a Discussion
Karen RakotonKaren Rakoton 

Send email in an Apex trigger (on CollaborationGroup) before calling addError

Hi everyone,
I want to have a trigger on ColllaborationGroup (Chatter group) that prevent users from deleting a group, and sending them an email for explanation. This subject was already discussed here (https://developer.salesforce.com/forums/?id=906F00000008xNeIAI) five years ago and I read in many articles all over Internet that this is not possible in one trigger, because when addError is called, "all work done in the trigger, including @future method calls, sending email, queueing batch jobs, or performing any DML, is rolled back".
Can anybody have an alternative solution that could work on the object Collaboration group, knowing that we cannot undelete a Collaboration group? The problem of the trigger, fired before delete, is that the error message is not displayed, probably because of the popup appearing when the user clicks on the "Delete" button. So there is no way the user can know why the deletion is not done. That's one of the reasons the client wants to send an email when someone is trying to delete a group.
I also opened the case #11032475 in Salesforce Support for my problem.
Thans a lot,
Karen
Best Answer chosen by Karen Rakoton
pconpcon
Sorry, I completely forgot that you cannot overload the CollaborationGroup with a custom visual force page. Unfortunately, I think you may be out of luck since all of the transactions get rolled back.

All Answers

pconpcon
Unfortunately it's not possible to to do this via apex for the reasons you listed above.  An possible work around for you to do is to make the view / edit page a VisualForce page and override the delete button.  This would allow you to do the logic in your VF controller to see if the group can be deleted and if no, not even submit it for deletion by the trigger.  This would allow you to then send your email as well as add a Page Error and redirect back to the view page.  This Page Error would be displayed and since you are not doing an addError on the object itself there would be not transaction to roll back and your email would be sent out.
Karen RakotonKaren Rakoton
Thanks for your reply pcon, but I'm afraid I can't do any of the things you said. Salesforce doesn't give access to modification of buttons, links and actions for the CollaborationGroup object (Chatter group). I don't know how to access to change the view / edit page for Chatter group in another way than the modification of buttons, links and actions. Even the standard Chatter group layout is not working for Salesforce standard, but only for Salesforce1, as I asked here (https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AkhxIAC).
pconpcon
Sorry, I completely forgot that you cannot overload the CollaborationGroup with a custom visual force page. Unfortunately, I think you may be out of luck since all of the transactions get rolled back.
This was selected as the best answer