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
Shruthi MN 28Shruthi MN 28 

Trigger To Roll up amount on Quota

I have written a trigger wherein I want all the opportunity amount to roll up on quote Actual_amount__c feild. The conditions are the opportunity should be closed won and and the opptunity should be on this month(jan). I am jnot able to get the logic right. Can anyone help me with the same

trigger LookupRollup on Opportunity (before insert) {
List<id> ParentIds = new List<id>();
    
    if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate){
        For(Opportunity Opp : Trigger.new){
            ParentIds.add(Opp.Amount);
        }
    }
    if(Trigger.isDelete){
        For(Opportunity Opp : Trigger.old){
            ParentIds.add(Opp.Amount);
        }
    }
    List<Quota__c> ProductsToUpdate = new List<Quota__c>();
    decimal sum = 0;
    if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate || trigger.isdelete){
        For(Quota__c q : [SELECT Name ,(SELECT id FROM  Actual_Amount__c) FROM Quota__c WHERE id =: ParentIds]){
            sum = 0;
            for(Opportunity p : q.Amount){
                
                sum = sum + 1;
            }
            q.Actual_Amount__c = sum;
            
        }
        ProductsToUpdate.add(q);        
    }
    try{
        update ProductsToUpdate;
    }Catch(Exception e){
        System.debug('Exception :'+e.getMessage());
    }
}
 
Shruthi MN 28Shruthi MN 28
User-added image