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
Vidya H 4Vidya H 4 

I have a text field on Account that is "Primary contact". User should not be able to delete the Primary Contact.I need to achive this by triggers

Need to trigger with handler class.

I have a text field on Account that is "Primary contact"(text field)  where you will give the related contact name of that particular account, which ever contact name is given in that field shouldn't be able to delete by any user.
ex - if account A has 2 related contacts i.e, c1 and c2.
now i will update that Primary contact field with related contact c1.
now any user should not be able to delete that C1 related contact.
Best Answer chosen by Vidya H 4
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vidya,

You can use the below trigger and hanlder.
 
trigger DoNotDeleteAccountHavingRelatedContact on Contact (before delete) {
    
    if(Trigger.isdelete && Trigger.isbefore){
        DeleteHandler d= new DeleteHandler();
        d.donotdelete(Trigger.old);
        
    }
}
 
public class DeleteHandler {

    public void donotdelete(List<Contact> conlist)
    {
        system.debug('into handler');
        map<id,String> accoutmap= new map<id,String>();
        set<id> accountid= new set<id>();
        for(Contact c: conlist){
            accountid.add(c.accountid);
            system.debug('accountid'+c.AccountId);
            accoutmap.put(c.accountid,c.name);
        }
        map<id,Account> mapaccount= new map<id,Account>([select id, Primary_Contact__c from account where id in :accountid ]);
        
        for(Contact c: conlist){
             system.debug('accountid'+mapaccount.get(c.AccountId).Primary_Contact__c);
            system.debug('name'+c.name);
            if((c.FirstName + ' ' + c.Lastname)  == mapaccount.get(c.AccountId).Primary_Contact__c){
                c.adderror('Primary contact cannot be deleted');
            }
            
        }
    }
}

If this solution helps,Please mark it as best answer.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vidya,

You can use the below trigger and hanlder.
 
trigger DoNotDeleteAccountHavingRelatedContact on Contact (before delete) {
    
    if(Trigger.isdelete && Trigger.isbefore){
        DeleteHandler d= new DeleteHandler();
        d.donotdelete(Trigger.old);
        
    }
}
 
public class DeleteHandler {

    public void donotdelete(List<Contact> conlist)
    {
        system.debug('into handler');
        map<id,String> accoutmap= new map<id,String>();
        set<id> accountid= new set<id>();
        for(Contact c: conlist){
            accountid.add(c.accountid);
            system.debug('accountid'+c.AccountId);
            accoutmap.put(c.accountid,c.name);
        }
        map<id,Account> mapaccount= new map<id,Account>([select id, Primary_Contact__c from account where id in :accountid ]);
        
        for(Contact c: conlist){
             system.debug('accountid'+mapaccount.get(c.AccountId).Primary_Contact__c);
            system.debug('name'+c.name);
            if((c.FirstName + ' ' + c.Lastname)  == mapaccount.get(c.AccountId).Primary_Contact__c){
                c.adderror('Primary contact cannot be deleted');
            }
            
        }
    }
}

If this solution helps,Please mark it as best answer.

Thanks,
 
This was selected as the best answer
Vidya H 4Vidya H 4
@Sai Praveen  its not working
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vidya,

Did you replace the field API name of primary contact at all the places.Are you facing any errors or you are able to delete the contact even you have the name. Can you also provide the Contact Name and primary contact name in account screenshots.

Thanks,