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

hii.. I have a custom field(Number_of_contact__c) on account .I had written a trigger to update the field when a contact is created or deleted ,but it is not working.
trigger C1 on Contact (after insert,after delete) {
decimal x;
If (trigger.IsInsert==true){
for(contact c:trigger.new){
If(c.Account!=null){
Account a =new account();
a=[select Number_of_contact__c from account where account.name=:c.account.name];
if(a.Number_of_contact__c==null){
a.Number_of_contact__c=0;}
x=a.Number_of_contact__c ;
x=x+1;
a.Number_of_contact__c=x;
update a;
}
}
}
}
decimal x;
If (trigger.IsInsert==true){
for(contact c:trigger.new){
If(c.Account!=null){
Account a =new account();
a=[select Number_of_contact__c from account where account.name=:c.account.name];
if(a.Number_of_contact__c==null){
a.Number_of_contact__c=0;}
x=a.Number_of_contact__c ;
x=x+1;
a.Number_of_contact__c=x;
update a;
}
}
}
}
All Answers
go to below link for your solution , you need to write trigger on after insert, after delete , after undelete , after update contact
http://www.infallibletechie.com/2013/09/trigger-to-count-number-of-contacts.html
thanks
let me inform if it helps you and mark it best answer if my answer helps you so it make proper solution for others in future :)
trigger countoncontact on contact(After insert,after update,After delete)
{
if(trigger.IsAfter && trigger.isinsert)
{
set<ID>Idset1=new set<Id>();
for(contact con:trigger.new)
{
Idset1.add(con.AccountID);
}
list<Account>accupdatelist=new list<Account>();
list<Account>acclist=new list<Account>([select Id,Name,No_Of_Contacts__c,(select id,name,AccountID from Contacts ) from Account where ID In:Idset1]);
for(Account acc:acclist){
acc.No_Of_Contacts__c=Acc.contacts.size();
accupdatelist.add(acc);
}
update accupdatelist;
}
if(trigger.IsAfter && trigger.isupdate)
{
set<ID>IDset1=new set<ID>();
for(contact con:trigger.new)
{
if(con.AccountID!=trigger.oldmap.get(con.ID).AccountID)
{
Idset1.add(con.AccountID);
idset1.add(trigger.oldmap.get(con.ID).AccountID);
}
}
list<Account>accupdatelist=new list<Account>();
list<Account>acclist=new list<Account>([select Id,Name,No_Of_Contacts__c,(select id,name,AccountID from Contacts ) from Account where ID In:Idset1]);
for(Account acc:acclist){
acc.No_Of_Contacts__c=Acc.contacts.size();
accupdatelist.add(acc);
}
update accupdatelist;
}
if(trigger.IsAfter && trigger.isdelete)
{
set<ID>Idset1=new set<Id>();
for(contact con:trigger.old)
{
idset1.add(con.AccountID);
}
list<Account>accupdatelist=new list<Account>();
list<Account>acclist=new list<Account>([select Id,Name,No_Of_Contacts__c,(select id,name,AccountID from Contacts ) from Account where ID In:Idset1]);
for(Account acc:acclist){
acc.No_Of_Contacts__c=Acc.contacts.size();
accupdatelist.add(acc);
}
update accupdatelist;
}
if(trigger.IsAfter && trigger.isundelete){
Map<ID,set<ID>>AccMap1=new Map<ID,Set<ID>>();
for(Contact con:trigger.new)
{
if(AccMap1.ContainsKey(con.AccountID))
{
AccMap1.get(con.AccountID).add(con.ID);
}
else
{
AccMap1.put(con.AccountID,new set<ID> {con.Id});
}
}
list<Account>accupdatelist=new list<Account>();
list<Account>acclist=new list<Account>([select Id,Name,No_Of_Contacts__c,(select id,name,AccountID from Contacts ) from Account where ID In:AccMap1.Keyset()]);
for(Account acc:acclist){
acc.No_Of_Contacts__c=Acc.contacts.size();
accupdatelist.add(acc);
}
update accupdatelist;
}
}
thanks
Could you please help me in writing the test class for this trigger?
Thanks in advance
please put a fresh question on forums related to test class. so with my answer other people take benefits of it. :)
thanks
thanks