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
narensfdcnarensfdc 

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;
  }

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

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

Jean-NoelJean-Noel

Your trigger only fire for insert action, you need to manage the delete action as well.

narensfdcnarensfdc

Hi,

 

     That is what my question,please help me when we delete the contact,the field in the account should be reduced.

 

Thanks,

naren

Jean-NoelJean-Noel

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;
}

Vinit_KumarVinit_Kumar

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;

}
}

}

This was selected as the best answer
narensfdcnarensfdc

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. 

Vinit_KumarVinit_Kumar

For the existing record,you need to put update event also in the Trigger.