Activity › Forums › Salesforce® Discussions › Write a Salesforce Trigger for account name should be a combination of lastnames of all its related contacts
Tagged: Account Detail, Contact Record, Record Processing, Salesforce Customization, Salesforce Trigger
-
Write a Salesforce Trigger for account name should be a combination of lastnames of all its related contacts
Posted by Raghav Chaturvedi on June 22, 2017 at 6:58 AMWrite a trigger for account name should be a combination of lastnames of all its related contacts. eg. if I have two contacts 1. test name 2. test name2 in an account. In this case account name should be “name name2”. If I add a new contact “test name3” to this account, now account name should be “name name2 name3
Raghav Chaturvedi replied 8 years, 10 months ago 2 Members · 9 Replies -
9 Replies
- [adinserter block='9']
-
sir in your trigger account name is updated by contact last name but i want the combination of both account name and contact last name
-
trigger contactlastName on Contact (after insert,after update)
{
list lst=new list();
for(contact c:Trigger.new)
{
lst.add(c.accountId);
}
list lac=new list();
list lacc=[select Id,Name,(select Id,lastName from contacts) from account where Id in:lst];
for(account a:lacc)
{
for(contact con: a.contacts)
{
a.Name=a.Name+’ ‘+ con.lastName;
lac.add(a);
}
}
update lac;
}this trigger works only one time,after run this trigger second related contact is not created,plz help
-
I see that this is not mentioned this (account name should be combination of both account name and contact last name) in your first post here. Please make sure you have your requirement clear in the first instance.
So your Account name should be “AccountName ContactLastName1 ContactLastName2 ContactLastName3 and so on” ? Confirm and then I will update the code and post.
-
Based on the assumption in my previous post, updated the code here..UpdateAccountNameFromContactLastName2
This will work in all scenario – Insert, Update, Delete, Undelete of a Contact on an Account.
-
This reply was modified 8 years, 10 months ago by
Sudhir.
-
This reply was modified 8 years, 10 months ago by
-
yes in a account(Raghav),if we have 1 contact(lastNamme=kumar) then account name should we updated by(Raghav Kumar) and again we add a contact(lastnam=sharma)then account nae should we raghav kumar sharma and so on.
this is my question
Log In to reply.