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
A AnanthiA Ananthi 

count the field "is active " from contact object and should populate the value in "Total active contacts" in account object.

Hi ... i have count the field "is active " from contact object and should populate the value in "Total active contacts" in account object.
can anyone help me i'm new to codings..
Best Answer chosen by A Ananthi
Shubham Jain 338Shubham Jain 338
Hi Ananthi,

Please find the working and optimized code. The code will check whether the account (lookup) is changing or is active status is changing or both are changing and accordingly will decide which account we need to update or we don't need to update the account.

trigger TriggerContact on Contact (after insert, after update, after delete, after undelete) {
    
    List<Contact> contactList = Trigger.isDelete ? Trigger.Old : Trigger.New;
    Set<Id> accountIdSet = new Set<Id>();
    Boolean accountChange = false;
    Boolean isActiveChange = false;
    for (Contact con : contactList) {
        if (Trigger.isUpdate) {
            accountChange = (con.AccountId != Trigger.oldMap.get(con.Id).AccountId);
            isActiveChange = (con.Is_Active__c != Trigger.oldMap.get(con.Id).Is_Active__c);
            if (accountChange && isActiveChange) {
                if (con.Is_Active__c) {
                    if (con.AccountId != null) accountIdSet.add(con.AccountId);    
                } else if (Trigger.oldMap.get(con.Id).AccountId != null) {
                    accountIdSet.add(Trigger.oldMap.get(con.Id).AccountId);
                }
            } else if (accountChange && con.Is_Active__c) {
                if (con.AccountId != null) accountIdSet.add(con.AccountId);
                if (Trigger.oldMap.get(con.Id).AccountId != null) accountIdSet.add(Trigger.oldMap.get(con.Id).AccountId);
            } else if (isActiveChange) {
                if (con.AccountId != null) accountIdSet.add(con.AccountId);
            }
        } else if (con.AccountId != null && con.Is_Active__c) {
            accountIdSet.add(con.AccountId);
        }
    }
    if (accountIdSet.size() > 0) {
        List<Account> accountList = [SELECT Id, Total_Active_Contacts__c, (SELECT Id FROM Contacts WHERE Is_Active__c = true) FROM Account WHERE Id IN :accountIdSet];
        List<Account> accountListToUpdate = new List<Account>();
        if (accountList.size() > 0) {
            for (Account acc : accountList) {
                if (acc.Total_Active_Contacts__c != acc.Contacts.size()) {
                    acc.Total_Active_Contacts__c = acc.Contacts.size();
                    accountListToUpdate.add(acc);
                }
            }
            if (accountListToUpdate.size() > 0) {
                update accountListToUpdate;
            }
        }
    }
}

Please mark this as the best answer if it helps

Thanks
Shubham Jain

All Answers

PriyaPriya (Salesforce Developers) 

Hey Ananthi,

You can develop a trigger to fecth the number of conact which is having "Is Active" as true, under each account and then update the field on the Account.

check the sample code and modify it accordingly :- 

trigger CountContactOnAccount on Contact (after INSERT, after UPDATE, after DELETE ) {
Set <Id> accountIds = new Set <Id>();
List <Account> lstAccountsToUpdate = new List <Account>();
 if(Trigger.isInsert){
    for(Contact con:trigger.new){
        accountIds.add(con.accountID);
    }
}
if(Trigger.isUpdate|| Trigger.isDelete){
    for(Contact con:trigger.old){
        accountIds.add(con.accountID);
    }
}

for(Account acc:[SELECT Id,Name,Count_Contact__c,(Select Id from Contacts where Is_Active = :True) from Account where Id IN: accountIds]){
    Account accObj = new Account ();
    accObj.Id = acc.Id;
    accObj.Count_Contact__c = acc.Contacts.size();
    lstAccountsToUpdate.add(accObj);
}

UPDATE lstAccountsToUpdate;
}
 

Kindly mark it as the best answer so that it can helps other as well.

Regards,

Priya Ranjan

A AnanthiA Ananthi
trigger Countisactivefield on Contact (after insert, after update) 

    for (Contact c : Trigger.new) 
    if(c.Is active = True)
    {
    // Count Is active field if the value is false
    List<Account> toUpdate = [SELECT j.Total active contacts, Total_Active_Contacts__c FROM Contact j WHERE j.Id =: c.Id FOR UPDATE];
    }

   
        List<Account> acc = [select id,Total_Active_Contacts__c from Account where Id in:aId];
        List<Contact> con = [select id from contact where AccountId in :aId];
        
        for(Account a : acc){
            Total_Active_Contacts__c=con.is active();
        }
        

this is my codings but 'm receiving error


    
    
    
    
 
A AnanthiA Ananthi
Yes i tried this again its showing error...

trigger CountContactOnAccount on Contact (after INSERT, after UPDATE, after DELETE ) {
Set <Id> accountIds = new Set <Id>();
List <Account> lstAccountsToUpdate = new List <Account>();
 if(Trigger.isInsert){
    for(Contact con:trigger.new){
        accountIds.add(con.accountID);
    }
}
if(Trigger.isUpdate|| Trigger.isDelete){
    for(Contact con:trigger.old){
        accountIds.add(con.accountID);
    }
}

for(Account acc:[SELECT Id,Name,Is_Active__c    ,(Select Id from Contacts where Is_Active = :True) from Account where Id IN: Total_Active_Contacts__c])
{
    Account accObj = new Account ();
    accObj.Id = acc.Id;
    accObj.Is_Active__c     = acc.Contacts.size();
    lstAccountsToUpdate.add(accObj);
}

UPDATE lstAccountsToUpdate;
}
Shubham Jain 338Shubham Jain 338
Hi Ananthi,

Please find the working and optimized code. The code will check whether the account (lookup) is changing or is active status is changing or both are changing and accordingly will decide which account we need to update or we don't need to update the account.

trigger TriggerContact on Contact (after insert, after update, after delete, after undelete) {
    
    List<Contact> contactList = Trigger.isDelete ? Trigger.Old : Trigger.New;
    Set<Id> accountIdSet = new Set<Id>();
    Boolean accountChange = false;
    Boolean isActiveChange = false;
    for (Contact con : contactList) {
        if (Trigger.isUpdate) {
            accountChange = (con.AccountId != Trigger.oldMap.get(con.Id).AccountId);
            isActiveChange = (con.Is_Active__c != Trigger.oldMap.get(con.Id).Is_Active__c);
            if (accountChange && isActiveChange) {
                if (con.Is_Active__c) {
                    if (con.AccountId != null) accountIdSet.add(con.AccountId);    
                } else if (Trigger.oldMap.get(con.Id).AccountId != null) {
                    accountIdSet.add(Trigger.oldMap.get(con.Id).AccountId);
                }
            } else if (accountChange && con.Is_Active__c) {
                if (con.AccountId != null) accountIdSet.add(con.AccountId);
                if (Trigger.oldMap.get(con.Id).AccountId != null) accountIdSet.add(Trigger.oldMap.get(con.Id).AccountId);
            } else if (isActiveChange) {
                if (con.AccountId != null) accountIdSet.add(con.AccountId);
            }
        } else if (con.AccountId != null && con.Is_Active__c) {
            accountIdSet.add(con.AccountId);
        }
    }
    if (accountIdSet.size() > 0) {
        List<Account> accountList = [SELECT Id, Total_Active_Contacts__c, (SELECT Id FROM Contacts WHERE Is_Active__c = true) FROM Account WHERE Id IN :accountIdSet];
        List<Account> accountListToUpdate = new List<Account>();
        if (accountList.size() > 0) {
            for (Account acc : accountList) {
                if (acc.Total_Active_Contacts__c != acc.Contacts.size()) {
                    acc.Total_Active_Contacts__c = acc.Contacts.size();
                    accountListToUpdate.add(acc);
                }
            }
            if (accountListToUpdate.size() > 0) {
                update accountListToUpdate;
            }
        }
    }
}

Please mark this as the best answer if it helps

Thanks
Shubham Jain
This was selected as the best answer
A AnanthiA Ananthi
Thank you...Shubham Jain 338...Its working