You need to sign in to do that
Don't have an account?
Chitral 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?
{
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?
All Answers
Regards,
Fred
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;
}
}
Regards,
Fred
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);
}
}
it is same/short form for below lines
List<Contact> contacts = null;
if (Trigger.isdelete) {
contacts = trigger.old;
} else {
contacts = trigger.new;
}
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