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
DeafForceDeafForce 

Retrieve email addresses from primary contacts of account

Hello everyone,

 

All our accounts have one or several "primary" (simply checkbox) contacts related to to them. 

I want to retrieve all those emails and gather them in a long text area field on the account. I found some sample code which retrieves the email but only for the first contact it finds.

 

Little bit of background:

Whenever someone opens a ticket related to this account we want the email addresses of all primary contacts automatically populated into the "additional to" section. I got this part sorted but can`t seems to get the email addresses into the account.

 

Do you have some guidance on how to retrieve all emails ?

 

Code I found:

trigger RetrieveEmails on Account (before update) {
      for (Account acc : Trigger.new) {
           Contact[]contactR = [select ID, Email from Contact where Primary_Contact__c = true and AccountId= :acc.id ORDER BY Primary_Contact__c DESC, createdDate limit 1];

 

        if (contactR.size() > 0) {

             acc.Description = contactR[0].Email;
              }else{
             acc.description = 'DEBUG; CONTACT = null ';
}
}}

 

Thanks very much in advance!

Christian

Best Answer chosen by Admin (Salesforce Developers) 
Dhaval PanchalDhaval Panchal

Try this.

trigger RetrieveEmails on Account (before update) {
	Set<ID> setAccountId = new Set<ID>();
	for(Account acc: Trigger.New){
		setAccountId.add(acc.Id);
	}
	if(setAccountId.size()>0){
		List<Contact> lstCon = [Select Id, AccountId, Email From Contact where Primary_Contact__c=true and AccountId in:setAccountId 
		ORDER BY Primary_Contact__c DESC, createdDate];
		Map<ID, List<Contact>> mapContact = new Map<ID, List<Contact>>();
		if(lstCon.size()>0){
			for(Id accId:setAccountId){
				for(Contact con:lstCon){
					if(mapContact.containsKey(con.AccountId)){
						mapContact.get(con.AccountId).add(con);
					}
					else{
						List<Contact> lst = new List<Contact>();
						lst.add(con);
						mapContact.put(con.AccountId,lst);
					}
				}
			}
			if(mapContact.size()>0){
				for(Account acc: Trigger.New){
					if(mapContact.containsKey(acc.Id)){
						acc.AdditionalTo__c = '';
						for(Contact con:mapContact.get(acc.Id)){
							if(acc.AdditionalTo__c == ''){
								acc.AdditionalTo__c = con.Email;
							}
							else{
								acc.AdditionalTo__c += ', ' + con.Email;
							}
						}
					}
				}
			}
		}
		
	}
}

 

All Answers

Dhaval PanchalDhaval Panchal

Try this.

trigger RetrieveEmails on Account (before update) {
	Set<ID> setAccountId = new Set<ID>();
	for(Account acc: Trigger.New){
		setAccountId.add(acc.Id);
	}
	if(setAccountId.size()>0){
		List<Contact> lstCon = [Select Id, AccountId, Email From Contact where Primary_Contact__c=true and AccountId in:setAccountId 
		ORDER BY Primary_Contact__c DESC, createdDate];
		Map<ID, List<Contact>> mapContact = new Map<ID, List<Contact>>();
		if(lstCon.size()>0){
			for(Id accId:setAccountId){
				for(Contact con:lstCon){
					if(mapContact.containsKey(con.AccountId)){
						mapContact.get(con.AccountId).add(con);
					}
					else{
						List<Contact> lst = new List<Contact>();
						lst.add(con);
						mapContact.put(con.AccountId,lst);
					}
				}
			}
			if(mapContact.size()>0){
				for(Account acc: Trigger.New){
					if(mapContact.containsKey(acc.Id)){
						acc.AdditionalTo__c = '';
						for(Contact con:mapContact.get(acc.Id)){
							if(acc.AdditionalTo__c == ''){
								acc.AdditionalTo__c = con.Email;
							}
							else{
								acc.AdditionalTo__c += ', ' + con.Email;
							}
						}
					}
				}
			}
		}
		
	}
}

 

This was selected as the best answer
DeafForceDeafForce

Hi Dhaval,

 

Awesome!!! It works like a charm! Thank you so much!!! 

 

Have a great day and thanks again,

Christian