You need to sign in to do that
Don't have an account?
srikanth gubbala 1
Remove Nested loop from below code
trigger ContactNamesOnAccount on Contact (after update, after insert) {
Set<id> accIdList = new Set<id>();
for(Contact 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(Contact 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;
}
Please follow below code:-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
All Answers
Please follow below code:-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh