function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Manjunath T NManjunath T N 

How to display validation error in same page using trigger on click of delete button?

How to display validation error in same page without redirecting to new page using trigger on click of delete button?
Sanjay Bhati 95Sanjay Bhati 95
Hi Manjunath

First check the condition in your trigger and add the below line in trigger it will display the error on same page once you handle it on ui
Trigger.new[0].addError('your message');
Please check and let me know
Thanks
Ajay K DubediAjay K Dubedi
Hi Manjunath,
Assume you want to create an Account and it's IsActive(Checkbox) field is always false when it is created and if it is true your page display a validation error that "This Account cannot be created with  IsActive = true."
Try this code:
Trigger:
trigger AccountTriggerToCheckValidation on Account (before insert) {
    if(trigger.IsInsert && trigger.IsBefore) {
        AccountTriggerToCheckValidation_handler.checkIsActiveField(trigger.new);
    }
}

Trigger Handler:
public class AccountTriggerToCheckValidation_handler {
    public static void checkIsActiveField(List<Account> accList) {
        try{
            if(accList.size() > 0) {
                for (Account ac : accList) {
                    if (ac.IsActive__c == true) {
                        ac.addError('This Account cannot be created with  IsActive = true.');
                    }
                }
            }
        } catch(Exception e) {
            System.debug('The following exception has occurred: ' + e.getMessage());
            System.debug('The following Line : ' + e.getLineNumber());
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Manjunath T NManjunath T N
@Ajay, @Sanjay  Thanks for your suggestion and I tried the same but couldn't able to achive. I am unable to display error in same page and below is my code snippet.

Trigger code:
  if (Trigger.isBefore)
    {
        if(Trigger.isDelete)
        {
            AccountDelete.checkOOSAccountId(trigger.old);
        }
     }

class: 
public with sharing class AccountDelete {
    public static void checkOOSAccountId(List<Account> accList)
    {
        try
        {
            for(Account a: accList)
            {
                if(a.OOSAccountId__c != null)
                {
                    //Trigger.old[0].addError('This account can not be deleted.');
                    //a.addError('This account can not be deleted.');
                    a.OOSAccountId__c.addError('This account can not be deleted.');
                }
            }
        }
        catch(Exception e)
        {
            system.debug('The following exception has occured:' +e.getMessage());
            system.debug('The following Line:' + e.getLineNumber());
        }
    }
}

error: (Showing in another page)
Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "This account can not be deleted.". 

Click here to return to the previous page.
Ajay K DubediAjay K Dubedi
Hi Manjunath,
Using standard page we can have these kinds of messages only when we save info, but we cannot have this for delete actions as the page will be redirected to some predefined page. The only way is to customize the detail page using visual force and there we can have own custom validation.
https://developer.salesforce.com/forums/?id=906F000000090gBIAQ
Thanks,
Ajay Dubedi