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
Chitral ChaddaChitral Chadda 

trigger to populate count of contacts in account

trigger CountFemales1 on Contact (after insert,after update,after delete)
{
set<id>  conId = new set<id>();

 if(trigger.isInsert||trigger.isUpdate)
 {
  for(contact c: trigger.new)
  {
   if(c.accountid!=null)
   {
   conId.add(c.accountid);
   }
  }
 }
 
 
 if(trigger.isDelete)
 {
 for(contact c:trigger.old)
 {
  if(c.accountid!=null)
  {
  conId.add(c.accountid);
  }
 }
 
 
list<account> FemaleList = [select id  ,(select Accountid, Sex__c from contacts where Sex__c='Female') from account where id In :conId];
 map<id,integer> countMap = new map<id,integer>();
 for(account a :FemaleList)
 {
 countMap.put(a.id, a.contacts.size());
 }
 
 
 list<account> countlist = [select Count_of_Females__c from account where id in :conId];
 for(account a : countlist)
 {
 //Integer countoffemale = countMap.get(a.id);
 //a.Count_of_Females__c= countoffemale;
  a.Count_of_Females__c= countMap.get(a.id);
 }
 update countList;

}  
    
}


I want that it should display the count of no. of contacts where sex = female 
it m able to  save the trigger but  there is no result  displayed ( its not displaying the count of females in account , even though i have contacts where sex= female in the picklist)

any help?
Best Answer chosen by Chitral Chadda
BalajiRanganathanBalajiRanganathan
I see that your main logic is in only with  if (Trigger.isDelete). so that is the reason it is not working. you have to correct your } to fix this

All Answers

Frédéric TrébuchetFrédéric Trébuchet
Use "System.debug('here comes your message');" to trace anything in logfile from a trigger.

Regards,
Fred
Chitral ChaddaChitral Chadda
hie , thanks
no logs generated i used this
trigger CountFemales1 on Contact (after insert,after update,after delete)
{
set<id>  conId = new set<id>();

 if(trigger.isInsert||trigger.isUpdate)
 {
  for(contact c: trigger.new)
  {
   if(c.accountid!=null)
   {
   conId.add(c.accountid);
   }
  }
 }
 
 
 if(trigger.isDelete)
 {
 for(contact c:trigger.old)
 {
  if(c.accountid!=null)
  {
  conId.add(c.accountid);
  }
 }
 
 
list<account> FemaleList = [select id  ,(select Accountid, Sex__c from contacts where Sex__c='Female') from account where id In :conId];
 map<id,integer> countMap = new map<id,integer>();
 for(account a :FemaleList)
 {
 countMap.put(a.id, a.contacts.size());
 }
 system.debug('**This is countMap**'+countMap);
 
 list<account> countlist = [select Count_of_Females__c from account where id in :conId];
 system.debug('***This is countList***'+countList);
 for(account a : countlist)
 {
 //Integer countoffemale = countMap.get(a.id);
 //a.Count_of_Females__c= countoffemale;
  a.Count_of_Females__c= countMap.get(a.id);
   System.debug('******count of females***'+a.Count_of_Females__c);
 }
 update countList;

}  
    
}


 
BalajiRanganathanBalajiRanganathan
I see that your main logic is in only with  if (Trigger.isDelete). so that is the reason it is not working. you have to correct your } to fix this
This was selected as the best answer
Frédéric TrébuchetFrédéric Trébuchet
closing brace for "if(trigger.isDelete) {" should be before "list<account>..."

Regards,
Fred 
BalajiRanganathanBalajiRanganathan
you can try below optimed version of your code
trigger CountFemales1 on Contact (after insert,after update,after delete) {

  List<Contact> contacts = Trigger.isdelete ? trigger.old : trigger.new;
  Set<Id> accountIds = new Set<Id>();

  for (Contact c : contacts) {
     if (c.accountid != null) {
       accountIds.add(c.accountid);
    } 
  }

list<account> FemaleList = [select Count_of_Females__c,(select id from contacts where Sex__c='Female') from account where id In :accountIds];

 for(account a :FemaleList)  {
   a.Count_of_Females__c = a.contacts.size();
 }
 update FemaleList ;
}

 
Chitral ChaddaChitral Chadda
Holy COW ! Bad of me Thnks people !
Chitral ChaddaChitral Chadda
Thanks Balaji
CUd u please help me explain the section of code u provided, the following line rather than using  the code section i wrote
List<Contact> contacts = Trigger.isdelete ? trigger.old : trigger.new; //this line 

cz i used following in my code

 if(trigger.isInsert||trigger.isUpdate)
 {
  for(contact c: trigger.new)
  {
   if(c.accountid!=null)
   {
   conId.add(c.accountid);
   }
  }
 }
 
 
 if(trigger.isDelete)
 {
 for(contact c:trigger.old)
 {
  if(c.accountid!=null)
  {
  conId.add(c.accountid);
  }
 }
 
Chitral ChaddaChitral Chadda
And could u please tell if this line would work for Before insert before update also?
BalajiRanganathanBalajiRanganathan
I am using ternary operator (?). the reference contacts will be refer to trigger.old if trigger.isdelete is true else it will refer to trigger.new.

it is same/short form for below lines
List<Contact> contacts = null;
if (Trigger.isdelete) {
  contacts = trigger.old;
} else {
 contacts = trigger.new;
}
sagar077sagar077
Create an Apex Trigger that will count the number of contacts associated with an account(create a field at account level). Must update the count in insertion and deletion of a contact.

CODE-

trigger CountonAccountofcontact on Contact (after insert,after delete)
{
    Set<Id> mysetid = new Set <Id>();
        if(Trigger.isinsert)
        {
            System.debug('Insert new contact for trigger.new '+ Trigger.new);
            for(Contact contac :trigger.new)
                {
                    mysetid.add(contac.Accountid);
                }
            List<Account> Acc = [Select Id,Number_Of_Contact_Count__c from Account where Id in : mysetid];
            List<Contact> Con = [Select Id from Contact where Accountid in : mysetid];
            for(Account A: Acc)
                {
                    A.Number_Of_Contact_Count__c = Con.size(); 
                }
            update Acc;
            System.debug('Number of count is ' + Acc);
        }

     if(Trigger.isdelete)
        {
            System.debug('The Delete Contact Name For Trigger.old'+ Trigger.Old); 
            for(Contact contac : Trigger.Old)
                {
                    mysetid.add(contac.Accountid);
                }          
            List<Account> Acc = [Select id,Number_Of_Contact_Count__c from Account where id in: mysetid];
            List<Contact> Con = [Select id from Contact where Accountid in : mysetid];
           
            for(Account A :Acc)
                {
                    A.Number_Of_Contact_Count__c = Con.size();
                }
                update Acc;
            System.debug('The Update number is '+ Acc);
        }
    }

NOTE- This code is running but I want for After Update event also and Plz Help me in that