You need to sign in to do that
Don't have an account?
priyanka mohapatra 10
Opportunity Trigger not displaying correct value on Parent Account
I am trying to find the highest Opportunities' Amount of an Account and want to reflect the value in one of the fields in Account, 'Highest_Opportunity_Amount__c'.
For that I have written an After Trigger on Opportunity and written below method in trigger Handler class. But my value on the 'Highest_Opportunity_Amount__c' field of that Account is not reflecting the Highest amount.
public static void highestOpportunityinsameAccount(list<Opportunity> lstOpportunity,boolean isInsert,boolean isUpdate, boolean isAfter){
List<Account> lstAccounts = New List<Account>();
set<id> setAccId=new set<id>();
map<id,list<Opportunity>> mapAccIdOpp=new map<id,list<Opportunity>>();
for(Opportunity oppr:lstOpportunity){
setAccId.add(oppr.AccountId);
}
if(setAccId.size()>0){
for (account acc:[SELECT Id,(SELECT ID,Amount FROM Opportunities),Highest_Opportunity_Amount__c FROM Account WHERE Id IN:setAccId]){
mapAccIdOpp.put(acc.id, acc.opportunities);
}}
for(Opportunity NewOppr:lstOpportunity){
decimal amount=0;
if(mapAccIdOpp.containsKey(NewOppr.AccountId)){
for(Opportunity ExistOppr:mapAccIdOpp.get(NewOppr.AccountId)){
amount=amount>ExistOppr.Amount?amount:ExistOppr.Amount;
}
if(NewOppr.Account.Highest_Opportunity_Amount__c!= Null){
NewOppr.Account.Highest_Opportunity_Amount__c=amount;
}
}
}
}
For that I have written an After Trigger on Opportunity and written below method in trigger Handler class. But my value on the 'Highest_Opportunity_Amount__c' field of that Account is not reflecting the Highest amount.
public static void highestOpportunityinsameAccount(list<Opportunity> lstOpportunity,boolean isInsert,boolean isUpdate, boolean isAfter){
List<Account> lstAccounts = New List<Account>();
set<id> setAccId=new set<id>();
map<id,list<Opportunity>> mapAccIdOpp=new map<id,list<Opportunity>>();
for(Opportunity oppr:lstOpportunity){
setAccId.add(oppr.AccountId);
}
if(setAccId.size()>0){
for (account acc:[SELECT Id,(SELECT ID,Amount FROM Opportunities),Highest_Opportunity_Amount__c FROM Account WHERE Id IN:setAccId]){
mapAccIdOpp.put(acc.id, acc.opportunities);
}}
for(Opportunity NewOppr:lstOpportunity){
decimal amount=0;
if(mapAccIdOpp.containsKey(NewOppr.AccountId)){
for(Opportunity ExistOppr:mapAccIdOpp.get(NewOppr.AccountId)){
amount=amount>ExistOppr.Amount?amount:ExistOppr.Amount;
}
if(NewOppr.Account.Highest_Opportunity_Amount__c!= Null){
NewOppr.Account.Highest_Opportunity_Amount__c=amount;
}
}
}
}
Please try the below code:
Thanks,
Maharajan.C
All Answers
Please try the below code:
Thanks,
Maharajan.C