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
Pawan MakkarPawan Makkar 

Triggger


Scenario 1.

1) Make two number fields on contact object
Amount_X
Amount_Y

2) Make one picklist field "Type" , values ('Positive', 'Negative')

3) Make two number fields on account
Rollup_Amount_X
Rollup_Amount_Y
Rollup_Amount

4) Make one trigger on contact object, which will do following:
--> Sum all child of contact's field "Amount_X" and store in parent account's "Rollup_Amount_X" Where Type is "Positive"
--> Sum all child of contact's field "Amount_Y" and store in parent account's "Rollup_Amount_Y" Where Type is "Negative"
--> Sum all child of contact's field "Amount_X" + "Amount_Y" and store in parent account's "Rollup_Amount"


ans on =amuraghav14@gmail.com
Prashant Pandey07Prashant Pandey07
Hi Pawan, You can achieve the solution to this problem using flow but not sure the reason behind Trigger, however, you can check the below trigger it may help you solve your problem. Please mark this as the best solution if this solution helped you.

 
trigger RollupSummary on Contact (after insert, after update, before delete, after delete) {

set<id> ids=new set<id>();
if(Trigger.isInsert) {
for(Contact c:trigger.new){
ids.add(c.accountid);
}
}

if(trigger.isUpdate) {
for(Contact c:trigger.new){
ids.add(c.accountid);
}
}


if(trigger.isdelete){
for(Contact c:trigger.old){
ids.add(c.accountid);
}
}

List<Account> Xaccupdatelist=new list<Account>();
List<Account> Yaccupdatelist=new list<Account>();
List<Account> Totalaccupdatelist =new list<Account>();
Map<id,decimal> Xmapdecimal=new map<id,decimal>();
Map<id,decimal> Ymapdecimal=new map<id,decimal>();
Map<id,decimal> Totalmapdecimal=new map<id,decimal>();
List<Account> acclist=[select id ,(select id,Amount_X__c from contacts) from account where id=:ids];
list<AggregateResult> agrresult=[select type__c,accountid, sum(Amount_X__c)Xtotal, sum(Amount_Y__c)Ytotal  from contact where accountid in :ids group by Type__c,accountid];

decimal allsum;

for(AggregateResult arg:agrresult){
   
    decimal d=0;
    if(arg.get('Type__c')=='Positive'){     
    decimal countOfChild = (decimal) arg.get('Xtotal');  
    id accountIds = (id) arg.get('accountid');   
    if(countOfChild!=null) 
    d=d + countOfChild;  
    xmapdecimal.put(accountIds ,d);
    } 
    
    else if (arg.get('Type__c')=='Negative'){
   
    decimal countOfChilds = (decimal) arg.get('Ytotal');  
    id accountIds = (id) arg.get('accountid');   
    if(countOfChilds!=null) 
    d=d + countOfChilds;  
    Ymapdecimal.put(accountIds ,d);
    
    }
    else{
           allsum=((decimal) arg.get('Xtotal') + (decimal) arg.get('Ytotal'));
            system.debug('code'+allsum);
    }
    
}

for(Account aX:acclist){
     
      aX.Rollup_Amount_X__c=Xmapdecimal.get(aX.Id);
      Xaccupdatelist.add(aX);

}

For(Account aY:acclist){
      aY.Rollup_Amount_Y__c=Ymapdecimal.get(aY.Id);
      Yaccupdatelist.add(aY);


}

For(Account aTotal:acclist){
     aTotal.Rollup_Amount__c=allsum;
     Totalaccupdatelist.add(aTotal);
    
}

If(Xaccupdatelist.size()>0)
update Xaccupdatelist;
if(Yaccupdatelist.size()>0)
update  Yaccupdatelist;
if(Totalaccupdatelist.size()>0)
update Totalaccupdatelist;
 
}