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 

WRITE LOGIC

SUM OF 1) Make two number fields on Opportunity 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 Opportunity object, which will do following:
--> Sum all child of Opportunity's field "Amount_X" and store in parent account's "Rollup_Amount_X" Where Type is "Positive"
--> Sum all child of Opportunity's field "Amount_Y" and store in parent account's "Rollup_Amount_Y" Where Type is "Negative"
--> Sum all child of Opportunity's field "Amount_X" + "Amount_Y" and store in parent account's "Rollup_Amount"

5) Make trigg
PrasathPrasath
Hi DIVAKAR,

We can achive this funtionality without writing the code by creating the Rollup Summary field and formula field.

Solution, 
Create Two Roll-Up Summary on Account Object, One for Rollup_Amount_X and another one for Rollup_Amount_Y, In the rollup summary use the SUM Funtion and in the filter criteria add you coditions like Type is "Positive" Or Type is "Negative".

To calculate both child of Opportunity's field "Amount_X" + "Amount_Y", We already have the values in Account so we can directly create the formula field on account object and add Rollup_Amount_X + Rollup_Amount_Y.
Deepali KulshresthaDeepali Kulshrestha
Hi Divakar,

Please do try the code below and you will surely get the expected results.

Trigger:
trigger RollAmounts on Opportunity (after insert, after update) {
    RollAmounts_TrigHandler.populatefields(trigger.new);
}

Trigger Handler:

public class RollAmounts_TrigHandler {
    public static void populatefields(List<Opportunity> opList)
    {
     set<Id> acId=new set<Id>();
     Decimal strX=0; Decimal strY=0;
     for(Opportunity op: opList) 
     {
         if(op.accountid!= null && op.Amount_X__c !=null && op.TypePN__c=='Positive')
         {
           acId.add(op.AccountId); 
            strX= strX + op.Amount_X__c;
         }
         
         else if(op.accountid!= null && op.Amount_Y__c !=null && op.TypePN__c=='Negative')
         {
           acId.add(op.AccountId);
            strY= strY + op.Amount_Y__c;
         }
     }
        List<Account> acList= [Select name,Rollup_Amount_X__c from Account where id in:acId];
        if(acList.size()>0)
        {
           for(Account a:acList) 
           {
               a.Rollup_Amount_X__c=strX;
               a.Rollup_Amount_Y__c=strY;
               a.Rollup_Amount__c=strX + strY;
               
           }
          update acList;
        }
        
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha