+ Start a Discussion
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

Trigger which will update the Account name based on the contacts

Hey guys,

I am in need of a trigger which will update my Account name based on the amount of contacts within the account. E.G. An account with ben and jane Samol would make the account name = Samol, Ben and Samol, Jane.

Any example code on how I may acheive this would be much appreciated. Or at the very least a point in the right direction.

Thank you so much for your help
Best Answer chosen by Developer.mikie.Apex.Student
Satish_SFDCSatish_SFDC
Sure. Try this.

<pre>
trigger ContactTrigger on Contact (after insert,after update, after delete, after undelete) {
    Set<ID> setAccountIDs = new Set<ID>();
    for(Contact c : Trigger.new){
        setAccountIDs.add(c.AccountId);
    }
  
    List<Account> accounts = [Select ID, Name,(Select Name From Contacts)  From Account WHERE ID IN :setAccountIDs];
    for(Account a : accounts){
        String accName = '';
        for(Contact c : a.Contacts){
            accName +=c.Name+ ' ';                      
        }
        a.Name=accName;
    }
    update accounts;
  
}
</pre>

Test it first and make changes as necessary.

Regards,
Satish Kumar

All Answers

Satish_SFDCSatish_SFDC
You will have to create a before or after update trigger on contacts and update the related accounts everytime the record is updated or inserted. 
Also you may have a before delete and after undelete trigger to ensure the code works for deletions and undeletions as well.

Regards,
Satish Kumar
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Would you possibly be able to assist me with some example code?
Satish_SFDCSatish_SFDC
Sure. Try this.

<pre>
trigger ContactTrigger on Contact (after insert,after update, after delete, after undelete) {
    Set<ID> setAccountIDs = new Set<ID>();
    for(Contact c : Trigger.new){
        setAccountIDs.add(c.AccountId);
    }
  
    List<Account> accounts = [Select ID, Name,(Select Name From Contacts)  From Account WHERE ID IN :setAccountIDs];
    for(Account a : accounts){
        String accName = '';
        for(Contact c : a.Contacts){
            accName +=c.Name+ ' ';                      
        }
        a.Name=accName;
    }
    update accounts;
  
}
</pre>

Test it first and make changes as necessary.

Regards,
Satish Kumar
This was selected as the best answer
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Thank you soo much for your help Satish you are a legend.

Works pretty well so far, whenever I create another contact it triggers and changes the account name. However, it now prevents me from deleting contacts.

I was also wondering what would I change:

for(Contact c : a.Contacts){

            accName +=c.Name+ ' ';                     

        }

        a.Name=accName;

that part to in order to make it so that it will paste the contacts last name and then first name with an "and" between each contact. However, not at the end?

For example: I could put a comma here -  accName +=c.Name+ ', ';            but it changes the name like this: Ted Ladel, Kellie Scalter, Perry Matthew, Chicken pie, (with a comma on the end.

What would I have to do in order to change it to this format: Ladel, Ted and Sclater, Kellie and Matthew, Perry and Pie, Chicken (with no comma or and on the end and the last name first).

Thank you so much for your help. You have really helped me.
Satish_SFDCSatish_SFDC
Sorry, Could not get time to check this. 
Did you have this resolved?

Regards,
Satish Kumar
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Hey Satish,

I sort of got it working....when it comes to adding contacts it does it perfectly...But because of the syntax the first contact added screws up the account name:

For example: Should an account have one contact being Jimmy mcnails, then the account would be called and Mcnails, Jimmy

This is the trigger:

trigger ContactTrigger on Contact (after insert,after update, after delete, after undelete) {
    Set<ID> setAccountIDs = new Set<ID>();
    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 = '';
        for(Contact c : a.Contacts){
        accName +=' and '+c.LastName+', '+c.FirstName;                     
        }
        a.Name=accName;
    }
    update accounts;
 
}

Is it possible to change it in a way that says, when the first contact is in that becomes the account name and as soon as there is two contacts it starts adding the and LastName, FirstName?

Thank you do muvh for your help Satish.