Skip to main content
Fixing code formatting, but more better this time
Source Link
Derek F
  • 64.1k
  • 15
  • 55
  • 104

I am trying to create a Trigger which will prevent deletion of Account which has atleast one contact linked to it. But its not working. Here is my code

trigger AccountDeleteonPrevention on Account (before delete) {
    
    if(Trigger.isBefore && Trigger.isDelete){
           for(Account acc: Trigger.old){
                          
             if(acc.Contacts.size()>0){
                   acc.addError('You cannot delete this account');
                   
               }          
       
    }
        
    }
   
}
trigger AccountDeleteonPrevention on Account (before delete) {
    if(Trigger.isBefore && Trigger.isDelete){
        for(Account acc: Trigger.old){
            if(acc.Contacts.size() > 0){
                acc.addError('You cannot delete this account');
            }
        }
    }
}

When ImI'm deleting Accounts which have contacts in my org they are getting deleted. Also I see that in debugging the value of account.Contacts.size()account.Contacts.size() comes to be 0 which is strange. ThatsThat's the reason my if block is not getting evaluated. I am unable to understand my mistake here. Please help

I am trying to create a Trigger which will prevent deletion of Account which has atleast one contact linked to it. But its not working. Here is my code

trigger AccountDeleteonPrevention on Account (before delete) {
    
    if(Trigger.isBefore && Trigger.isDelete){
           for(Account acc: Trigger.old){
                          
             if(acc.Contacts.size()>0){
                   acc.addError('You cannot delete this account');
                   
               }          
       
    }
        
    }
   
}

When Im deleting Accounts which have contacts in my org they are getting deleted. Also I see that in debugging the value of account.Contacts.size() comes to be 0 which is strange. Thats the reason my if block is not getting evaluated. I am unable to understand my mistake here. Please help

I am trying to create a Trigger which will prevent deletion of Account which has atleast one contact linked to it. But its not working. Here is my code

trigger AccountDeleteonPrevention on Account (before delete) {
    if(Trigger.isBefore && Trigger.isDelete){
        for(Account acc: Trigger.old){
            if(acc.Contacts.size() > 0){
                acc.addError('You cannot delete this account');
            }
        }
    }
}

When I'm deleting Accounts which have contacts in my org they are getting deleted. Also I see that in debugging the value of account.Contacts.size() comes to be 0 which is strange. That's the reason my if block is not getting evaluated. I am unable to understand my mistake here. Please help

edited tags
Link
Derek F
  • 64.1k
  • 15
  • 55
  • 104
Source Link
Amax1
  • 121
  • 4
  • 16

Trigger to prevent deletion of account which has atleast one contact linked to it not working

I am trying to create a Trigger which will prevent deletion of Account which has atleast one contact linked to it. But its not working. Here is my code

trigger AccountDeleteonPrevention on Account (before delete) {
    
    if(Trigger.isBefore && Trigger.isDelete){
           for(Account acc: Trigger.old){
                          
             if(acc.Contacts.size()>0){
                   acc.addError('You cannot delete this account');
                   
               }          
       
    }
        
    }
   
}

When Im deleting Accounts which have contacts in my org they are getting deleted. Also I see that in debugging the value of account.Contacts.size() comes to be 0 which is strange. Thats the reason my if block is not getting evaluated. I am unable to understand my mistake here. Please help