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 

Trigger to update account's custom field with contacts mailing countries

Shivam Yadav 8Shivam Yadav 8
Hi Adi,

I think your quesion is not valid. Because there can be multiple contacts related to single account.So we need to find out which contact you need to use.
sowmya Inturi 9sowmya Inturi 9
Hi Adi, 
If your requirement is adding all the contact mailing country names to the account custom field, this might help you.

trigger UpdateContact on Contact (after insert,after delete,after update) { 
    if(trigger.isInsert ||trigger.isUpdate)
    {
        
        List<Account> ac=new List<Account>();
        
        for(Contact con:trigger.new)
        {
            if(con.MailingCountry<>null)
            {
                Account act=new Account();
                act.Id=con.Accountid;
                ac.add(act);
            }
        }
        System.debug('ac'+ac);
        //    List<Account> acout=[Select Id,Mailing_Countries__c,(Select id,MailingCountry from Contacts) from Account where id in: ac];
        
        List<Account> ac_Update=new List<Account>();
        for(Account act:[Select Id,Mailing_Countries__c,(Select id,MailingCountry from Contacts) from Account where id in: ac])
        {
            if(act.Contacts.size()>0){
                for(Contact c:act.Contacts){
                    System.debug('In Contacts for');
                    if(c.MailingCountry!=null){
                        System.debug('In Contacts mailing Country for');
                        if(act.Mailing_Countries__c!=null){
                            if(!act.Mailing_Countries__c.Contains(c.MailingCountry) ){
                                System.debug('In Contains');
                                act.Mailing_Countries__c =act.Mailing_Countries__c +' '+ c.MailingCountry;
                                System.debug('act.Mailing_Countries__c'+act.Mailing_Countries__c);
                                ac_Update.add(act);
                            }else {
                                act.Mailing_Countries__c =act.Mailing_Countries__c;
                                ac_Update.add(act);
                            }
                        }
                        else{
                            System.debug('In Else Mailing Countries in Acc');
                          act.Mailing_Countries__c =  c.MailingCountry;
                            ac_Update.add(act);
                           System.debug('c.MailingCountry'+c.MailingCountry); 
                           System.debug('act.Mailing_Countries__c'+act.Mailing_Countries__c); 
                            
                        }
                    }
                }
            }
        }
        System.debug('ac_Update'+ac_Update);
        update ac_Update;    
    }
    if(trigger.isDelete)
    {
        List<Account> ac_list=new List<Account>();  
        List<String> CountryList = new List<String>();
        for(Contact con:trigger.old)
        {
            if(con.accountid<>null && con.MailingCountry!=null)
            {
                Account act=new Account();
                act.Id=con.Accountid;
                ac_list.add(act);
                CountryList.add(con.MailingCountry);
            }
        }
        
        //    List<Account> acout=[Select Id,Mailing_Countries__c,(Select id from Contacts) from Account where id in: ac_list];
        
        List<Account> ac_Up=new List<Account>();
        for(Account ac:[Select Id,Mailing_Countries__c,(Select id,MailingCountry from Contacts) from Account where id in: ac_list])
        {
            if(ac.contacts.size()==0)
            {
                System.debug('In if delete');
                ac.Mailing_Countries__c='';
                ac_Up.add(ac);
            }
            else{
                for(String s:CountryList){
                    System.debug('In Delete Else');
                    if(s!=null){
                        System.debug('s'+s);
                        //  if(!act.Mailing_Countries__c.Contains(c.MailingCountry)){
                        ac.Mailing_Countries__c =ac.Mailing_Countries__c.remove(s);
                        System.debug('ac.Mailing_Countries__c'+ac.Mailing_Countries__c);
                        ac_Up.add(ac);
                        //  }else{
                        //      act.Mailing_Countries__c =act.Mailing_Countries__c;
                        //    ac_Update.add(act);
                        // }
                    }
                }
            }
            
        }
        System.debug('ac_Up'+ac_Up);
        update ac_Up;
    }
}


Thanks,
Sowmya.