//To do rollup on Account with opportunity and display the sum of No of hours on related account
trigger OpportunityRollup on Opportunity (after delete, after insert, after update) {
Set<Id> accountids=new Set<Id>();
Map<id,Account> mapaccount=new Map<id,Account>();
Map<id,List<opportunity>> mapopportunity=new Map<id,List<opportunity>>();
if(Trigger.isInsert || Trigger.isUpdate){
List<Opportunity> lstopp=[select id,name,No_of_Hours__c,accountid from opportunity where id in:Trigger.newMap.keyset()];
for(Opportunity opp:lstopp){
accountids.add(opp.accountid);
}
List<Account> acclst=[select id,name,No_of_opp__c,No_of_Hours__c,industry,(select accountid,id,No_of_Hours__c,name from opportunities) from account where id in:accountids];
for(Account acc:acclst){
mapaccount.put(acc.id,acc);
mapopportunity.put(acc.id,acc.opportunities);
}
AggregateResult i=[select sum(No_of_Hours__c) from opportunity where accountid in:mapaccount.keyset()];
for(Id id:mapaccount.keyset()){
mapaccount.get(id).No_of_opp__c=0;
mapaccount.get(id).No_of_Hours__c=0;
mapaccount.get(id).No_of_opp__c=mapopportunity.get(id).size();
decimal total=0;
for(Opportunity opp:mapopportunity.get(id)) {
if(opp.No_of_Hours__c!=null)
//total+=opp.No_of_Hours__c;
mapaccount.get(id).No_of_Hours__c=mapaccount.get(id).No_of_Hours__c+opp.No_of_Hours__c;
}
//mapaccount.get(id).No_of_Hours__c=total;
}
if(mapaccount!=null && mapaccount.size()>0){
try{
update mapaccount.values();
}
catch(Exception e){
}
}
}
if(Trigger.isDelete){
List<Opportunity> lstopp=[select id,name,No_of_Hours__c,accountid from opportunity where id in:Trigger.oldMap.keyset()];
for(Opportunity opp:lstopp){
accountids.add(opp.accountid);
}
List<Account> acclst=[select id,name,No_of_opp__c,No_of_Hours__c,industry,(select accountid,id,No_of_Hours__c,name from opportunities) from account where id not in:accountids];
for(Account acc:acclst){
mapaccount.put(acc.id,acc);
mapopportunity.put(acc.id,acc.opportunities);
}
AggregateResult i=[select sum(No_of_Hours__c) from opportunity where accountid in:mapaccount.keyset()];
for(Id id:mapaccount.keyset()){
mapaccount.get(id).No_of_opp__c=0;
mapaccount.get(id).No_of_Hours__c=0;
mapaccount.get(id).No_of_opp__c=mapopportunity.get(id).size();
decimal total=0;
for(Opportunity opp:mapopportunity.get(id)) {
if(opp.No_of_Hours__c!=null)
//total+=opp.No_of_Hours__c;
mapaccount.get(id).No_of_Hours__c=mapaccount.get(id).No_of_Hours__c+opp.No_of_Hours__c;
}
//mapaccount.get(id).No_of_Hours__c=total;
}
if(mapaccount!=null && mapaccount.size()>0){
try{
update mapaccount.values();
}
catch(Exception e){
}
}
}
}
trigger OpportunityRollup on Opportunity (after delete, after insert, after update) {
Set<Id> accountids=new Set<Id>();
Map<id,Account> mapaccount=new Map<id,Account>();
Map<id,List<opportunity>> mapopportunity=new Map<id,List<opportunity>>();
if(Trigger.isInsert || Trigger.isUpdate){
List<Opportunity> lstopp=[select id,name,No_of_Hours__c,accountid from opportunity where id in:Trigger.newMap.keyset()];
for(Opportunity opp:lstopp){
accountids.add(opp.accountid);
}
List<Account> acclst=[select id,name,No_of_opp__c,No_of_Hours__c,industry,(select accountid,id,No_of_Hours__c,name from opportunities) from account where id in:accountids];
for(Account acc:acclst){
mapaccount.put(acc.id,acc);
mapopportunity.put(acc.id,acc.opportunities);
}
AggregateResult i=[select sum(No_of_Hours__c) from opportunity where accountid in:mapaccount.keyset()];
for(Id id:mapaccount.keyset()){
mapaccount.get(id).No_of_opp__c=0;
mapaccount.get(id).No_of_Hours__c=0;
mapaccount.get(id).No_of_opp__c=mapopportunity.get(id).size();
decimal total=0;
for(Opportunity opp:mapopportunity.get(id)) {
if(opp.No_of_Hours__c!=null)
//total+=opp.No_of_Hours__c;
mapaccount.get(id).No_of_Hours__c=mapaccount.get(id).No_of_Hours__c+opp.No_of_Hours__c;
}
//mapaccount.get(id).No_of_Hours__c=total;
}
if(mapaccount!=null && mapaccount.size()>0){
try{
update mapaccount.values();
}
catch(Exception e){
}
}
}
if(Trigger.isDelete){
List<Opportunity> lstopp=[select id,name,No_of_Hours__c,accountid from opportunity where id in:Trigger.oldMap.keyset()];
for(Opportunity opp:lstopp){
accountids.add(opp.accountid);
}
List<Account> acclst=[select id,name,No_of_opp__c,No_of_Hours__c,industry,(select accountid,id,No_of_Hours__c,name from opportunities) from account where id not in:accountids];
for(Account acc:acclst){
mapaccount.put(acc.id,acc);
mapopportunity.put(acc.id,acc.opportunities);
}
AggregateResult i=[select sum(No_of_Hours__c) from opportunity where accountid in:mapaccount.keyset()];
for(Id id:mapaccount.keyset()){
mapaccount.get(id).No_of_opp__c=0;
mapaccount.get(id).No_of_Hours__c=0;
mapaccount.get(id).No_of_opp__c=mapopportunity.get(id).size();
decimal total=0;
for(Opportunity opp:mapopportunity.get(id)) {
if(opp.No_of_Hours__c!=null)
//total+=opp.No_of_Hours__c;
mapaccount.get(id).No_of_Hours__c=mapaccount.get(id).No_of_Hours__c+opp.No_of_Hours__c;
}
//mapaccount.get(id).No_of_Hours__c=total;
}
if(mapaccount!=null && mapaccount.size()>0){
try{
update mapaccount.values();
}
catch(Exception e){
}
}
}
}
No comments:
Post a Comment