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
Aladdin USAladdin US 

whenever status field in account is updated automatically status field in contact also updated with the same value and also vice versa

SandhyaSandhya (Salesforce Developers) 
Hi,

You can try by modifying below sample code which is for contact phone number change.
trigger updateContactsFromAccount on Account (after update) {
  Contact[] updatedContacts = new Contact[0];
  Account[] changedAccounts = new Account[0];
  for(account a:Trigger.new) {
    if(a.phone != trigger.oldmap.get(a.id).phone ||
       a.fax != trigger.oldmap.get(a.id).fax ||
       a.billingstreet != trigger.oldmap.get(a.id).billingstreet || 
       a.billingcity != trigger.oldmap.get(a.id).billingcity ||
       a.billingstate != trigger.oldmap.get(a.id).billingstate || 
       a.billingpostalcode != trigger.oldmap.get(a.id).billingpostalcode ||
       a.billingcountry != trigger.oldmap.get(a.id).billingcountry) {
      changedAccounts.add(a);
    }
  }
  for(Contact c:[select id,accountid,phone,mailingstreet,mailingcity,mailingpostalcode,mailingcountry,mailingstate from contact where accountid in :trigger.new]) {
    account a = trigger.newmap.get(c.accountid);
    boolean updatedrecord = false;
    if(c.phone == trigger.oldmap.get(c.accountid).phone) {
      c.phone = a.phone;
      updatedrecord = true;
    }
    if(c.fax == trigger.oldmap.get(c.accountid).fax) {
      c.fax = a.fax;
      updatedrecord = true;
    }
    if(c.mailingstreet == trigger.oldmap.get(c.accountid).billingstreet) {
      c.mailingstreet = a.billingstreet;
      updatedrecord = true;
    }
    if(c.mailingcity == trigger.oldmap.get(c.accountid).billingcity) {
      c.mailingcity = a.billingcity;
      updatedrecord = true;
    }
    if(c.mailingstate == trigger.oldmap.get(c.accountid).billingstate) {
      c.mailingstate = a.billingstate;
      updatedrecord = true;
    }
    if(c.mailingpostalcode == trigger.oldmap.get(c.accountid).billingpostalcode) {
      c.mailingpostalcode = a.billingpostalcode;
      updatedrecord = true;
    }
    if(c.mailingcountry == trigger.oldmap.get(c.accountid).billingcountry) {
      c.mailingcountry = a.billingcountry;
      updatedrecord = true;
    }
    if(updatedrecord) {
      updatedcontacts.add(c);
    }
  }
  update updatedcontacts;
}

Best Regards,
Sandhya
Sonali_takkeSonali_takke
Hi,

Please Try below code


//TRIGGER TO UPDATE ACCOUNT IF CONTACT STATUS CHANGES

trigger UpdateAccount on Contact (After insert,After Update) {
    public static Boolean isFirstTime = true;
    if(isFirstTime){
    isFirstTime = False;    
    List<Account> accList = new List<Account>();
  
    For(Contact con:[SELECT id,Status__c,Account.Id,Account.Status__c FROM Contact Where Id In:Trigger.new]){ 
        
        if(con.Status__c == con.Account.Status__c || trigger.oldmap.get(con.Id).Status__c  == con.Status__c){
            System.debug('==========================');
        }
        else{
           System.debug('Updating status on Account');
           con.Account.Status__c = con.Status__c;
           accList.add(con.Account); 
        }
    }
    System.debug('Account List'+accList);
    update accList;
    System.debug('UPdated Account Map'+accList);
    
    }

}

//TRIGGER TO UPDATE CONTACT IF ACCOUNT STATUS CHANGES

trigger UpdateContact on Account (After insert,After Update) {
    public static Boolean isFirstTime = true;
    List<Contact> conList = new List<Contact>();
    if(isFirstTime){
        For(Account acc:[SELECT id,Status__c,(SELECT id,Status__c FROM Contacts) FROM Account WHERE ID IN:Trigger.new]){
            for(Contact con:acc.Contacts){
             if(trigger.oldmap.get(acc.Id).Status__c  == acc.Status__c || con.Status__c == acc.Status__c){
                 System.debug('==========================');
                }
            else{
                System.debug('Updating status on Contact');
                   con.Status__c = acc.Status__c;
                   conList.add(con);  
                }   
            }
             System.debug('Contact List'+conList);
               update conList;
             System.debug('UPdated Contact Map'+conList);
                
        } 
        
    }

}
Sonali_takkeSonali_takke
Hi,
Requirement is  : whenever status field in account is updated to open itd associted contact also updated with the same value and vice versa Right?

Code Covers this scenario..

 
Sonali_takkeSonali_takke
Hi,

I have updated the code please try this one..

trigger UpdateAccount on Contact (After insert,After Update) {
    public static Boolean isFirstTime = true;
    if(isFirstTime){
    isFirstTime = False;    
    List<Account> accList = new List<Account>();
    List<Contact> conList = new List<Contact>();
  
    For(Contact con:[SELECT id,Status__c,Account.Id,Account.Status__c FROM Contact Where Id In:Trigger.newMap.keyset()]){ 
        
        if(Account.Status__c != NULL && con.Status__c == con.Account.Status__c ){
            if(Trigger.isupdate && trigger.oldmap.get(con.Id).Status__c  == con.Status__c){
                System.debug('===========UPDATE===============');
            }
            System.debug('=========INSERT=================');
          }
        else{
           System.debug('Updating status on Account');
            if(con.Status__c != NULL){
                   con.Account.Status__c = con.Status__c;
                 accList.add(con.Account); 
            }
            else{
                Contact conObj = new Contact(id=con.id);
                conObj.Status__c = con.Account.Status__c;
                conList.add(conObj);
             }
        }
    }
    System.debug('Account List'+accList);
    update accList;
    update conList;
    System.debug('UPdated Account Map'+accList);
    
    }

}



trigger UpdateContact on Account (After insert,After Update) {

    public static Boolean isFirstTime = true;
    List<Contact> conList = new List<Contact>();
    
    if(isFirstTime){
        For(Account acc:[SELECT id,Status__c,(SELECT id,Status__c FROM Contacts) FROM Account WHERE ID IN:Trigger.newMAp.keyset()]){
            if(acc!=NULL){
            for(Contact con:acc.Contacts){
                if(acc.Status__c != NULL && con.Status__c == acc.Status__c){
                 if(Trigger.isupdate && trigger.oldmap.get(acc.Id).Status__c  == acc.Status__c){
                     System.debug('==========================');
                 }
                System.debug('==========================');
                }
            else{
                System.debug('Updating status on Contact');
                   con.Status__c = acc.Status__c;
                   conList.add(con);  
                }   
            }
             System.debug('Contact List'+conList);
               update conList;
             System.debug('UPdated Contact Map'+conList);
                
        } 
        }
        
    }
}


Tahnk you!!
Sonali_takkeSonali_takke
Hi,

Try to identify any other possibilities in your org like flows or multiple triggers and turn them off
Or analyze code and try to idenify problem.

Here attached tested screenshots of my code.

Account with status completed

User-added image

Created new contact associated with this account

User-added image

Save contact .Status populated from account.
User-added image

Thank you
Sonali_takkeSonali_takke
Tested above code for Contact to Account Scenario and its  Working..

Account with status open
User-added image


Contact status updated to  completed 

User-added image


Account status automatically updated to completed

User-added image

Thank you
Sonali_takkeSonali_takke
HI,    You are refering two different fields of account in code 


trigger statuscontact on Contact (After insert,After Update) {  //sts__c(account)  status__c(contact)
    public static Boolean isFirstTime = true;
    if(isFirstTime){
    isFirstTime = False;    
    List<Account> accList = new List<Account>();
    List<Contact> conList = new List<Contact>();
  
    For(Contact con:[SELECT id,status__c,Account.Id,Account.status__c FROM Contact Where Id In:Trigger.newMap.keyset()]){ 
        //SELECT id,Status__c,Account.Id,Account.Status__c FROM Contact Where Id In:Trigger.newMap.keyset()
        
        if(Account.sts__c != NULL && con.status__c == con.Account.sts__c ){
            if(Trigger.isupdate && trigger.oldmap.get(con.Id).Status__c  == con.status__c){
                System.debug('===========UPDATE===============');
            }
            System.debug('=========INSERT=================');
          }
        else{
           System.debug('Updating status on Account');
            if(con.status__c != NULL){
                   con.Account.sts__c = con.status__c;
                 accList.add(con.Account); 
            }
            else{
                Contact conObj = new Contact(id=con.id);
                conObj.Status__c = con.Account.Status__c;
                conList.add(conObj);
             }
        }
    }
    System.debug('Account List'+accList);
    update accList;
    update conList;
    System.debug('UPdated Account Map'+accList);
    
    }

}

Plese make Correct Change.

Thanks!!!!
Sonali_takkeSonali_takke
Please Mark as Solved and close the thread to keep community clean. Mark as best answer if you find it useful for others.