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
Yokesh Narayanan KYokesh Narayanan K 

Trigger on opportunity for max amount

Create a field high Opportunity amount on Account object and write a trigger on Opportunity to populate the highest amount among all opportunity for a single  Account on Account Object 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Yokesh,

Refer the below link have solution.
https://developer.salesforce.com/forums/?id=906F00000008yLMIAY

https://salesforce.stackexchange.com/questions/201083/how-to-find-max-of-amount-of-opportunity-related-to-account

If still facing issue, let me know will help you with code.

Thanks!!
Yokesh Narayanan KYokesh Narayanan K
Hi Ankaiah, 
I saw these articles above mentioned, but still I didn't get enough clarification to write a code for bulkification.

This is my code,
List<Account> lstAcc= new List<Account>();
        for(Opportunity opp:trigger.new)
        {
            set<Id> accIds = new set<Id>();
            Account acc = [SELECT Id FROM Account WHERE Id =:opp.AccountId];
            List<AggregateResult> highAmount = [SELECT MAX(Amount) High_Amount 
                                                FROM Opportunity
                                                WHERE AccountId =:acc.Id];
        }
Kinda stuck here , since for bulkfication we cant use soql in for loop .
AnkaiahAnkaiah (Salesforce Developers) 
Hi Yokesh,

Instead of trigger, You can create a rollup summary field on Account object.

Field Name :  high Opportunity amount 
Data type: Rollup summary

Refer the below screenshot.
User-added image

If you still need to do this using trigger then let me know.

If this helps, Please mark it as best answer.

Thanks!!
 
AnkaiahAnkaiah (Salesforce Developers) 
If you want to do it using trigger then use the below code.
 
trigger oppmaxamount on Opportunity (After insert,after update){
    set<id> accids = new set<id>();
    for(opportunity opp:trigger.new){
        
        if(opp.AccountId!=Null){
          accids.add(opp.AccountId) ; 
        }
        
    }
    
    List<account> acclistupdate = new List<account> ();
    
    for(account acc :[select id,high_Opportunity_amount__c,(select id,amount from Opportunities order by Amount DESC limit 1) from account where id=:accids]){
        
        acc.high_Opportunity_amount__c = acc.Opportunities[0].amount;
        acclistupdate.add(acc);
        
    }
    
    Update acclistupdate;
    

}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
mukesh guptamukesh gupta
Hi Yokesh,

Please use below code:-
 
trigger oppmaxamount on Opportunity (After insert,after update){
    set<id> accids = new set<id>();
    for(opportunity opp:trigger.new){
        
        if(opp.AccountId!=Null){
          accids.add(opp.AccountId) ; 
        }
        
    }
    
    List<account> acclistupdate = new List<account> ();
    
    AggregateResult[] oppResults = [SELECT AccountId, MAX(amount) FROM Opportunity where Id IN:accids GROUP BY AccountId];
    
    Map<Id,decimal> oppMaxAmount = new Map<Id,Decimal>();
   for (AggregateResult ar : oppResults)  {
       String accId = (String)ar.get('AccountId');
       Decimal amt = (Decimal)ar.get('expr0');
       oppMaxAmount.put(accId,amt);
   }
    
   List<Account> accList = [Select Id, Opp_Max_Amount__c Name from Account where Id IN: oppMaxAmount.keySet()];
    
    for(Account acc: accList){
        acc.Opp_Max_Amount__c = oppMaxAmount.get(acc.Id);
    }
    if(accList.size() > 0)
     update accList;
     
    

}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh