You need to sign in to do that
Don't have an account?

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.
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.
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
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.
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 .!
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.
I tried it and its working.
Thanks for your help.