+ Start a Discussion
Abhishek AS 3Abhishek AS 3 

After update ,delete,inserting contact record update account email field(custom) with contacts email with comma seperated values

Using map in trigger. If account has more than one contact it should display like this. abc@gmail.com,xyz@gmail.com.
CharuDuttCharuDutt
Hii Abhishek
Try Below Code
trigger NumberOfChild on Contact (After Insert,After Update,After Delete) {
List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Contact con : Trigger.new){
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
			}
		}
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Contact con : Trigger.new){ 
            if(con.AccountId!=Trigger.oldMap.get(con.Id).AccountId){
               	setAccIds.add(con.AccountId);
                setAccIds.add(Trigger.oldMap.get(con.Id).AccountId);
            	}
          
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Contact con : Trigger.old) { 
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
        	}
        }
    }    
    for(Account acc :[Select id,Contact_Email__c ,Description ,(Select id,Name,Email from contacts) from Account where Id in : setAccIds]){
      String s ='';
        acc.Total_Contacts__c = acc.contacts.size();
        for(Contact Con :acc.contacts){
            s+=Con.Email+',';
        }
        acc.Contact_Email__c=  s.removeEnd(',');
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList;     
    }
}
Please Mark It As Best Asnwer If It Helps
Thank You!
AnkaiahAnkaiah (Salesforce Developers) 
Hi Abhishek,

try with below code. Replace field API name as per your org.
 
trigger Acountemailupdate on Contact (After Insert,After Update,After Delete) {
List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Contact con : Trigger.new){
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
			}
		}
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Contact con : Trigger.new){ 
            if(con.AccountId!=Trigger.oldMap.get(con.Id).AccountId){
               	setAccIds.add(con.AccountId);
                setAccIds.add(Trigger.oldMap.get(con.Id).AccountId);
            	}
          
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Contact con : Trigger.old) { 
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
        	}
        }
    }    
    for(Account acc :[Select id,Contact_Emails__c ,Description ,(Select id,Name,Email from contacts) from Account where Id in : setAccIds])
{
    List<String> lstSrting = new List<String>();
    for(Contact con : acc.contacts)
    {
        lstSrting.add(con.email);
    }
    acc.Contact_Emails__c = String.join(lstSrting, ',');
	accList.add(acc);
}

        update accList;     
}

If this helps, Please mark it as best answer.

Thanks!!
 
ravi soniravi soni
Hi Abhishek,
try below code.
trigger ContactTriggerV2 on Contact (after insert, after update, after delete) {
    
    if(trigger.isAfter){
        set<string> accIdSet = new set<string>();
        if(trigger.isInsert){
            for(contact con : trigger.new){
                if(con.AccountId != null){
                accIdSet.add(con.AccountId);    
                }
             }
            
        }
        if(trigger.isUpdate){
            for(contact con : trigger.new){
                if(con.AccountId != null){
                accIdSet.add(con.AccountId);    
                accIdSet.add(trigger.oldMap.get(con.Id).AccountId);    
                }
             }
            
        }
        if(trigger.isDelete){
            for(contact con : trigger.old){
                if(con.AccountId != null){
                accIdSet.add(con.AccountId);    
                }
             }
            
        }
        list<Account> lstAccount = new list<Account>();
        for(Account acc : [SELECT Id,Name,Child_Contact_Emails__c,(SELECT Id,Name,Email FROM Contacts WHERE Email != null) FROM Account WHERE Id IN: accIdSet]){
            string sEmail = '';
            for(contact con : acc.contacts){
                sEmail+=con.Email + ',';
              }
            acc.Child_Contact_Emails__c = sEmail.removeEnd(',');
            lstAccount.add(acc);
        }
        
        if(lstAccount.size() > 0){
            update lstAccount;
        }
    }

}

In above code I bold a where condition that you must use. it will restrict contact whose doesn't have email. if you will not use this condition then you will get null in text field.
one thing you will keep in mind that your custom text filed in which you are storing all the child email must be long text area.

let me know by marking it as the best answer.
Thank you