+ Start a Discussion
Abraham kumarAbraham kumar 

Trigger to fire before delete

Hi All,

Please help me with this trigger. It currently calls a class called webservicecallout and send notification method on insert and update actions. I want it to also call this class and method once the contact is deleted.. Please help
trigger Contactcall on Contact (after insert, after update) {
Map<Id, String> m = new Map<Id, String>();
list<Contact> validContacts = new list<Contact>();
set<ID> accIds = new set<ID>();
for (contact c : Trigger.new) {
    if(c.RecordTypeId == '012D7865454BaFA' && c.Reg__c == TRUE){
    if(Trigger.isUpdate){
        contact old = Trigger.oldMap.get(c.Id);
        if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID)
         {
             validContacts.add(c);
                accIds.add(c.accountid);
         }
         }else{
         validContacts.add(c);
                accIds.add(c.accountid);
         }   
}
map<ID, Account> accMap;
if(!accIds.IsEmpty()) // guard condition for SOQL
    accMap = new map<ID, Account>([select name from account where id in :accIds]);

for (contact c1 : validContacts) {
    Account acc = accMap.get(c.AccountID);
    string accName = acc == null ? null : acc.Name;
    WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.phone,c.Title__c,accName,c.status__c);
            
    
    }
}
}

Many Thanks in advance
Abraham
Best Answer chosen by Abraham kumar
Akhil AnilAkhil Anil
Hi Abraham,

Please try with the updated snippet below. Please paste the code as it is without making any changes.
 
trigger Contactcallout on Contact (after insert, after update, before delete) {
  Map<Id, String> m = new Map<Id, String>();
  list<Contact> validContacts = new list<Contact>();
  set<ID> accIds = new set<ID>();
  if(Trigger.isDelete)
  {
        for (contact c : Trigger.old) 
        {
            if(c.RecordTypeId == '012D7865456BaFA' && c.Reg__c == TRUE)
            {
                validContacts.add(c);
                accIds.add(c.accountid);
                
            }   
        }
  }
  else
  {
    for (contact c : Trigger.new)
    {
     if(c.RecordTypeId == '012D7865456BaFA' && c.Reg__c == TRUE)
	 {
       if(Trigger.isUpdate)
	   {
         contact old = Trigger.oldMap.get(c.Id);
         if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID||c.Reg__c == TRUE)
         {
             validContacts.add(c);
             accIds.add(c.accountid);
         }
       }
	   else
	   {
              validContacts.add(c);
              accIds.add(c.accountid);
       }
      }   
    }
  }

  map<ID, Account> accMap;
  if(!accIds.IsEmpty()) // guard condition for SOQL
    accMap = new map<ID, Account>([select name from account where id in :accIds]);

   for (contact c : validContacts) {
     Account acc = accMap.get(c.AccountID);
     string accName = acc == null ? null : acc.Name;
     WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.phone,c.Title__c,accName,c.status__c);
   }
}

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help you
 
trigger Contactcall on Contact (after insert, after update , before delete ) 
{
	Map<Id, String> m = new Map<Id, String>();
	list<Contact> validContacts = new list<Contact>();
	set<ID> accIds = new set<ID>();
	
	if(Trigger.isDelete)
	{
		for (contact c : Trigger.old) 
		{
			if(c.RecordTypeId == '012D7865454BaFA' && c.Reg__c == TRUE)
			{
				validContacts.add(c);
				accIds.add(c.accountid);
			}	
		}
	}
	else
	{
		for (contact c : Trigger.new) 
		{
			if(c.RecordTypeId == '012D7865454BaFA' && c.Reg__c == TRUE){
			if(Trigger.isUpdate){
				contact old = Trigger.oldMap.get(c.Id);
				if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID)
				 {
					 validContacts.add(c);
						accIds.add(c.accountid);
				 }
				 }else{
				 validContacts.add(c);
						accIds.add(c.accountid);
				 }   
		}
	}
		map<ID, Account> accMap;
		if(!accIds.IsEmpty()) // guard condition for SOQL
			accMap = new map<ID, Account>([select name from account where id in :accIds]);

		for (contact c1 : validContacts) 
		{
			Account acc = accMap.get(c.AccountID);
			string accName = acc == null ? null : acc.Name;
			WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.phone,c.Title__c,accName,c.status__c);
					
			
		}
	}
}
Please let us know if this will help you
Thanks
Amit Chaudhary
 
Abraham kumarAbraham kumar
HI Amit,

Thank you soo much for helping me again..

Im getting this below error on deleting
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 Contactcallout caused an unexpected exception, contact your administrator: Contactcallout: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Contactcallout: line 16, column 1".

Many Thanks
Abraham
Akhil AnilAkhil Anil
Hi Abraham,

Can you try Amit's snippet with the "after delete" event, keeping the rest of the code as it is.
 
Abraham kumarAbraham kumar

Thanks Akhil...But still the same error

Thanks
Abraham

Akhil AnilAkhil Anil

Are you sure that you are getting the error from this trigger itself, becasue the error message states that the error is in the "
trigger Contactcallout" whereas the trigger you posted here is named trigger Contactcall.

 

Abraham kumarAbraham kumar
Hi Akhil,

Yes it is both the same

Thanks
Abraham 
Abraham kumarAbraham kumar
Hi Amit,

They both are the same i had just edited the name to Contactcall while posting here. The actual trigger name is contactcallout.

Thanks
Abraham
Abraham kumarAbraham kumar
Hi Amit,

Im still getting the same error.

Thanks
Abraham
Abraham kumarAbraham kumar
trigger Contactcallout on Contact (after insert, after update, before delete) {
Map<Id, String> m = new Map<Id, String>();
list<Contact> validContacts = new list<Contact>();
set<ID> accIds = new set<ID>();
if(Trigger.isDelete)
    {
        for (contact c : Trigger.old) 
        {
            if(c.RecordTypeId == '012D76538675BaFA' && c.Reg__c == TRUE)
            {
                validContacts.add(c);
                if(c.accountid != null)
                {
                accIds.add(c.accountid);
                }
            }   
        }
    }
for (contact c : Trigger.new) {
    if(c.RecordTypeId == '012D76538675BaFA' && c.Reg__c == TRUE){
    if(Trigger.isUpdate){
        contact old = Trigger.oldMap.get(c.Id);
        if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID||c.Reg__c == TRUE)
         {
             validContacts.add(c);
                accIds.add(c.accountid);
         }
         }else{
         validContacts.add(c);
                accIds.add(c.accountid);
         }   
}
map<ID, Account> accMap;
if(!accIds.IsEmpty()) // guard condition for SOQL
    accMap = new map<ID, Account>([select name from account where id in :accIds]);

for (contact c1 : validContacts) {
    Account acc = accMap.get(c.AccountID);
    string accName = acc == null ? null : acc.Name;
    WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.phone,c.Title__c,accName,c.status__c);
            
    
    }
}
}
For the above above code it occurs in Trigger.Contactcallout: line 19, column 1 in this line 
for (contact c : Trigger.new) {


Thanks
Abraham
Abraham kumarAbraham kumar
This is the error on delete action:--

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 Contactcallout caused an unexpected exception, contact your administrator: Contactcallout: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Contactcallout: line 19, column 1". 

Click here to return to the previous page.
Akhil AnilAkhil Anil
Try this code. 
 
trigger Contactcallout on Contact (after insert, after update, before delete) {
Map<Id, String> m = new Map<Id, String>();
list<Contact> validContacts = new list<Contact>();
set<ID> accIds = new set<ID>();
if(Trigger.isDelete)
    {
        for (contact c : Trigger.old) 
        {
            if(c.RecordTypeId == '012D76538675BaFA' && c.Reg__c == TRUE)
            {
                validContacts.add(c);
                if(c.accountid != null)
                {
                accIds.add(c.accountid);
                }
            }   
        }
    }
else
{
   for (contact c : Trigger.new) {
      if(c.RecordTypeId == '012D76538675BaFA' && c.Reg__c == TRUE){
      if(Trigger.isUpdate){
         contact old = Trigger.oldMap.get(c.Id);
        if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID||c.Reg__c == TRUE)
         {
             validContacts.add(c);
                accIds.add(c.accountid);
         }
         }else{
         validContacts.add(c);
                accIds.add(c.accountid);
         }   
   }
}

map<ID, Account> accMap;
if(!accIds.IsEmpty()) // guard condition for SOQL
    accMap = new map<ID, Account>([select name from account where id in :accIds]);

for (contact c1 : validContacts) {
    Account acc = accMap.get(c.AccountID);
    string accName = acc == null ? null : acc.Name;
    WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.phone,c.Title__c,accName,c.status__c);
            
    
    }
}
}

 
Akhil AnilAkhil Anil
Hi Abraham,

You were not including the "else" block in Amit's code, hence the error creeped in.
 
Amit Chaudhary 8Amit Chaudhary 8
Please try below code.
trigger Contactcallout on Contact (after insert, after update, before delete) {
Map<Id, String> m = new Map<Id, String>();
list<Contact> validContacts = new list<Contact>();
set<ID> accIds = new set<ID>();
	if(Trigger.isDelete)
    {
        for (contact c : Trigger.old) 
        {
            if(c.RecordTypeId == '012D76538675BaFA' && c.Reg__c == TRUE)
            {
                validContacts.add(c);
                if(c.accountid != null)
                {
                accIds.add(c.accountid);
                }
            }   
        }
    }
	else
	{
		for (contact c : Trigger.new) {
			if(c.RecordTypeId == '012D76538675BaFA' && c.Reg__c == TRUE){
			if(Trigger.isUpdate){
				contact old = Trigger.oldMap.get(c.Id);
				if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID||c.Reg__c == TRUE)
				 {
					 validContacts.add(c);
						accIds.add(c.accountid);
				 }
				 }else{
				 validContacts.add(c);
						accIds.add(c.accountid);
				 }   
		}
	}
	
map<ID, Account> accMap;
if(!accIds.IsEmpty()) // guard condition for SOQL
    accMap = new map<ID, Account>([select name from account where id in :accIds]);

for (contact c1 : validContacts) {
    Account acc = accMap.get(c.AccountID);
    string accName = acc == null ? null : acc.Name;
    WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.phone,c.Title__c,accName,c.status__c);
            
    
    }
}
}
Problem is that you are not adding proper if else.
Please let us know if this will help you
 
Abraham kumarAbraham kumar
On this Im getting a compile error 
Error: Compile Error: Variable does not exist: c.AccountID at line 42 column 30

This line---> Account acc = accMap.get(c.AccountID);

Thanks
Abraham
Akhil AnilAkhil Anil
Hi Abraham,

The below code should resolve the compilation error.
 
trigger Contactcallout on Contact (after insert, after update, before delete) {
Map<Id, String> m = new Map<Id, String>();
list<Contact> validContacts = new list<Contact>();
set<ID> accIds = new set<ID>();
if(Trigger.isDelete)
    {
        for (contact c : Trigger.old) 
        {
            if(c.RecordTypeId == '012D76538675BaFA' && c.Reg__c == TRUE)
            {
                validContacts.add(c);
                if(c.accountid != null)
                {
                accIds.add(c.accountid);
                }
            }   
        }
    }
else
{
   for (contact c : Trigger.new) {
      if(c.RecordTypeId == '012D76538675BaFA' && c.Reg__c == TRUE){
      if(Trigger.isUpdate){
         contact old = Trigger.oldMap.get(c.Id);
        if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID||c.Reg__c == TRUE)
         {
             validContacts.add(c);
                accIds.add(c.accountid);
         }
         }else{
         validContacts.add(c);
                accIds.add(c.accountid);
         }   
   }
}

map<ID, Account> accMap;
if(!accIds.IsEmpty()) // guard condition for SOQL
    accMap = new map<ID, Account>([select name from account where id in :accIds]);

for (contact c1 : validContacts) {
    Account acc = accMap.get(c1.AccountID);
    string accName = acc == null ? null : acc.Name;
    WebServiceCallout.sendNotification(c1.Id,c1.Email,c1.FirstName,c1.LastName,c1.phone,c1.Title__c,accName,c1.status__c);
            
    
    }
}
}



 
Abraham kumarAbraham kumar
Hi Akhil

No i did not work.. but changing it as below saves the code but it is not making the callout on delete action. It is not calling sendnotification method
It is only deleting
 
trigger Contactcallout on Contact (after insert, after update, before delete) {
Map<Id, String> m = new Map<Id, String>();
list<Contact> validContacts = new list<Contact>();
set<ID> accIds = new set<ID>();
if(Trigger.isDelete)
    {
        for (contact c : Trigger.old) 
        {
            if(c.RecordTypeId == '012D76538675BaFA' && c.Reg__c == TRUE)
            {
                validContacts.add(c);
                if(c.accountid != null)
                {
                accIds.add(c.accountid);
                }
            }   
        }
    }
else
{
   for (contact c : Trigger.new) {
      if(c.RecordTypeId == '012D76538675BaFA' && c.Reg__c == TRUE){
      if(Trigger.isUpdate){
         contact old = Trigger.oldMap.get(c.Id);
        if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID||c.Reg__c == TRUE)
         {
             validContacts.add(c);
                accIds.add(c.accountid);
         }
         }else{
         validContacts.add(c);
                accIds.add(c.accountid);
         }   
   }
}

map<ID, Account> accMap;
if(!accIds.IsEmpty()) // guard condition for SOQL
    accMap = new map<ID, Account>([select name from account where id in :accIds]);

for (contact c : validContacts) {
    Account acc = accMap.get(c.AccountID);
    string accName = acc == null ? null : acc.Name;
    WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.phone,c.Title__c,accName,c.status__c);
            
    
    }
}
}

Any Suggestions. Please let me know

Thanks
Abraham
Abraham kumarAbraham kumar
Hi Amit,

Below is the code which compiles successfully and works on insert and update but does not trigger in delete alone. PLease can you help me with any  suggestions 
trigger Contactcallout on Contact (after insert, after update, before delete) {
Map<Id, String> m = new Map<Id, String>();
list<Contact> validContacts = new list<Contact>();
set<ID> accIds = new set<ID>();
if(Trigger.isDelete)
    {
        for (contact c : Trigger.old) 
        {
            if(c.RecordTypeId == '012D7865456BaFA' && c.Reg__c == TRUE)
            {
                validContacts.add(c);
                accIds.add(c.accountid);
                
            }   
        }
    }
    else
    {
for (contact c : Trigger.new) {
    if(c.RecordTypeId == '012D7865456BaFA' && c.Reg__c == TRUE){
    if(Trigger.isUpdate){
        contact old = Trigger.oldMap.get(c.Id);
        if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID||c.Reg__c == TRUE)
         {
             validContacts.add(c);
                accIds.add(c.accountid);
         }
         }else{
         validContacts.add(c);
                accIds.add(c.accountid);
                }
         }   
}
map<ID, Account> accMap;
if(!accIds.IsEmpty()) // guard condition for SOQL
    accMap = new map<ID, Account>([select name from account where id in :accIds]);

for (contact c : validContacts) {
    Account acc = accMap.get(c.AccountID);
    string accName = acc == null ? null : acc.Name;
    WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.phone,c.Title__c,accName,c.status__c);
              }
}
}

Many Thanks in advance
Abraham
Akhil AnilAkhil Anil
Hi Abraham,

Please try with the updated snippet below. Please paste the code as it is without making any changes.
 
trigger Contactcallout on Contact (after insert, after update, before delete) {
  Map<Id, String> m = new Map<Id, String>();
  list<Contact> validContacts = new list<Contact>();
  set<ID> accIds = new set<ID>();
  if(Trigger.isDelete)
  {
        for (contact c : Trigger.old) 
        {
            if(c.RecordTypeId == '012D7865456BaFA' && c.Reg__c == TRUE)
            {
                validContacts.add(c);
                accIds.add(c.accountid);
                
            }   
        }
  }
  else
  {
    for (contact c : Trigger.new)
    {
     if(c.RecordTypeId == '012D7865456BaFA' && c.Reg__c == TRUE)
	 {
       if(Trigger.isUpdate)
	   {
         contact old = Trigger.oldMap.get(c.Id);
         if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID||c.Reg__c == TRUE)
         {
             validContacts.add(c);
             accIds.add(c.accountid);
         }
       }
	   else
	   {
              validContacts.add(c);
              accIds.add(c.accountid);
       }
      }   
    }
  }

  map<ID, Account> accMap;
  if(!accIds.IsEmpty()) // guard condition for SOQL
    accMap = new map<ID, Account>([select name from account where id in :accIds]);

   for (contact c : validContacts) {
     Account acc = accMap.get(c.AccountID);
     string accName = acc == null ? null : acc.Name;
     WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.phone,c.Title__c,accName,c.status__c);
   }
}

 
This was selected as the best answer