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
Usha Charles 3Usha Charles 3 

Validation on Contacts deletion

Hello,

Below is a code I created for validating contact deletion. But it dosent seem to work, I'm able to delete the contacts from accounts whose country is India. The trigger is active. Can someone please let me know the reason ?

1    trigger SV1 on Contact (before delete) {
2        //donot allow deletion of any contact whose account country is India
3        for (Contact cont : trigger.old){
4                    if(cont.Account.country__c == 'India'){
5                Cont.addError ('You Cannot delete the contacts of Indian accounts');
6            }
7            
8        }
9            
10    }

Regards,
Usha

 
Best Answer chosen by Usha Charles 3
pigginsbpigginsb
You are attempting to compare the Account's Country value by traversing the relationship to the Account record. This will only be available by querying for the contact records. Inside trigger.old, the value will be null.

So if you include this line,
List<Contact> contactList = [select Id, Account.Country__c from Contact where Id in :trigger.old];
you will have the Account's Country value in a list of Contacts in your trigger.

But you will not be able to use addError() on the rows retrieved via this SOQL statement. You will have to use addError() on the rows inside trigger.old or trigger.oldMap.

By iterating over the contactList from the query, you can determine if any of the rows in your trigger have "India" as their Account's country, then you can use the Id to get the same record from trigger.oldMap and add the error to there.
for (Contact cont : contactList) {
    if(cont.Account.country__c == 'India') {
        trigger.oldMap.get(cont.Id).addError ('You Cannot delete the contacts of Indian accounts');
    }
}
It's a very subtle error, easy to miss. I hope this helps!

All Answers

Le NguyenLe Nguyen
Hi Usha,

Unfortunally, You cannot access related field in trigger.old.  Therefore, cont.Account.country__c is always null in this content. 

To you around this. You can either query accounts or follow this simple workaround.

1. Create a text formula field country__c in Contact which get value from Account.country__c. 

2. Fix your trigger to get value from the furmula field.
trigger SV1 on Contact (before delete) {
        //donot allow deletion of any contact whose account country is India
        for (Contact cont : trigger.old){
                    if(cont.country__c == 'India'){
                Cont.addError ('You Cannot delete the contacts of Indian accounts');
            }
            
        }
            
    }

Hope this Help.

Le
pigginsbpigginsb
You are attempting to compare the Account's Country value by traversing the relationship to the Account record. This will only be available by querying for the contact records. Inside trigger.old, the value will be null.

So if you include this line,
List<Contact> contactList = [select Id, Account.Country__c from Contact where Id in :trigger.old];
you will have the Account's Country value in a list of Contacts in your trigger.

But you will not be able to use addError() on the rows retrieved via this SOQL statement. You will have to use addError() on the rows inside trigger.old or trigger.oldMap.

By iterating over the contactList from the query, you can determine if any of the rows in your trigger have "India" as their Account's country, then you can use the Id to get the same record from trigger.oldMap and add the error to there.
for (Contact cont : contactList) {
    if(cont.Account.country__c == 'India') {
        trigger.oldMap.get(cont.Id).addError ('You Cannot delete the contacts of Indian accounts');
    }
}
It's a very subtle error, easy to miss. I hope this helps!
This was selected as the best answer
Usha Charles 3Usha Charles 3
Thanks a lot for the quick reply ! It works now !