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
Subodh shuklaSubodh shukla 

Scenario: How can i achieve this


we have a account and contacts there is status field in both
if the status of contact become open i want status of account become close and vise versa.
there is 6 contacts in account and all have different status , how can i achieve this.
pconpcon
This is a simple trigger that you would write on both your Contact and Account objects.
 
trigger UpdateAccountStatus on Contact (after insert, after delete) {
    Set<Id> accountIds = new Set<Id>();

    for (Contact c : Trigger.new) {
        if (c.Status__c = 'Closed') {
            if (Trigger.new || oldMap.get(c.Id).Status__c != 'Closed') {
                accountIds.add(c.AccountId);
            }
        }
    }

    accountIds.remove(null);

    List<Account> accounts = new List<Account>();

    for (Id id : accountIds) {
        accounts.add(new Account(
            Id = id,
            Status__c = 'Closed'
        );
    }

    if (!accounts.isEmpty()) {
        update accounts;
    }
}
NOTE: When including code please use the "Add a code sample" button (icon <>) to increase readability and make referencing code easier.

You would do something similar on the account object but you would need to pull down all the contacts associated with the account first.