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
Abraham kumarAbraham kumar 

Trigger to fire on Before Delete

Hi All,

I am having below trigger which calls a method "sendNotification" when the account name is changed. I want this trigger to call the method when the account is deleted ie)on before delete operation. Please help me on how this can be done.
trigger Contactcallout2 on Account(after update) {
List<Contact> ListContact = new List<Contact>();
Set<ID> acctIds = new set<ID>();
    for(Account acct : trigger.new) {
        if(Trigger.oldMap.get(acct.id).Name != acct.Name){
             acctIds.add(acct.Id);      
    }
    }
    if(acctIds.size() > 0){
        for(Contact con : [SELECT id,Email,FirstName,LastName,phone,Title__c,Account.Name,status__c, AccountId FROM Contact WHERE AccountId IN : acctIds AND RecordTypeid = '012D6765454BaFA']){
          WebServiceCallout.sendNotification(con.Id,con.Email,con.FirstName,con.LastName,con.phone,con.Title__c,con.Account.Name,con.status__c);
        }
    }
   }
Many Thanks in advance
Abraham
 
Best Answer chosen by Abraham kumar
Prabhat Kumar12Prabhat Kumar12
Use Before delete and trigger.old.
 
trigger Contactcallout2 on Account(Before Delete) {
List<Contact> ListContact = new List<Contact>();
Set<ID> acctIds = new set<ID>();

if(Trigger.IsBefore && Trigger.IsDelete){
	
for(Account acct : trigger.old) {
        
             acctIds.add(acct.Id);      
    
    }
    if(acctIds.size() > 0){
        for(Contact con : [SELECT id,Email,FirstName,LastName,phone,Title__c,Account.Name,status__c, AccountId FROM Contact WHERE AccountId IN : acctIds AND RecordTypeid = '012D6765454BaFA']){
          WebServiceCallout.sendNotification(con.Id,con.Email,con.FirstName,con.LastName,con.phone,con.Title__c,con.Account.Name,con.status__c);
        }
    }
	}
}

 

All Answers

Prabhat Kumar12Prabhat Kumar12
Use Before delete and trigger.old.
 
trigger Contactcallout2 on Account(Before Delete) {
List<Contact> ListContact = new List<Contact>();
Set<ID> acctIds = new set<ID>();

if(Trigger.IsBefore && Trigger.IsDelete){
	
for(Account acct : trigger.old) {
        
             acctIds.add(acct.Id);      
    
    }
    if(acctIds.size() > 0){
        for(Contact con : [SELECT id,Email,FirstName,LastName,phone,Title__c,Account.Name,status__c, AccountId FROM Contact WHERE AccountId IN : acctIds AND RecordTypeid = '012D6765454BaFA']){
          WebServiceCallout.sendNotification(con.Id,con.Email,con.FirstName,con.LastName,con.phone,con.Title__c,con.Account.Name,con.status__c);
        }
    }
	}
}

 
This was selected as the best answer
Abraham kumarAbraham kumar
HI prabhat,

Thank you soo much for the quick help and response.
i tried the above code it compiles successfully but i get below error on deleting an account
Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger Contactcallout2 caused an unexpected exception, contact your administrator: Contactcallout2: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Contactcallout2: line 8, column 1". 
 
trigger Contactcallout2 on Account(after update, before delete) {
List<Contact> ListContact = new List<Contact>();
Set<ID> acctIds = new set<ID>();
if(Trigger.IsBefore && Trigger.IsDelete){
for(Account acct : trigger.old) {
             acctIds.add(acct.Id);     
    }
    for(Account acct : trigger.new) {
        if(Trigger.oldMap.get(acct.id).Name != acct.Name){
             acctIds.add(acct.Id);       
    }
    }
    if(acctIds.size() > 0){
        for(Contact con : [SELECT id,Email,FirstName,LastName,phone,Title__c,Account.Name,status__c, AccountId FROM Contact WHERE AccountId IN : acctIds AND RecordTypeid = '012D5467876BaFA']){
          WebServiceCallout.sendNotification(con.Id,con.Email,con.FirstName,con.LastName,con.phone,con.Title__c,con.Account.Name,con.status__c);
        }
    }
   }
}

Many Thanks in advance
Abraham
Abraham kumarAbraham kumar
Thanks works now with else condition in line 8

Thanks