You need to sign in to do that
Don't have an account?
mirza OSAMA
/* No of contacts field on account object=total contacts associated to account * insert,update works but not delete*/
/* No of contacts field on account=total contacts associated to account
* insert,update works but not delete*/
public class mirza {
public static void method1(List<Contact>newlist,list<Contact>oldlist){
set<id>conset=new set<id>();
try{
for(Contact c: newlist){
if(c.AccountId!=null){
conset.add(c.Accountid);
}
}
if(oldlist!=null){
for(Contact c:oldlist){
conset.add(c.AccountId);
}
}
if(conset!=null){
list<Account> acclist =[select id,No_of_Contacts__c,(select id from Contacts)from Account where id in :conset];
if(acclist!=null){
for(Account acc:acclist){
acc.No_of_Contacts__c=acc.Contacts.Size();
}
}
if(!acclist.isEmpty()){
Update acclist;
}
}
} Catch(ExCEPtion e){
System.debug(e.getMESSAGE());
}
}
}
------------------
trigger mirzatrigger on Contact (after insert,after update,after delete, after undelete) {
if(Trigger.isInsert || Trigger.isupdate || Trigger.isdelete || Trigger.isUndelete){
mirza.method1(Trigger.new,Trigger.old);
}
}
* insert,update works but not delete*/
public class mirza {
public static void method1(List<Contact>newlist,list<Contact>oldlist){
set<id>conset=new set<id>();
try{
for(Contact c: newlist){
if(c.AccountId!=null){
conset.add(c.Accountid);
}
}
if(oldlist!=null){
for(Contact c:oldlist){
conset.add(c.AccountId);
}
}
if(conset!=null){
list<Account> acclist =[select id,No_of_Contacts__c,(select id from Contacts)from Account where id in :conset];
if(acclist!=null){
for(Account acc:acclist){
acc.No_of_Contacts__c=acc.Contacts.Size();
}
}
if(!acclist.isEmpty()){
Update acclist;
}
}
} Catch(ExCEPtion e){
System.debug(e.getMESSAGE());
}
}
}
------------------
trigger mirzatrigger on Contact (after insert,after update,after delete, after undelete) {
if(Trigger.isInsert || Trigger.isupdate || Trigger.isdelete || Trigger.isUndelete){
mirza.method1(Trigger.new,Trigger.old);
}
}
What is the error occuring while deleting the contact record?
Did you check the debug log ?
Thanks!