You need to sign in to do that
Don't have an account?
Developer.mikie.Apex.Student
Account Name trigger delete help
Hey guys,
I have a trigger which is designed to change the name of the account based on the contacts that are associated with it. It works perfectly for adding and updating the contacts, however should i delete a contact I have to edit and save one of the remaining contacts associated with the account before the account name will change. How do I make it so that the name will re-evaluate after I delete one of the contacts.
Thank you for your help.
This is my code:
/////////////////Trigger\\\\\\\\\\\\\\\\\\\\\
trigger AccountNameTrigger on Contact (after insert,after update, after delete, after undelete) {
Set<ID> setAccountIDs = new Set<ID>();
if(trigger.isdelete)
{
for(Contact c : Trigger.old)
{
setAccountIds.add(c.Accountid);
}
}else
{
for(Contact c : Trigger.new){
setAccountIDs.add(c.AccountId);
}
List<Account> accounts = [Select ID, Name,(Select FirstName, LastName From Contacts) From Account WHERE ID IN :setAccountIDs];
for(Account a : accounts){
String accName = '';
Boolean hit = false;
for(Contact c : a.Contacts){
if(hit)
{
accName+=' and ';
}
accName += c.FirstName+' '+c.LastName;
hit = true;
}
a.Name=accName;
}
update accounts;
}
}
I have a trigger which is designed to change the name of the account based on the contacts that are associated with it. It works perfectly for adding and updating the contacts, however should i delete a contact I have to edit and save one of the remaining contacts associated with the account before the account name will change. How do I make it so that the name will re-evaluate after I delete one of the contacts.
Thank you for your help.
This is my code:
/////////////////Trigger\\\\\\\\\\\\\\\\\\\\\
trigger AccountNameTrigger on Contact (after insert,after update, after delete, after undelete) {
Set<ID> setAccountIDs = new Set<ID>();
if(trigger.isdelete)
{
for(Contact c : Trigger.old)
{
setAccountIds.add(c.Accountid);
}
}else
{
for(Contact c : Trigger.new){
setAccountIDs.add(c.AccountId);
}
List<Account> accounts = [Select ID, Name,(Select FirstName, LastName From Contacts) From Account WHERE ID IN :setAccountIDs];
for(Account a : accounts){
String accName = '';
Boolean hit = false;
for(Contact c : a.Contacts){
if(hit)
{
accName+=' and ';
}
accName += c.FirstName+' '+c.LastName;
hit = true;
}
a.Name=accName;
}
update accounts;
}
}
try this out, its just a matter of using the account ids from the delete trigger, you were just adding them to a list without processing anything.
trigger AccountNameTrigger on Contact (after insert,after update, after delete, after undelete) {
Set<ID> setAccountIDs = new Set<ID>();
if(trigger.isdelete)
{
for(Contact c : Trigger.old)
{
setAccountIds.add(c.Accountid);
}
}
else
{
for(Contact c : Trigger.new){
setAccountIDs.add(c.AccountId);
}
}
List<Account> accounts = [Select ID, Name,(Select FirstName, LastName From Contacts) From Account WHERE ID IN :setAccountIDs];
for(Account a : accounts){
String accName = '';
Boolean hit = false;
for(Contact c : a.Contacts){
if(hit)
{
accName+=' and ';
}
accName += c.FirstName+' '+c.LastName;
hit = true;
}
a.Name=accName;
}
if(accounts.size()>0)
{
update accounts;
}
}
}
All Answers
I think this will help you....
Just replace your "for loop code which is on Account records" with below for loop code :
for(Account a : accounts)
{
String accName = '';
Boolean hit = false;
for(Contact c : a.Contacts){
//if(hit)
//{
// accName+=' and ';
// }
if(!Trigger.oldMap.containKey(c.id))
{
accName += accName==''?c.FirstName+' '+c.LastName:'and '+c.FirstName+' '+c.LastName;
}
/hit = true;
}
a.Name=accName !=''?accName:'No contact found';
}
Best Regards,
Ashlekh Gera,
Salesforce Certified Developer .
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger AccountNameTrigger caused an unexpected exception, contact your administrator: AccountNameTrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.AccountNameTrigger: line 29, column 1
This is my cod:
trigger AccountNameTrigger on Contact (after insert,after update, after delete, after undelete) {
Set<ID> setAccountIDs = new Set<ID>();
if(trigger.isdelete)
{
for(Contact c : Trigger.old)
{
setAccountIds.add(c.Accountid);
}
}else
{
for(Contact c : Trigger.new){
setAccountIDs.add(c.AccountId);
}
List<Account> accounts = [Select ID, Name,(Select FirstName, LastName From Contacts) From Account WHERE ID IN :setAccountIDs];
for(Account a : accounts)
{
String accName = '';
Boolean hit = false;
for(Contact c : a.Contacts){
//if(hit)
//{
// accName+=' and ';
// }
if(!Trigger.oldMap.containsKey(c.id))
{
accName += accName==''?c.FirstName+' '+c.LastName:'and '+c.FirstName+' '+c.LastName;
}
hit = true;
}
a.Name=accName !=''?accName:'No contact found';
}
update accounts;
}
}
When you are doing Trigger.oldMap, you need not to be in an insert trigger as oldMap will be null.
So you could replace if(!Trigger.oldMap.containsKey(c.id))
by if(Trigger.oldMap == null || !Trigger.oldMap.containsKey(c.id))
Or you could also using Trigger.isInsert
try this out, its just a matter of using the account ids from the delete trigger, you were just adding them to a list without processing anything.
trigger AccountNameTrigger on Contact (after insert,after update, after delete, after undelete) {
Set<ID> setAccountIDs = new Set<ID>();
if(trigger.isdelete)
{
for(Contact c : Trigger.old)
{
setAccountIds.add(c.Accountid);
}
}
else
{
for(Contact c : Trigger.new){
setAccountIDs.add(c.AccountId);
}
}
List<Account> accounts = [Select ID, Name,(Select FirstName, LastName From Contacts) From Account WHERE ID IN :setAccountIDs];
for(Account a : accounts){
String accName = '';
Boolean hit = false;
for(Contact c : a.Contacts){
if(hit)
{
accName+=' and ';
}
accName += c.FirstName+' '+c.LastName;
hit = true;
}
a.Name=accName;
}
if(accounts.size()>0)
{
update accounts;
}
}
}