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
Pushkar GuptaPushkar Gupta 

hi, trigger on account to update contact information

 i have a question regarding trigger. I have two custom field on Account namely Country And State., And same field on Contact Whenever i choose the country on contact state get automatically filled using account state field if the country is same in account field.
Ajay K DubediAjay K Dubedi
Hi Pushkar,

Please run this code.

//Apex Helper class//
public class automaticallyFillContactState {
   public static void contactState(map<Id,Account> newAccMap){
        set <id> IdCollect = newAccMap.keySet();
        List<Contact> toUpdateconList = new List<Contact>();
        if(IdCollect.size() > 0){
            List <Contact> conList = [SELECT Country__c,AccountId From Contact WHERE AccountId IN :IdCollect];
            if(conList.size()>0){
                for(Contact c:conList){
                    if(c.Country__c==newAccMap.get(c.AccountId).Country__c){
                        c.State__c = newAccMap.get(c.AccountId).State__c;
                        toUpdateconList.add(c);
                    }
                }
            }
            if(toUpdateconList.size()>0){
                update toUpdateconList;            
            }
        }
    }    
}

//Trigger Class//

trigger automaticallyFillContactStateTrigger on Account ( before update) {
    if(Trigger.isBefore && Trigger.isUpdate){
        automaticallyFillContactState.contactState(Trigger.NewMap);
    }   
}
 
 
Please mark it as best Answer if you find it helpful.

Thank You
Ajay Dubedi
Pushkar GuptaPushkar Gupta
its not working
Akshay_DhimanAkshay_Dhiman
Hi Pushkar,

Please try the below code
 
Apex Trigger :
trigger conTrigger on Contact (before insert,before update) {
    if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isBefore){
        HelpUserAddCopyInConAc.copyUserAdd333(Trigger.New);
    }
}

Trigger Helper class :
public with sharing class HelpUserAddCopyInConAc {
  public static void copyUserAdd333(List<Contact> conList){
        System.debug('Call method');
        Set<Id> accId = new set<Id>();
        List<Account> toAccUpdate = new List<Account>();
        for(Contact c:conList){
            if(c.AccountId!=null){
                accId.add(c.AccountId);
            }
        }
        if(accId.size() > 0){
            map<id,Account> newAcmap = new map<id,Account>([select BillingState,BillingCountry from Account where Id IN:accId]);
            if(!newAcmap.isEmpty()){
                for(Contact c :conList){
                    if(c.mailingCountry==newAcmap.get(c.AccountId).BillingCountry){
                        c.MailingState=newAcmap.get(c.AccountId).BillingState;
                        
                    }
                }
            }
            
        }     
        
    }
}

If you found this answer helpful then please mark it as best answer so it can help others.   
  
  Thanks 
  Akshay
  
Ajay K DubediAjay K Dubedi
Hi Pushkar,

Please run this code.
//Apex Helper class//

public class automaticallyFillContactState {
    public static void contactState(List<Contact> conList){
        set <id> IdCollect = new set <id>();
        For(Contact c :conList){
            If (c.AccountId != null){
                IdCollect.add(c.AccountId);
            }
        }
        List <Account> accounts = [SELECT Id,
                                   State__c,
                                   Country__c
                                   From Account 
                                   WHERE Id IN :IdCollect];
        
        Map <Id, string> conmap = new Map <Id, string>();
        for(Account c : accounts){
            conmap.put(c.Id , c.State__c);
            
        }
        Map <Id, string> conmap1 = new Map <Id, string>();
        for(Account c : accounts){
            
            conmap1.put(c.Id,c.Country__c);
        }       
        if(accounts!=null){
            for(Contact c : conList ){
                if(c.Country__c==conmap1.get(c.AccountId)){
                    c.State__c = conmap.get(c.AccountId);
                }
            }
        }
    } 
}

//Trigger Class//

trigger automaticallyFillContactStateTrigger on Contact (before update) {

    automaticallyFillContactState.contactState(Trigger.New);
}

Please mark it as best Answer if you find it helpful.

Thank You
Ajay Dubedi
Pushkar GuptaPushkar Gupta
Hey Akshay your code works perfectly but i want this trigger when contact has no relation with account
Akshay_DhimanAkshay_Dhiman
Hi Puskar
 try this code to when Account and contact have no relation
 
Apex Trigger :

trigger conTrigger on Contact (before insert,before update) {
    if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isBefore){
        ContactTriggerHandler.handlebeforeInsert(Trigger.New);
    }
}

Trigger Helper class :

public class ContactTriggerHandler{
    public static void handlebeforeInsert(List<Contact> conList){ 
        System.debug('Call Trigger Helper');
        Set <String> country = new Set <String>();
        for(Contact con : conList){
            country.add(con.MailingCountry);
        }
        if(country.size() > 0){
            List<Account> accList =[select Id,BillingCountry,BillingState from Account where  BillingCountry IN:country];
            if(accList.size() > 0){
                for(Contact con :conList) {
                    for(Account acc:accList){
                        if(con.MailingCountry==acc.BillingCountry) {
                            con.MailingState=acc.BillingState; 
                        }
                    }        
                }
            }
        }
    }
}



  if you found this answer helpful then please mark it as best answer so it can help others.   
   
   Thanks 
   Akshay