You need to sign in to do that
Don't have an account?
Usha 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
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
So if you include this line, 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. It's a very subtle error, easy to miss. I hope this helps!
All Answers
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.
Hope this Help.
Le
So if you include this line, 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. It's a very subtle error, easy to miss. I hope this helps!