You need to sign in to do that
Don't have an account?

Trigger to find no of contacts for an account
Hi,
Please help me in trigger coding,
I have written a trigger for finding no of contacts on a account as below,but the problem with this is when i am deleting the contact the field in account is showing the same no.(If suppose i have 5 contacts for a account xxxx when i delete a contact the field in the account xxxx should be 4)but the field is showing 5
Find the code below.
------------------------------------------------------------------------------------------------
trigger NoofCononAcc on Contact (after insert) {
list<contact> con = trigger.new;
for(contact c:con){
list<contact> conlst = [select id from contact where accountid=:c.accountid];
account acc = [select id from account where id=:c.accountid ];
acc.NoofCononAcc__c = conlst.size();
update acc;
}
Naren,
Try below
trigger NoofCononAcc on Contact (after insert,before delete) {
list<contact> con = trigger.new;
List <Contact> conList = new List<Contact>();
if(Trigger.IsInsert){
for(contact c:trigger.new)
{
Account acc = [Select RelatedContactCount__c from Account where ID =: c.Accountid];
if(acc.RelatedContactCount__c !=null)
{
acc.RelatedContactCount__c = acc.RelatedContactCount__c + con.size();
}
else{
acc.RelatedContactCount__c = con.size();
}
update acc;
}
}
if(Trigger.IsDelete){
List<Contact> delcon = trigger.old;
for(contact c:trigger.old)
{
Account acc = [Select RelatedContactCount__c from Account where ID = : c.Accountid];
acc.RelatedContactCount__c = acc.RelatedContactCount__c - delcon.size();
update acc;
}
}
}
All Answers
Your trigger only fire for insert action, you need to manage the delete action as well.
Hi,
That is what my question,please help me when we delete the contact,the field in the account should be reduced.
Thanks,
naren
Hello,
To update your trigger, from the top of my head, it should work.
You'll have an problem with bulk insert/delete, have a look on Salesforce dev documentation for the query in the loops.
trigger NoofCononAcc on Contact (after insert. after delete) {
list<contact> con;
if (trigger.isDelete == true) {
con = trigger.new;
}
else
{
con = trigger.new;
}
for(contact c:con){
list<contact> conlst = [select id from contact where accountid=:c.accountid];
account acc = [select id from account where id=:c.accountid ];
acc.NoofCononAcc__c = conlst.size();
update acc;
}
Naren,
Try below
trigger NoofCononAcc on Contact (after insert,before delete) {
list<contact> con = trigger.new;
List <Contact> conList = new List<Contact>();
if(Trigger.IsInsert){
for(contact c:trigger.new)
{
Account acc = [Select RelatedContactCount__c from Account where ID =: c.Accountid];
if(acc.RelatedContactCount__c !=null)
{
acc.RelatedContactCount__c = acc.RelatedContactCount__c + con.size();
}
else{
acc.RelatedContactCount__c = con.size();
}
update acc;
}
}
if(Trigger.IsDelete){
List<Contact> delcon = trigger.old;
for(contact c:trigger.old)
{
Account acc = [Select RelatedContactCount__c from Account where ID = : c.Accountid];
acc.RelatedContactCount__c = acc.RelatedContactCount__c - delcon.size();
update acc;
}
}
}
Hi,
Thanks for your reply,the trigger is working fine,but for already existing records its not working,for the new entry of records and on deleting ts working fine.
Thanks
Naren.
For the existing record,you need to put update event also in the Trigger.