2

I have some trigger logic, which prevents Account to be deleted under specific circumstances. This part works. However when user tries to delete Account via GUI, Salesforce displays really ugly and confusing alert: "There's a problem saving this record. You might not have permission to edit it, or it might have been deleted or archived. Contact your administrator for help."

It should display exception message instead. From my experience it usually does. Why not in this case? Did I miss some setting? I'll be grateful for any hint or explanation.

EDIT:

            if(String.IsEmpty(acc.NavisionId__c))           
            {
                //irrelevant logic                      
            }
            else
            {
                throw new MyCustomInvalidOperationException('You cannot delete Account which has already become a customer and is linked to Navision. You can however change its Status to Former Customer instead');
            } 

2 Answers 2

3

Why don't you use trigger.addError() instead of custom exception handling

Go through the following example:

eg. Suppose i want to restrict the user from deleting an account, if account has related opportunities

trigger AccountDeletion on Account (before delete) {
//Prevent the deletion of accounts if they have related opportunity.
for (Account a : [SELECT Id FROM Account
                 WHERE Id IN (SELECT AccountId FROM Opportunity) AND
                 Id IN :Trigger.old]){
    //Error
    Trigger.oldMap.get(a.Id).addError(
        'Cannot delete account with related opportunities.');
}

The same way you can do!

1

It's nearly impossible to fully answer your question without seeing your trigger code. That being said, my best guess is that the GUI is causing page validation rules to fire which prevents the page from being deleted by the user that are related to the user's permissions. In essence, they don't have CRUD permissions to delete the record.

1
  • I added short snippet. This code caused break in the trigger run. Simple exception throw. Regarding users right and permissions, when I remove this code, the Account can be deleted without any issues.
    – Qerts
    Commented Sep 26, 2017 at 12:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .