You need to sign in to do that
Don't have an account?
Developer.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
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
<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
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
<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
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.
Did you have this resolved?
Regards,
Satish Kumar
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.