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
priyanka mohapatra 10priyanka 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;
                }
            }
        }
    }
Best Answer chosen by priyanka mohapatra 10
Maharajan CMaharajan C
Hi Priyanka,

Please try the below code:
 
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>();
        List<Account> accList = new List<Account>();
        
        for(Opportunity oppr:lstOpportunity){
            if(oppr.AccountId != null)
            	setAccId.add(oppr.AccountId);
        }
        
        if(setAccId.size()>0){
            
            List<AggregateResult> aggrList = [select AccountId, max(Amount)amt from Opportunity where AccountId in: setAccId group by AccountId];
            
            if(!aggrList.isEmpty()){ 
                for(AggregateResult aggr:aggrList){ 
					Account acc = new Account();                    
                    acc.Id=(id)aggr.get('AccountId'); 
                    acc.Highest_Opportunity_Amount__c =(decimal)aggr.get('amt');     
                    accList.add(acc);
                }
            }
            
            if(!accList.IsEmpty())
                update accList;
        }
    }

Thanks,
Maharajan.C

All Answers

kate dukekate duke
dial : 1-855-212-3410 If you run into difficulties while using QuickBook Support Number (https://www.bark.com/en/us/company/quickbook-support-number/ElVKy/), you should not be concerned. The client help gathering can be reached through phone. In addition, you can contact the client care bunch in a combination of ways. Coming up next are without a doubt the most worthwhile procedures for arriving at the specific assistance bunch.
Maharajan CMaharajan C
Hi Priyanka,

Please try the below code:
 
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>();
        List<Account> accList = new List<Account>();
        
        for(Opportunity oppr:lstOpportunity){
            if(oppr.AccountId != null)
            	setAccId.add(oppr.AccountId);
        }
        
        if(setAccId.size()>0){
            
            List<AggregateResult> aggrList = [select AccountId, max(Amount)amt from Opportunity where AccountId in: setAccId group by AccountId];
            
            if(!aggrList.isEmpty()){ 
                for(AggregateResult aggr:aggrList){ 
					Account acc = new Account();                    
                    acc.Id=(id)aggr.get('AccountId'); 
                    acc.Highest_Opportunity_Amount__c =(decimal)aggr.get('amt');     
                    accList.add(acc);
                }
            }
            
            if(!accList.IsEmpty())
                update accList;
        }
    }

Thanks,
Maharajan.C
This was selected as the best answer
priyanka mohapatra 10priyanka mohapatra 10
Thanks Maharajan C for the resolution.The code is working perfectly fine.