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
adi salesforceadi salesforce 

show comma seperated unique mailing countries from contacts on related custom field on account

Raj VakatiRaj Vakati
Try this code
 
trigger ContactUpdate on Contact (after insert) {

set<Id> acctIds = new set<Id>();
for(Contact con : trigger.new) {
	acctIds.add(con.AccountId);
}
List<Account> accs = [Select Id ,uniquemailing__c , (Select Id, LastName ,MailingCountry from Contacts) from Account where Id in :acctIds] ;

for(Account a :accs){
	Set<String> cnons = Set<String>() ;
	for(Contact c :a.Contacts){
		cnons.add(c.MailingCountry);
	}
	String strCntry = '';
for(String s:cnons) {
strCntry += (strCntry == ''?'':',')+s;

}
idString = idString.substring(0, idString.length()-1);
a.uniquemailing__c = idString ;



	
}

Update accs;

}