You need to sign in to do that
Don't have an account?
Manjunath 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?
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 Please check and let me know
Thanks
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 Handler:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
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.
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