Answered by:
Plug-in That blocks Delete and Pops Message

Question
-
BACKGROUND
I am writing a plug-in that needs to fire when a record deletion is attempted and a IsPrimary (Boolean) field on the record is has a true value, I need to stop the delete from happening and pop a messge informing the user: "Cannot delete Primary Record."
QUESTIONS
This involves two things that I have not done in a plug-in before: (1) killing the transaction, and (2) Displaying a user message.
I can't find any examples of either of these actions for a plug-in. I know to kill the tranaction I will need to handle a pre-operation message (pre-validation or pre-operation?) but I'm not sure waht action to take to make sure the tranaction is killed. On the message, since we are using CRM Online, I assume there would have to be some special way of handling this since it is a server-side event that is being handled.
Any help and/or references to examples implementing these actions would be appreciated.
Monday, June 18, 2012 7:02 PM
Answers
-
HI Kahuna2000,
You can stop the execution and displaying an error to the user by simply throwing an exception from your plugin as follows
throw new ApplicationException("Cannot delete Primary Record.");
Regards,
Damian Sinay- Marked as answer by Andrii ButenkoMVP, Moderator Monday, June 18, 2012 8:42 PM
Monday, June 18, 2012 7:51 PM
All replies
-
HI Kahuna2000,
You can stop the execution and displaying an error to the user by simply throwing an exception from your plugin as follows
throw new ApplicationException("Cannot delete Primary Record.");
Regards,
Damian Sinay- Marked as answer by Andrii ButenkoMVP, Moderator Monday, June 18, 2012 8:42 PM
Monday, June 18, 2012 7:51 PM -
Thanks Damian. This should work.
Out curiosity, is there a way to display messages from within a plug-in that may be unrelated to an exception condition?
Monday, June 18, 2012 9:17 PM -
Hi Kahuna,
As of the latest CRM 2011 rollup updates, the only way to display a message from a plugin to the CRM website is by throwing Exception.
This is one area that is a bit lacking in CRM and I'm sure Microsoft will look into extending this further in the future.
Hope this helps.
Dimaz Pramudya | CSG (Melbourne) | http://www.xrmbits.com | dimaz@xrmbits.com If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"
- Proposed as answer by Dimaz Pramudya (www.xrmbits.com) Monday, June 18, 2012 11:11 PM
Monday, June 18, 2012 11:10 PM -
Not what I wanted to hear, but appreciate receiving the info. from one who knows. Thanks much.Tuesday, June 19, 2012 12:28 AM