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
Developer.mikie.Apex.StudentDeveloper.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;
 
}

}
Best Answer chosen by Developer.mikie.Apex.Student
Elie.RodrigueElie.Rodrigue
Wow I was off track! 

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

AshlekhAshlekh

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 .
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
I tried to save it, however there was an error on !Trigger.oldMap.containKey(c.id))
Elie.RodrigueElie.Rodrigue
It should be !Trigger.oldMap.containsKey(c.id)) 
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
It now allowed me to save, but upon manually testing I received this error when trying to add a contact to an account:

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;
 
}

}
Elie.RodrigueElie.Rodrigue
I didnt had a proper read of your trigger at first. 
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
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
The trigger does not seem to change the Account name anymore
Elie.RodrigueElie.Rodrigue
Wow I was off track! 

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;
    }

}

}
This was selected as the best answer
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Thank you so much, it works and tests perfectly!!! You are a legend.