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
BhagyaBhagya 

Avoid deletion of contact related to account.

Can someone help me with this trigger?
Avoid deletion of related contact while deleting account which has rating as 'warm' and No_of_Employees__c is not empty(add error message).
I've tried this, but not working and not showing error message.
trigger AvoidContactDel on Account (before delete) {
    list<account> acc = new list<account>();
    list<contact> con = new list<contact>();
    set<id> ids = new set<id>();
    for(account a : trigger.old){
    ids.add(a.id);
    }
    con = [select id,lastname from contact where accountid IN : ids ];
    //acc = [select id,name,stage,No_of_Employees__c from account where id in : ids];
    for(account a : trigger.old){
        for(contact c : con){
        if(a.id == c.accountid && a.rating == 'Warm' && a.No_of_Employees__c != null){
            a.adderror('you are not allow to delete this contact for some reasons');     
        }
        }
    }
    
}
       
 
Best Answer chosen by Bhagya
mukesh guptamukesh gupta
Hi Neela,

Please use below code:-
 
trigger AvoidContactDel on Account (before delete) {
    list<contact> conList = new list<contact>();
    conList = [select id,accountid,lastname from contact where accountid IN : Trigger.OldMap.keySet()];
    for(account a : trigger.old){
        for(contact c : conList){
            if(a.id == c.accountid && a.rating == 'Warm' && a.No_of_Employees__c != null){
                a.adderror('you are not allow to delete this contact for some reasons');     
            }
        }
    }
    
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

All Answers

mukesh guptamukesh gupta
Hi Neela,

Please use below code:-
 
trigger AvoidContactDel on Account (before delete) {
    list<contact> conList = new list<contact>();
    conList = [select id,accountid,lastname from contact where accountid IN : Trigger.OldMap.keySet()];
    for(account a : trigger.old){
        for(contact c : conList){
            if(a.id == c.accountid && a.rating == 'Warm' && a.No_of_Employees__c != null){
                a.adderror('you are not allow to delete this contact for some reasons');     
            }
        }
    }
    
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
This was selected as the best answer
PriyaPriya (Salesforce Developers) 
Hey Neela,

Kindly check these 2 links where similar task has been discussed :-
1. https://salesforce.stackexchange.com/questions/111098/account-that-has-related-contacts-should-not-be-deleted
2. https://developer.salesforce.com/forums/?id=9060G0000005QR4QAM
 

Kindly mark it as the best answer if it works for you.

 

Thanks & Regards,

Priya Ranjan