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".

7
  • Hi Shweta, what is the error you are getting? May 31, 2017 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, 2017 at 6:35
  • 1
    @shweta you are getting error in OpportunityRollUpPayments trigger and you have posted code of ContactTrigger . This will not help. May 31, 2017 at 6:43
  • please post the correct trigger code.. or both of them. You can edit you question again and include both codes. May 31, 2017 at 6:45
  • Could you please post error logs from debug logs? May 31, 2017 at 6:46

2 Answers 2

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;
}
1
  • this code work fine according to my requirement ,Thanks ..
    – shweta
    May 31, 2017 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)
        {
2
  • 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, 2017 at 7:09
  • @shweta you can use Trigger.old for that. May 31, 2017 at 7:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .