i created new custom field (Count_Of_Contact__c). then i written the trigger for update the related contact count, but did mistake.
it showing:
Compile Error: unexpected token: ')' at line 6 column 29
trigger countContact on contact(after insert, after Undelete){
set<Id> aId = new set<Id>();
List<contact> con = [select id from contact where accountId in:aId];
List<Account> acc = [select id, Count_Of_Contact__c from Account where Id in:aId];
if(trigger.Isinsert || trigger.IsUndelete){
for(contact ct = new contact){
aId.add(ct.accountId);
}
for(account ac = new account){
Count_Of_Contact__c = con.size();
}
}
update acc;
}
how can I fix the issue
Best Answer
It is advisable to use Aggregate Functions to count Contact records. It has performance benefit.
Refer Working with SOQL Aggregate Functions
Note: I haven't compile the code but approach will be like that.