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 change a field of related contact before delete

Hi All,

i have a trigger on account object which upon delete action sends notification of the related contacts of the account to an external system, Now i want a particular field called status__c in contact to be changed to value "inactive" once the trigger is fired. The status__c field update to "inactive" should only happen on delete action. Please help me achieve this in below trigger.
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);     
    }
    }
    else{
    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 = '012D1676545BaFA']){
          WebServiceCallout.sendNotification(con.Id,con.Email,con.FirstName,con.LastName,con.phone,con.Title__c,con.Account.Name,con.status__c);
        }
    }
   }

 
Best Answer chosen by Abraham kumar
Abhishek BansalAbhishek Bansal
Hi Abraham,

Please change your code with below one :
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);     
    }
	}
    else{
    for(Account acct : trigger.new) {
        if(Trigger.oldMap.get(acct.id).Name != acct.Name){
             acctIds.add(acct.Id);  
             }     
    }
    }
&nbsp;   //Added Newly by Abhishek : Start
	if(trigger.isDelete && acctIds.size() > 0){
		List<Contact> updateContactList = new List<Contact>();
		for(Contact con : [Select status__c from Contact where AccountId IN : acctIds]){//Use any other filters if requires
			con.status__c = 'Inactive';
			updateContactList.add(con);
		}
		if(updateContactList.size() > 0){
			update updateContactList;
		}
	}
    //Added Newly by Abhishek : End

    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 = '012D1676545BaFA']){
          WebServiceCallout.sendNotification(con.Id,con.Email,con.FirstName,con.LastName,con.phone,con.Title__c,con.Account.Name,con.status__c);
        }
    }
    
   }

Thanks,
Abhishek

All Answers

Abhishek BansalAbhishek Bansal
Hi Abraham,

Please change your code as mentioned below :
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);     
    }
	}
    else{
    for(Account acct : trigger.new) {
        if(Trigger.oldMap.get(acct.id).Name != acct.Name){
             acctIds.add(acct.Id);  
             }     
    }
    }
    //Added Newly by Abhishek : Start
	if(trigger.isDelete && acctIds.size() > 0){
		List<Contact> updateContactList = new List<Contact>();
		for(Contact con : Select status__c from Contact where AccountId IN : acctIds){//Use any other filters if requires
			con.status__c = 'Inactive';
			updateContactList.add(con);
		}
		if(updateContactList.size() > 0){
			update updateContactList;
		}
	}
    //Added Newly by Abhishek : End

    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 = '012D1676545BaFA']){
          WebServiceCallout.sendNotification(con.Id,con.Email,con.FirstName,con.LastName,con.phone,con.Title__c,con.Account.Name,con.status__c);
        }
    }
    
   }

I have added the code in between "Added Newly by Abhishek : Start/End" tags.
Please modify your code as above and let me know if you have issue.

Thanks,
Abhishek

Abraham kumarAbraham kumar

Hi Abishek,

Many Thanks for the quick help.. Im just getting this error in line 19
Error: Compile Error: unexpected token: 'Select' at line 19 column 26... 

Thanks
Abraham

Abhishek BansalAbhishek Bansal
Hi Abraham,

Please change your code with below one :
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);     
    }
	}
    else{
    for(Account acct : trigger.new) {
        if(Trigger.oldMap.get(acct.id).Name != acct.Name){
             acctIds.add(acct.Id);  
             }     
    }
    }
&nbsp;   //Added Newly by Abhishek : Start
	if(trigger.isDelete && acctIds.size() > 0){
		List<Contact> updateContactList = new List<Contact>();
		for(Contact con : [Select status__c from Contact where AccountId IN : acctIds]){//Use any other filters if requires
			con.status__c = 'Inactive';
			updateContactList.add(con);
		}
		if(updateContactList.size() > 0){
			update updateContactList;
		}
	}
    //Added Newly by Abhishek : End

    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 = '012D1676545BaFA']){
          WebServiceCallout.sendNotification(con.Id,con.Email,con.FirstName,con.LastName,con.phone,con.Title__c,con.Account.Name,con.status__c);
        }
    }
    
   }

Thanks,
Abhishek
This was selected as the best answer
Abraham kumarAbraham kumar
Thats perfect!!!!  Thank you soo much 

Thanks
Abraham