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
developer org 183developer org 183 

Hi, I have one scenario if account status is inactive want to set the contact status also inactive and one custom field in account countcontact__c i want t to update this field with count of inactive contacts

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

In the above scenerio if Account is inactive we are updtaing contact status to inactive which means count of contacts is equal to count of inactive contacts in this scenerio. Can you correct me.

Thanks,
 
Shri RajShri Raj
trigger UpdateContactsOnAccount on Account (before update) {
  List<Contact> contactsToUpdate = new List<Contact>();
  for (Account acc : Trigger.new) {
    if (acc.Status__c == 'Inactive' && acc.Status__c != Trigger.oldMap.get(acc.Id).Status__c) {
      for (Contact con : [SELECT Id FROM Contact WHERE AccountId = :acc.Id AND IsActive = true]) {
        con.IsActive = false;
        contactsToUpdate.add(con);
      }
      acc.countcontact__c = contactsToUpdate.size();
    }
  }
  if (!contactsToUpdate.isEmpty()) {
    update contactsToUpdate;
  }
}

 
developer org 183developer org 183
Hi Praveen,

Yes you are correct
 
developer org 183developer org 183
Hi Shri Raj

If we do like this it will work for first account only I think so

Thanks.