Trigger NoAccountIdNoDelete on Contact (Before Delete) {
If(Trigger.IsDelete){
For(Contact Con : Trigger.Old)
{
If(Con.AccountId == NULL)
{
Con.addError('You cannot delete the contact who does not have any associated account');
}
}
}
}
public class Default_CreateContact { public static void createContact(List<Contact> contList) { for(Contact con: contList) { if(con.AccountId == NULL) { con.addError('You Can not delete the account'); } } } }
// Trigger
trigger deleteContact on Contact (before delete) {
Trigger NoAccountIdNoDelete on Contact (Before Delete) {
If(Trigger.IsDelete){
For(Contact Con : Trigger.Old)
{
If(Con.AccountId == NULL)
{
Con.addError('You cannot delete the contact who does not have any associated account');
}
}
}
}
Hope this helps!
All Answers
Please try the below code.
// Apex Class
public class Default_CreateContact {
public static void createContact(List<Contact> contList)
{
for(Contact con: contList)
{
if(con.AccountId == NULL)
{
con.addError('You Can not delete the account');
}
}
}
}
// Trigger
trigger deleteContact on Contact (before delete) {
if(Trigger.IsDelete && Trigger.IsBefore)
{
Default_CreateContact.createContact(Trigger.old);
}
}
Please select it as best answer if you find it helpful
Thank You,
Ajay Dubedi
Hope this helps!