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
SambitNayakSambitNayak 

trigger to distribute the amount on Parent Account equally among all opportunities

Hi,
I have a requirement to distribute the amount on Parent Account equally among all opportunities. The following code is throwing NULL pointer exception. Please help.


public static void distAmountEqually(List<Opportunity> oppList, map<Id,Opportunity>oldmap, boolean isInsert, boolean isUpdate){
        Set<Id> oppAccIds = New Set<Id>();
        Map<Id,List<opportunity>> mapAccToOpps = New Map<Id,List<opportunity>>();
        Integer noOfOpps = 0;
        List<Opportunity> oppsToUpdt = New List<Opportunity>();

        for(Opportunity opp:oppList){
            oppAccIds.add(opp.accountId);
        }
        
        for(Account acc: [SELECT AnnualRevenue ,(SELECT Distributed_Amount__c FROM Opportunities) 
                          FROM Account WHERE Id IN :oppAccIds]){
            
            if(!mapAccToOpps.containsKey(acc.Id)){ 
                mapAccToOpps.put(acc.Id, new List<opportunity>());                
            }
            mapAccToOpps.get(acc.Id).addall(acc.opportunities);
        }
        
        for(Opportunity op:oppList){
            if(mapAccToOpps.containsKey(op.accountId)){
                 noOfOpps= op.account.Opportunities.size();

                 for(Opportunity opy:mapAccToOpps.get(op.accountId)){
                    opy.Distributed_Amount__c = op.Account.AnnualRevenue / noOfOpps;
                }
            }
        }
    
    }
}
Best Answer chosen by SambitNayak
CharuDuttCharuDutt
Hii Sambit
Try Below Code
public static void distAmountEqually(List<Opportunity> oppList, map<Id,Opportunity>oldmap, boolean isInsert, boolean isUpdate){
    List<Opportunity> oppsToUpdt = New List<Opportunity>();
    
    for(Opportunity opp:oppList){
        
        if(isInsert && opp.accountId != null && oldmap == null){
            
            oppAccIds.add(opp.accountId);
            
        }else if(isUpdate && opp.accountId != null && oldmap != null && opp.accountId != oldmap.get(opp.Id).AccountId){
            
            oppAccIds.add(opp.accountId);
            oppAccIds.add(oldmap.get(opp.Id).AccountId);
            
        }
        
    }
    
    for(Account acc: [SELECT AnnualRevenue ,(SELECT Distributed_Amount__c FROM Opportunities) 
                      FROM Account WHERE Id IN :oppAccIds]){
                          for(opportunity oOpp : acc.Opportunities){
                              oOpp.Distributed_Amount__c = Acc.AnnualRevenue/acc.Opportunities.size();
                              oppsToUpdt.add(oOpp);
                          }
                      }
    if(oppsToUpdt.size()>0){
    	update oppsToUpdt;    
    }
    
    
}
Please Mark It As Best Answer If It helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Sambit
Try Below Code
public static void distAmountEqually(List<Opportunity> oppList, map<Id,Opportunity>oldmap, boolean isInsert, boolean isUpdate){
    List<Opportunity> oppsToUpdt = New List<Opportunity>();
    
    for(Opportunity opp:oppList){
        
        if(isInsert && opp.accountId != null && oldmap == null){
            
            oppAccIds.add(opp.accountId);
            
        }else if(isUpdate && opp.accountId != null && oldmap != null && opp.accountId != oldmap.get(opp.Id).AccountId){
            
            oppAccIds.add(opp.accountId);
            oppAccIds.add(oldmap.get(opp.Id).AccountId);
            
        }
        
    }
    
    for(Account acc: [SELECT AnnualRevenue ,(SELECT Distributed_Amount__c FROM Opportunities) 
                      FROM Account WHERE Id IN :oppAccIds]){
                          for(opportunity oOpp : acc.Opportunities){
                              oOpp.Distributed_Amount__c = Acc.AnnualRevenue/acc.Opportunities.size();
                              oppsToUpdt.add(oOpp);
                          }
                      }
    if(oppsToUpdt.size()>0){
    	update oppsToUpdt;    
    }
    
    
}
Please Mark It As Best Answer If It helps
Thank You!
This was selected as the best answer
nensyi brijaniyanensyi brijaniya
Survey TellPizzaHut is open to your input as well as your opinions and concerns.

Let's Check here :  Pizza Hut survey
SambitNayakSambitNayak
Hi CharuDutt,
Thanks so much for your help. Unfortunately, it's not updating the amount field on opportunities. I tried to figure out the bug, but still unable to.
 
SambitNayakSambitNayak

Hi CharuDutt,
Few modifications I made

trigger opportunityTrigger on Opportunity (after insert, after update) {
    if(RecursiveTriggerHandler.isFirstTime){
        RecursiveTriggerHandler.isFirstTime = false;
opportunityTriggerHandler.distAmountEqually(Trigger.new, Trigger.oldMap, Trigger.isInsert, Trigger.isUpdate);
    }
}
****************************
public class RecursiveTriggerHandler {
public static Boolean isFirstTime = true;
}
****************************
  public static void distAmountEqually(List<Opportunity> oppList, map<Id,Opportunity>oldmap, boolean isInsert, boolean isUpdate){
    List<Opportunity> oppsToUpdt = New List<Opportunity>();
    Set<Id> oppAccIds = New Set<Id>();
        
    for(Opportunity opp:[select id, account.name from opportunity where ID IN:oppList]){
        if(isInsert && opp.accountId != null && oldmap == null){            
            oppAccIds.add(opp.accountId);            
        }else if(isUpdate && opp.accountId != null && oldmap != null){            
            oppAccIds.add(opp.accountId);
            system.debug('opp acc is added '+opp.AccountId);           
        }        
    }
    
    for(Account acc: [SELECT AnnualRevenue ,(SELECT Distributed_Amount__c FROM Opportunities)
                      FROM Account WHERE Id IN :oppAccIds]){
                          for(opportunity oOpp : acc.Opportunities){
                              oOpp.Distributed_Amount__c = Acc.AnnualRevenue/acc.Opportunities.size();
                              oppsToUpdt.add(oOpp);
                          }
                      }
    if(oppsToUpdt.size()>0){
    update oppsToUpdt;    
    }
    
    
}

jeon laejeon lae
Its help me a lot. Thanks Guys....
https://howtoguidex.com/
jeon laejeon lae
public static void distAmountEqually(List<Opportunity> oppList, map<Id,Opportunity>oldmap, boolean isInsert, boolean isUpdate){
        Set<Id> oppAccIds = New Set<Id>();
        Map<Id,List<opportunity>> mapAccToOpps = New Map<Id,List<opportunity>>();
        Integer noOfOpps = 0;
        List<Opportunity> oppsToUpdt = New List<Opportunity>();

        for(Opportunity opp:oppList){
            oppAccIds.add(opp.accountId);
        }
        
        for(Account acc: [SELECT AnnualRevenue ,(SELECT Distributed_Amount__c FROM Opportunities) 
                          FROM Account WHERE Id IN :oppAccIds]){
            
            if(!mapAccToOpps.containsKey(acc.Id)){ 
                mapAccToOpps.put(acc.Id, new List<opportunity>());                
            }
            mapAccToOpps.get(acc.Id).addall(acc.opportunities);
        }
        
        for(Opportunity op:oppList){
            if(mapAccToOpps.containsKey(op.accountId)){
                 noOfOpps= op.account.Opportunities.size();

                 for(Opportunity opy:mapAccToOpps.get(op.accountId)){
                    opy.Distributed_Amount__c = op.Account.AnnualRevenue / noOfOpps;
                }
            }
        }
    
    }
}

This code really helpful. Thaks for sharing this with us.
my view wilko (https://payslipx.com/my-view-wilko/) ess compass associate com (https://esscompassassociatea.com/) mymorri (https://guidesbuddy.com/mymorri-login/) ess compass associate (https://esscompassassociatee.com/
 
jeon laejeon lae
ESS.Compass Associate.Com (https://esscompassassociatex.com/)