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
Nagarjuna Reddy.PNagarjuna Reddy.P 

Before delete Trigger

Hi All,
          I wrote a simple trigger with before delete event on contact to prevent deletion if either contact field source__c == 'SAPExternal' or its related account.source=='SAPExternal'.my code is as follows.

public class ContactTriggerExamples {
 public static void beforeDelete(List<Contact> cns){
    for(Contact c:cns){
                 if( c.Source__c=='SAPExternal' || c.Account.Source=='SAPExternal'){
                     c.addError('You cant Delete Contact with Source as SAPExternal'); 
                 } 
           } 
}
trigger:
trigger ContactTriggerExamples on Contact (before Delete){
 if(Trigger.isDelete && Trigger.isBefore){
        ContactTriggerExamples.beforeDelete(Trigger.old);
    }
}
  It is perfectly working for first condtition i.e c.source == 'SAPExternal' and it is not working for c.account.source=='SAPExternal',whats wrong with my code.
Thank you all.
Best Answer chosen by Nagarjuna Reddy.P
Anant KamatAnant Kamat
You cannot reference Parent fields directly in child without querying them. First you need to query them and then use it.

public class ContactTriggerExamples {
    public static void beforeDelete(List < Contact > cns) {
        Set < Id > acctIds = new Set < id > ();   
        
        for (Contact c: cns) {
            acctIds.add(c.AccountId);
        }

        List < Account > lstAccts = [Select id, Source__c from Account where id in: acctIds];
        Map < String, String > mapContactToAccSource = new Map < String, String > ();

        for (Account a: lstAccts) {
            mapContactToAccSource.add(a.id, a.Source__c);
        }

        for (Contact c: cns) {                     
            if (c.Source__c == 'SAPExternal' || mapContactToAccSource.get(c.AccountId) == 'SAPExternal') {           
                c.addError('You cant Delete Contact with Source as SAPExternal');          
            }       
        } 
    }
}

Try this and let me know if it works for you.

All Answers

Nagarjuna Reddy.PNagarjuna Reddy.P
Account field Source is cutom field and i used it as c.Account.Source__c == 'SAPExternal'
Harsh P.Harsh P.
Is it Working now ?
Anant KamatAnant Kamat
You cannot reference Parent fields directly in child without querying them. First you need to query them and then use it.

public class ContactTriggerExamples {
    public static void beforeDelete(List < Contact > cns) {
        Set < Id > acctIds = new Set < id > ();   
        
        for (Contact c: cns) {
            acctIds.add(c.AccountId);
        }

        List < Account > lstAccts = [Select id, Source__c from Account where id in: acctIds];
        Map < String, String > mapContactToAccSource = new Map < String, String > ();

        for (Account a: lstAccts) {
            mapContactToAccSource.add(a.id, a.Source__c);
        }

        for (Contact c: cns) {                     
            if (c.Source__c == 'SAPExternal' || mapContactToAccSource.get(c.AccountId) == 'SAPExternal') {           
                c.addError('You cant Delete Contact with Source as SAPExternal');          
            }       
        } 
    }
}

Try this and let me know if it works for you.
This was selected as the best answer
Harsh P.Harsh P.
This below code is working for me.
Try this code:

Controller:

public class PreventDeleteController {
    public static void beforeDelete(List < Contact > cns) {
        Set < Id > acctIds = new Set < id > ();   
        
        for (Contact c: cns) {
            acctIds.add(c.AccountId);
        }

        List < Account > lstAccts = [Select id, Source__c from Account where id in: acctIds];
        Map < String, String > mapContactToAccSource = new Map < String, String > ();

        for (Account a: lstAccts) {
            mapContactToAccSource.put(a.id, a.Source__c);
        }

        for (Contact c: cns) {                     
            if (c.Source__c == 'SAPExternal' || mapContactToAccSource.get(c.AccountId) == 'SAPExternal') {           
                c.addError('You cant Delete Contact  OR Account those having Source as SAPExternal');          
            }       
        } 
    }
}



Trigger :

trigger PreventToDelete on Contact (before Delete) {
    
 if(Trigger.isDelete && Trigger.isBefore){
        PreventDeleteController.beforeDelete(Trigger.old);
    }
}


If It help mark as best Answere ...................
Thanks .!
Nagarjuna Reddy.PNagarjuna Reddy.P
@Anant Kamat,
                             Thank you for your reply and your code inspired me to using of maps in situations like the above scenario and elaborated me to think in different ways and finally its working.
Nagarjuna Reddy.PNagarjuna Reddy.P
@Harsh P.
                   I tried it and its working.
          Thanks for your help.