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
neela prasadneela prasad 

I have A related List of Custom Object on Contact Related Lists.So Now I Should restrict User to delete whenever I have record On that related List on Contact Screen. Could anybody help Me with Any Suggestion On trigger?

Best Answer chosen by neela prasad
Jainam ContractorJainam Contractor
Hi Neela,

You can write trigger on Contact for before delete event for this case.

Please refer the below code. Change the sObject fields to your Own Custom Object fields

trigger DontDeleteContact on Contact (before delete) {
    list<Id> ConId = new list<Id>();
    map<Id, List<sObject>> IDListMap = new map<Id, List<sObject>>();
    for(Contact C : Trigger.Old){
        ConId.add(C.Id);
    }
    for(sObject s : [select id, name, contact__c from sObject where contact__c IN :ConId order by contact__c]){
        for(Contact C : Trigger.Old){
            if(C.Id == s.contact__c){
                c.addError('Contact have an existing related record. Hence it can not be deleted');
            }
        }
    }

}

Please let me know if it works.

Please mark it as the solution if it helped you.

Thanks,
Jainam Contractor,
Salesforce Consultant
Varasi LLC
www.varasi.com