In was able to write code for Insert and update but, not able to find a way for the delete operation. Could you please assist ?
//Below is the code
trigger ABCTrigger on Contact (after insert,after update,after delete)
{
List<Account> acclst = new List<Account>();
for(Contact con : trigger.new)
{
if(con.Birthdate != null)
{
Account acc = new Account();
acc.id = con.AccountId;
acc.Birth_Date__c = con.Birthdate;
acclst.add(acc);
}
}
update acclst;
for(Contact con : trigger.new)
{
if(con.Birthdate != null)
{
Account acc = new Account();
acc.id = con.AccountId;
acc.Birth_Date__c = con.Birthdate;
acclst.add(acc);
}
}
}
42 respuestas
Close, but now you're populating the set BEFORE you've instantiated it. In programming, the order of operations really has to be exact - especially when it comes to memory management.
Like in a recipe, pretend youre making a cake and the recipe says to put the tray in the oven, and only after to add the eggs. Yes, both are correct steps - but the order matters :)