You need to sign in to do that
Don't have an account?
srikanth cheera 13
write a test class for This
trigger ContactNamesOnAccount on Contact (after update, after insert) {
Set<id> accIdList = new Set<id>();
for(Cotact con : Trigger.new){
accIdList.add(con.accountid);
}
List<Account> accUpdateList = new List<Account>();
List<String> names = new List<String>;
for(Account acc : [Select id, Contact_Names__c, (Select LastName From Contacts) From Account Where Id In : accIdList])
{
for(Contact con : acc.contacts){
if(con.LastName != null){
/* add name to list */
names.add(con.LastName);
}
}
/* update name separating ', ' */
acc.Contact_Names__c = String.join(names, ', ');
accUpdateList.add(acc);
/* clear list to add new account contact names */
names.clear();
}
update accUpdateList;
}
Set<id> accIdList = new Set<id>();
for(Cotact con : Trigger.new){
accIdList.add(con.accountid);
}
List<Account> accUpdateList = new List<Account>();
List<String> names = new List<String>;
for(Account acc : [Select id, Contact_Names__c, (Select LastName From Contacts) From Account Where Id In : accIdList])
{
for(Contact con : acc.contacts){
if(con.LastName != null){
/* add name to list */
names.add(con.LastName);
}
}
/* update name separating ', ' */
acc.Contact_Names__c = String.join(names, ', ');
accUpdateList.add(acc);
/* clear list to add new account contact names */
names.clear();
}
update accUpdateList;
}
Raj Vakati
Use this