0
trigger OpportunityRollUpPayments on Opportunity_Splits__c(after delete, after insert, after update) {

  set<Id> OpportunityIds = new set<Id>();

  if(trigger.isInsert || trigger.isUpdate){
    for(Opportunity_Splits__c p : trigger.new){
      OpportunityIds.add(p.Opportunity__c);
    }
  } 
  if(trigger.isDelete){
    for(Opportunity_Splits__c p : trigger.old){
      OpportunityIds.add(p.Opportunity__c);
    }
  }
  map<Id,Double> OpportunityMap = new map<Id,Double> ();
  for(AggregateResult q : [select Opportunity__c,sum(Percent__c) from Opportunity_Splits__c where Opportunity__c IN :OpportunityIds group by Opportunity__c]){
      OpportunityMap.put((Id)q.get('Opportunity__c'),(Double)q.get('expr0'));
  }
  List<Opportunity> OpportunitiesToUpdate = new List<Opportunity>();
  for(Opportunity o : [Select Id, Total_Payments__c from Opportunity where Id IN :OpportunityIds]){
    Double PaymentSum = OpportunityMap.get(o.Id);
    o.Total_Payments__c = PaymentSum;
    OpportunitiesToUpdate.add(o);
  }  
  if(OpportunityIds.size()>0)
    for(Opportunity_Splits__c objC : trigger.new)
    {
       if(OpportunityMap .containskey(objC.Opportunity__c))
       {               
        if(OpportunityMap .get(objC.Opportunity__c) > 100)
        {
            objC.addError('Parent Opportunity Sum is greater than 100');
        }                
       }
      update OpportunitiesToUpdate;
    }
}

Error:

Validation Errors While Saving Record(s) There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger OpportunityRollUpPayments caused an unexpected exception, contact your administrator: OpportunityRollUpPayments: execution of AfterDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.OpportunityRollUpPayments: line 26, column 1".

  • Hi Shweta, what is the error you are getting? – Nitish Singhal May 31 '17 at 6:29
  • Validation Errors While Saving Record(s) There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger OpportunityRollUpPayments caused an unexpected exception, contact your administrator: OpportunityRollUpPayments: execution of AfterDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.OpportunityRollUpPayments: line 26, column 1". – shweta May 31 '17 at 6:35
  • 1
    @shweta you are getting error in OpportunityRollUpPayments trigger and you have posted code of ContactTrigger . This will not help. – Tushar Sharma May 31 '17 at 6:43
  • please post the correct trigger code.. or both of them. You can edit you question again and include both codes. – Nitish Singhal May 31 '17 at 6:45
  • Could you please post error logs from debug logs? – user4567570 May 31 '17 at 6:46
0

Try this ...

trigger OpportunityRollUpPayments on Opportunity_Splits__c(after delete, after insert, after update) {

  set<Id> OpportunityIds = new set<Id>();
  List<Opportunity_Splits__c> opportunitySplitList = new List<Opportunity_Splits__c>();

    if(trigger.isInsert || trigger.isUpdate)
    {
        for(Opportunity_Splits__c p : trigger.new)
        {
            OpportunityIds.add(p.Opportunity__c);    
            opportunitySplitList.add(p);
        }
    } 

    if(trigger.isDelete)
    {
        for(Opportunity_Splits__c p : trigger.old)
        {
            OpportunityIds.add(p.Opportunity__c);    
            opportunitySplitList.add(p);
        }
    }

    Map<Id,Double> OpportunityMap = new Map<Id,Double> ();
    for(AggregateResult q : [select Opportunity__c,sum(Percent__c) from Opportunity_Splits__c where Opportunity__c IN :OpportunityIds group by Opportunity__c])
    {
      OpportunityMap.put((Id)q.get('Opportunity__c'),(Double)q.get('expr0'));
    }

    List<Opportunity> OpportunitiesToUpdate = new List<Opportunity>();

    for(Opportunity o : [Select Id, Total_Payments__c from Opportunity where Id IN :OpportunityIds])
    {
        if(OpportunityMap.get(o.Id) <> null)
        {        
            Double PaymentSum = OpportunityMap.get(o.Id);
            o.Total_Payments__c = PaymentSum;
            OpportunitiesToUpdate.add(o);
        }
    }  

    for(Opportunity_Splits__c objC : opportunitySplitList)
    {
       if(OpportunityMap.containskey(objC.Opportunity__c))
       {               
            if(OpportunityMap.get(objC.Opportunity__c) > 100)
            {
                objC.addError('Parent Opportunity Sum is greater than 100');
            }           
       }
    }

    update OpportunitiesToUpdate;
}
  • this code work fine according to my requirement ,Thanks .. – shweta May 31 '17 at 7:26
1
if(OpportunityIds.size()>0)
    for(Opportunity_Splits__c objC : trigger.new)
    {

You are getting error in for loop Trigger.new because in case of after delete we don't get value in Trigger.New. So you need to put a check for that

if(OpportunityIds.size()>0  && !trigger.isDelete)
        for(Opportunity_Splits__c objC : trigger.new)
        {
  • thanks, this work fine.bt when i am deleting opp split record ,this will not update the Total_Payments__c on opportunity. – shweta May 31 '17 at 7:09
  • @shweta you can use Trigger.old for that. – Tushar Sharma May 31 '17 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.