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
Abishek NDAbishek ND 

Throw an error whenever the user tries to delete the contact which is not associated to the account want to use trigger with hander class

Best Answer chosen by Abishek ND
Suraj Tripathi 47Suraj Tripathi 47
Hi Abhisek,

try below code ,it will help you
trigger triggerOnContact on Contact (before Delete) {
    
    if(Trigger.isDelete){
        if(trigger.isBefore){
            contactTriggerHelper.restrictToDeleteContact(trigger.old);
        }
    }
    
    
    Helper Class
        public class contactTriggerHelper{
            public static void restrictToDeleteContact(list<Contact> ContactList){
                
                try{
                    for(Contact conObj : ContactList){ 
                        if(conObj .AccountId == null){
                            conObj .AddError('Cannot delete contact because it is not associated with account'); 
                        }   
                    } 
                }catch(exception e){  
                    system.debug('get error message ==>'+e.getMessage()+' in line number ==> '+e.getLineNumber());
                    
                }
            }
        }

If you find your Solution then mark this as the best answer.

Thank you!
Regards,
Suraj Tripathi  

All Answers

CharuDuttCharuDutt
Hii Abhishek
Try Below Trigger
trigger ContactTrigger on Contact (before Delete) {

    if(Trigger.isDelete){
        if(trigger.isBefore){
      ContactTriggerHandler.deleteMethod(trigger.old);
        }
    }


HANDLER Class
public class ContactTriggerHandler {
 public static Void deleteMethod(list<Contact> lstCon){
  
         for(Contact con : lstCon){ 
            if(con.AccountId == null){
                    Con.AddError('Cannot delete Contact Without Account'); 
            }   
        } 
    }
}    
  
Please Mark It As Best Answer If It Helps
Thank You!
SFDC12SFDC12
Hi ,please try below trigger.

trigger Trigger_Example1 on Contact (before insert) {
List<contact>cnlist=new List<contact>();
    for(contact c:Trigger.new){
        if(c.AccountId==null){
            c.adderror('contact record should have an account');
        }
        
    }
}
Suraj Tripathi 47Suraj Tripathi 47
Hi Abhisek,

try below code ,it will help you
trigger triggerOnContact on Contact (before Delete) {
    
    if(Trigger.isDelete){
        if(trigger.isBefore){
            contactTriggerHelper.restrictToDeleteContact(trigger.old);
        }
    }
    
    
    Helper Class
        public class contactTriggerHelper{
            public static void restrictToDeleteContact(list<Contact> ContactList){
                
                try{
                    for(Contact conObj : ContactList){ 
                        if(conObj .AccountId == null){
                            conObj .AddError('Cannot delete contact because it is not associated with account'); 
                        }   
                    } 
                }catch(exception e){  
                    system.debug('get error message ==>'+e.getMessage()+' in line number ==> '+e.getLineNumber());
                    
                }
            }
        }

If you find your Solution then mark this as the best answer.

Thank you!
Regards,
Suraj Tripathi  
This was selected as the best answer