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
Amit Singh 2702Amit Singh 2702 

I want to write a Trigger to update related contact when Account is Updated and Update Account when its Related Contact is updated c

AnkaiahAnkaiah (Salesforce Developers) 
Hi Amit,

Which fields you want update?

Thanks!!
Amit Singh 2702Amit Singh 2702
checkbox 
 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Amit,

Try with below code.
trigger checkboxupdateoncontact on Account (after update)
{
    Set<Id> accountIds = new Set<Id>();
    for(Account a:Trigger.new)
    {
	if(a.checkbox__c==True){
        accountIds.add(a.Id);
		}
    }

    List<Contact> ConListToUpdate=new List<Contact>();
    
    List<Contact> ConList = [Select Id, AccountId,checbox__c from Contact where AccountId IN :accountIds AND checkbox__c=False];
    if(ConList.size()>0)
    {
        for(Contact Con : ConList)
        {            
                Con.checbox__c=true;    
        
                ConListToUpdate.add(Con);  
        }
    }    
    if(ConListToUpdate.size()>0)
    {
        update ConListToUpdate;
    }
}

If this helps, please mark it as best answer.

Thanks!!​​​​​​​