function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
srikanth gubbala 1srikanth 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;
}
Best Answer chosen by srikanth gubbala 1
mukesh guptamukesh gupta
Hi Srikanth,

Please follow 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>;

  Map<Id, Account> accMap = new Map<Id,Account>([Select id, Contact_Names__c From Account Where Id In : accIdList])
  
  for(Contact con : Trigger.New){
      if(con.LastName != null){
	     Account acc = new Account(Id = con.AccountId);
		 if(acc.Contact_Names__c == ''){
		   acc.Contact_Names__c = con.LastName;
		 }else{
		   acc.Contact_Names__c += ', '+con.LastName;
		 }
		
        accMap.put(con.AccountId, acc);
      }
    }
  update accMap.values();
}


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

srikanth gubbala 1srikanth gubbala 1
The Requirement here is to copy all account related contacts and update on Account field (long text ) Contact_Names__c 

 
mukesh guptamukesh gupta
Hi Srikanth,

Please follow 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>;

  Map<Id, Account> accMap = new Map<Id,Account>([Select id, Contact_Names__c From Account Where Id In : accIdList])
  
  for(Contact con : Trigger.New){
      if(con.LastName != null){
	     Account acc = new Account(Id = con.AccountId);
		 if(acc.Contact_Names__c == ''){
		   acc.Contact_Names__c = con.LastName;
		 }else{
		   acc.Contact_Names__c += ', '+con.LastName;
		 }
		
        accMap.put(con.AccountId, acc);
      }
    }
  update accMap.values();
}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
This was selected as the best answer