You need to sign in to do that
Don't have an account?
A 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..
can anyone help me i'm new to codings..
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
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 :-
Kindly mark it as the best answer so that it can helps other as well.
Regards,
Priya Ranjan
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
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;
}
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