You need to sign in to do that
Don't have an account?
SambitNayak
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;
}
}
}
}
}
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;
}
}
}
}
}
Try Below Code Please Mark It As Best Answer If It helps
Thank You!
All Answers
Try Below Code Please Mark It As Best Answer If It helps
Thank You!
Let's Check here : Pizza Hut survey
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.
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;
}
}
https://howtoguidex.com/
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/)
https://mymcdstuff.com
https://payslipview.xyz
https://oursainsburys.net
https://worksmartmichaelsetm.com/ https://streamlineqldhealth.com/ https://oursainsburyss.com/https://oursainsburys.net/