You need to sign in to do that
Don't have an account?
arpit vijayvergiya
Trigger on Contact for Total Family Members
- We have standard "Contact" object in SFDC.
- You have to add one Field in Contact Object that is "Total Family Members" type is Number(2,0):
- Let Account be the Family. So if there are 5 Contacts belongs to same account, that means these all 5 belongs to same family.
- Create a trigger on Contact to calculate "Family Members Count".
- Create one Account: Create New Contact as a child for it. Then Contact's "Total Family Member" will have value 1.
- Create another Contact for the same Account then Both Contact should have value 2 for "Total Family Member Field".
- if someone changes contact's family like : changing account lookup value for a contact, then Both family will reflect. Because one family member is removed from one family and added to new Family. values should be updated:
- All Contact's of Older Family should now have the "Total Family Member" value decreased by1.
- And for new family all Contact's "Total Family Member" will have value increased by1.
Hi Arpit,
I think the field "Total Family Members" which you have created on Contact should be on Account which will show the count of Contacts corresponding to that Account.
And then if you want that count should be visible in each contact then create formula field on Contact which will store the value of "Total Family Members" from Account.
Below is the trigger:
thank you
By your help i resolved the problem.
Here is code for that
trigger contactTrigger on Contact(after insert, after delete, after update, after undelete){
Map<String, List<Contact>> mapOfAccContacts = new Map<String, List<Contact>>();
Map<String, List<Contact>> mapAccIdDelCon = new Map<String, List<Contact>>();
List<Contact> listOfContact = new List<Contact>();
List<Contact> listOfUpdateContact = new List<Contact>();
List<Account> lstAcc = new List<Account>();
Set<String> setOfAccIds = new Set<String>();
if(trigger.isInsert || trigger.isUndelete || trigger.isUpdate){
for(Contact con : trigger.new){
if(con.AccountId != null){
setOfAccIds.add(con.AccountId);
}
}
}
if(trigger.isDelete || trigger.isUpdate){
for(Contact con : trigger.old){
if(con.AccountId != null){
setOfAccIds.add(con.AccountId);
}
}
}
if(setOfAccIds.size()>0){
listOfContact = [SELECT id,Total_Family_Members__c,accountId from Contact WHERE AccountId IN : setOfAccIds];
if(listOfContact.size()>0){
for(Contact objCon : listOfContact){
List<Contact> templistOfContact = new List<Contact>();
if(mapOfAccContacts.containsKey(objCon.accountId)){
templistOfContact = mapOfAccContacts.get(objCon.accountId);
}
templistOfContact.add(objCon);
mapOfAccContacts.put(objCon.accountId,templistOfContact);
}
if(mapOfAccContacts.size()>0){
for(Contact objCon : listOfContact){
if(objCon.Total_Family_Members__c != mapOfAccContacts.get(objCon.accountId).size()){
objCon.Total_Family_Members__c = mapOfAccContacts.get(objCon.accountId).size();
listOfUpdateContact.add(objCon);
}
}
}
}
if(listOfUpdateContact.size()>0){
update listOfUpdateContact;
}
}
}
My Scenario is same but I am creating a Trigger is Before Event Using Aggregate Query But Field is not Update Proper
My Code Is:-
trigger Totalfamilymember2 on Contact (Before insert,Before update,Before delete) {
set<id> ids = new set<id>();
list<contact> contactToUpdate = new List<contact>();
if(Trigger.isinsert||Trigger.isupdate){
for(contact con : trigger.new){
if(con.AccountId != null){
Ids.add(con.AccountId);
}
}
}
if(Trigger.isdelete){
for(contact con : trigger.old){
if(con.AccountId != null){
Ids.add(con.AccountId);
}
}
}
for(AggregateResult ar :[SELECT AccountId,count(id) related FROM Contact where AccountId in: Ids GROUP BY AccountId]){
contactToUpdate.add(new contact(Accountid = (Id)ar.get('AccountId'),Total_Family_Member__c= (Decimal)ar.get('related') ));
}
}
Can you please explain why here is need Trigger.old and trigger.new when you just put same set of accountid in Trigger.new and trigger.old