What is the best practice for the following situation?

We have a checkbox on Account called "Frozen". When a user attempts to delete a contact, related to a frozen account, the deletion is prevented, but it generates a system-styled apex error. I'd like to have the user redirected to the Account and display something like "You've attempted to delete a contact related to this Frozen account." But I'm not entirely sure what the correct way to code something like this would be. I've tried with an IF-statement on the trigger, which works, but only on a single delete. Could someone point me in the right direction for a better solution (if there is one)?

Current trigger attempt:

trigger UpdateAccountDetail on Contact (after insert, after update, after delete) {
        Contact[] Contacts = trigger.new;
        UpdateAccountDetail.updateContactList (contacts);

    if(trigger.isDelete && contact.account.frozen__c = FALSE){
        Contact[] contacts = trigger.old;
        UpdateAccountDetail.updateContactList (contacts);
        <<jump to account and display error w/o delete>>
  • Quick update: I've talked with some developers and there is thought that the way to handle the situation is to use a Try/Catch block, but I'm not sure even with that code how to 'stop' the deletion. Ideas? – HomerJ May 19 '16 at 14:27

For the delete trigger unfortunately the addError does not work and the error screen can't be controlled .

Unless you override delete button with a Custom Button and using some Visualforce this is not possible

  • That's unfortunate. So what is the correct way to 'escape' from the delete and communicate to the user that he/she cannot delete the Contact because of the Account value? – HomerJ May 16 '16 at 21:06
  • If user is too particular then a vf is needed else I would live with ugly error – Mohith Shrivastava May 16 '16 at 21:10

You can override the Delete action (go to Setup -> Customize -> Contacts -> Buttons, Links and Actions) by creating a page that uses a standard controller and extension:

public with sharing class DeleteContactController {

    private ApexPages.StandardController sc;

    public DeleteContactController(ApexPages.StandardController sc) {
        this.sc = sc;

    public PageReference init() {
        // Use object ID from sc.getId() in logic
        Boolean allowDelete = ...;
        if (allowDelete) {
            // Do delete: may just be able to return the PageReference this returns
        // Go to new page or stay on current page and display error
        return allowDelete ? new PageReference(ApexPages.currentPage().getParameters().get('retURL')) : null;

<apex:page standardController="Contact" extensions="DeleteContactController" action="{!init}">
    <apex:pageMessage summary="You can't do that." severity="error" strength="3"/>

Clicking the standard "Delete" will then invoke the page which puts up an error message if the delete shouldn't go ahead and deletes and then just forwards without displaying anything of the delete should go ahead.

Unfortunately, the "Are you sure?" prompt still precedes the override page. Also this only stops users from deleting: it does not stop other business logic from deleting (that a trigger would stop).

  • And, of course, if they do this, they might still need to implement a trigger to block "creative" users from getting around the page. – sfdcfox May 16 '16 at 21:42
  • @sfdcfox Hmmm. Hope there isn't an equivalent of /e that does a delete... But point taken: UI and trigger would be safest. – Keith C May 16 '16 at 21:46
  • 1
    Depends on what else admins let the user do. Sync with Outlook? There's a deletion path there. Use the Data Loader? Actually, I'm pretty sure just using Salesforce1 would let them get away with it (especially if the VF page isn't "enabled for mobile."). And yes, there is a URL you can use to delete, it just needs a "confirmation token" that you could scrape from the screen. However, using this with a trigger will give those non-savvy users a friendlier error message. – sfdcfox May 16 '16 at 21:55
  • This is all great feedback, but at the end of the day, I just want to stop a delete with a message. The class that does the rest of the (unrelated) work is already in place, but throws a garbage error when the Account is Frozen (via validation rule). So, unless I override the delete button, there's no other options? – HomerJ May 16 '16 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.