• priyanka mohapatra 10
  • NEWBIE
  • 10 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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;
                }
            }
        }
    }
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;
                }
            }
        }
    }