+ Start a Discussion
AnicheAniche 

Avoid Deletion of accounts through triggers

Can I prevent deletion of an account that comes through our ERP but allow prospects to get deleted...  ?
I know i can override the button but i not very familiar with S-Controls.
 
I can distinguish between them though the prospect flag but dont know how to alert saying you cannot delete a account which is not propsect and there by make my trigger to work such that it avoids the deletion
 
Thanks in advance.
mikefmikef
So you are asking for an answer to two very different solutions.

One is avoid deletion through and S-Control.
The other and more of what I think you mean is avoid deletion through an account trigger.

Both will get the the job done but they do it in different ways.

With the S-Control way you will have to override the delete button on accounts with an S-Control that has simple logic to check if the account is a prospect.
Then show the user a message if the account can not be deleted, then bring the user back to the details page of the account.

With the trigger you can use Trigger.isDelete to check if a user is deleting the account. Then in the trigger check to see if the account is a prospect.
Then show the user the Apex error message, just like the field validation error message.
It is up to the user to cancel out of this.

Hope this helps.
AnicheAniche

I am capturing Isdelete event.. and i can check for prospect but.. what should i do the display the error on the screen?

If(Trigger.IsDelete)

{

// The Delete Trigger will Only be triggered from the GUI when a user clicks on Delete

System.debug('Its here');

Account[] oldval = Trigger.old;

If (oldval[0].Prospect__C = True)

{

System.Debug('Test');

}

This is my Isdelete part of my trigger.

Thanks in advance

 

SuperfellSuperfell
there's an addError method on the trigger new array items that you can call to stop processing of that item, something like
Trigger.new[0].addError("can't delete prospects");
AnicheAniche
Simon to the rescue again!!! :)
 
I used the addError method to prevent the deletion of Accounts.
 
Thanks
 
 
AxxxVAxxxV
I have an additional question related to this thread.

In my case, I would like to prevent a user from deleting a Contact record based on certain logic.
However, instead of outputting an error message, I would like to update a record ownership, and not show the error to the user.

In short, can I prevent deletion without throwing an error?




ASMASM
I would like to override the delete button for Opportunities. The Opportunity should not be deleted if the Stage="Entered" else delete the opportunity. How can a I do this? I am new developer in salesforce. I am writing a S-control for it. But it seems to go to a blank page and does not give the alert which I want it to show.
 
Please help
ASMASM
I have implemented the Trigger and now I am trying to migrate that to the Production Org. It gives me an error Test coverage of selected trigger is 0%. I have unlimited edition of salesforce. I do not know what needs to be done to run the trigger in production org.
 
Thanks.
ASMASM
I have solved the problem. Thanks