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
Abilash.SAbilash.S 

To get count of users related to contact in Account object

I have account and contact which have lookup relationship. In contact there are three fields like f1, f2, f3. Each field has some users count. I need to get all count of users in account object. How to do this.
ANUTEJANUTEJ (Salesforce Developers) 
Hi Pavushetti,

>> https://trailblazers.salesforce.com/answers?id=9063A000000e6q5QAA

The above link has a similar implementation you can try checking the answer in this link once.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Suraj Tripathi 47Suraj Tripathi 47

Hi Pavushetti,

we can write a trigger for this. we will add the total user from the contact field and put them in the account field.

trigger DataUpdate on Contact (before update) {
if(trigger.isBefore && Trigger.isUpdate){
set<Id> accountSet=new set<Id>();
for(contact con:trigger.new){
accountSet.add(con.accountid);
}

Map<Id,Account> accountMap=new Map<Id,Account>([select id,f1__c,f2__c,f3__c from Account where id in: accountSet]);

for(contact con:trigger.new){
if(con.accountid==accountMap.get(con.accountid).id){
if(con.f1__c!=null && con.f2__c!=null && con.f3__c!=null){
	accountMap.get(con.accountid).TotalUserCount__c=con.f1__c+con.f2__c+con.f3__c;
}
}
}
}
update accountMap.values();
}
}


You can take reference from this code.

If it helps you please mark it as Best Answer.

Thank You!