You need to sign in to do that
Don't have an account?
DIVAKAR 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();
}
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();
}
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