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
DIVAKAR BABU 15DIVAKAR BABU 15 

i have 3 triggers after insert ,after update ,after delete but i have to write in only one trigger how

after insert
trigger rollup summary on Contact (after insert) {
    set<id>accid = new set<id>();
    for(Contact c:Trigger.new){
        accid.add(c.Accountid);
    }
    list<Account> acc =[select Id,name from Account where Id=:accid];
     Map<id,Account> accmap = new Map<id,Account>();
    for(Account ac: acc){
        accmap.put(ac.Id,ac);
    }
    for( contact c:Trigger.new){
        if(accmap.containskey(c.Accountid)){
    
    Account a = accmap.get(c.Accountid);
    a.Number_of_Contacts__c = a.Number_of_Contacts__c +1 ;
            accmap.put(c.Accountid,a);
        }
    }
    
update accmap.values();
    }    

after delete
trigger rollup summary on Contact (after delete) {
    set<id>accid = new set<id>();
    for(Contact c:Trigger.old){
        accid.add(c.Accountid);
    }
    list<Account> acc =[select Id,name from Account where Id=:accid];
     Map<id,Account> accmap = new Map<id,Account>();
    for(Account ac: acc){
        accmap.put(ac.Id,ac);
    }
    for( contact c:Trigger.old){
        if(accmap.containskey(c.Accountid)){
    
    Account a = accmap.get(c.Accountid);
    a.Number_of_Contacts__c = a.Number_of_Contacts__c +1 ;
            accmap.put(c.Accountid,a);
        }
    }
    
update accmap.values();
    }    
After update
trigger afterupdate on Contact (after update) {
    set<id>accid = new set<id>();
    for(Contact c:Trigger.new){
        accid.add(c.Accountid);
    }
    list<Account> acc =[select Id,name from Account where Id=:accid];
    Map<id,Account> accmap = new Map<id,Account>();
    for(Account ac: acc){
        accmap.put(ac.Id,ac);
    }
    for( contact c:Trigger.new){
        if(c.AccountId!=trigger.oldmap.get(c.Id).Accountid){
            if(accmap.containskey(c.Accountid)){
                Account a = accmap.get(c.Accountid);
                a.Number_of_Contacts__c = a.Number_of_Contacts__c +1 ;
                accmap.put(c.Accountid,a);
            }
            if(accmap.containskey(trigger.oldmap.get(c.Id).Accountid)){
                Account a = accmap.get(trigger.oldmap.get(c.Id).Accountid);
                a.Number_of_Contacts__c = a.Number_of_Contacts__c -1 ;
                accmap.put(c.Accountid,a);
            }
        }
    }
    update accmap.values();
}

 
Murali MattaMurali Matta
Hi DIVAKAR,

Please find the below code for after insert, after update, after delete
trigger rollup summary on Contact (after insert,after update, after deletet) {

if(trigger.isafter){
    set<id>accid = new set<id>();
    for(Contact c:Trigger.new){
        accid.add(c.Accountid);
    }
    list<Account> acc =[select Id,name from Account where Id=:accid];
     Map<id,Account> accmap = new Map<id,Account>();
    for(Account ac: acc){
        accmap.put(ac.Id,ac);
    }
    for( contact c:Trigger.new){
        if(accmap.containskey(c.Accountid)){
    
    Account a = accmap.get(c.Accountid);
    a.Number_of_Contacts__c = a.Number_of_Contacts__c +1 ;
            accmap.put(c.Accountid,a);
        }
    }
    
update accmap.values();
    }
}


Let me know if you have any confusion..

Kindly mark this as solved if the reply was helpful.

Thanks,
Murali