• Paul H 5
  • NEWBIE
  • 10 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 0
    Replies
trigger OpportunityTrigger on Opportunity (after insert, after update, after delete, after undelete) {
       
    set<Id> accIds = new set<Id>();
    
      if(trigger.isAfter && trigger.isInsert){
        
        for(Opportunity opp : trigger.new){
            
            if(opp.AccountId != null){
                
                accIds.add(opp.AccountId);
            }
        }
    }
    if(trigger.isUpdate && trigger.isUpdate){
        
        for(Opportunity opp : trigger.new){
            
            if(opp.AccountId != Trigger.oldMap.get(opp.Id).AccountId){
                
                accIds.add(opp.AccountId);
                accIds.add(Trigger.oldMap.get(opp.Id).AccountId);
            }
        }
    }
    
    if(trigger.isAfter && trigger.isDelete){
        for(Opportunity opp : trigger.old){
            if(opp.AccountId != null){
                accIds.add(opp.AccountId);
            }
        }
    }
   
  
         List<AggregateResult> aggrList = [Select Name, AccountId, MAX(Amount) maxAmt from Opportunity
                                          where Amount!=null and AccountId IN: accIds
                                          group by Name, AccountId ORDER BY MAX(Amount) desc limit 1];
        
            List<Account> accList = new List<Account>();
            for(AggregateResult aggr: aggrList){
            Id accId = (Id) aggr.get('AccountId');
            String oppName = (String) aggr.get('Name');
            Decimal maxOppAmount = (Decimal) aggr.get('maxAmt');
            Account acc = new Account(Id= accId,HighestOppAmount__c=oppName);
            accList.add(acc);
        }
        if(accList.size() > 0){
            update accList;
        }
    }
Delete and insert is working fine but I am not getting why my update is not working please check this code 
I tried to implement the above scenarios ,I wanted to know is there any settings in user profile I need to do or this is possible by creating new profile..Help me with the solutions ..Thank you
Can Somebody Explain ?? do we need to change the fields in Contact 
whenever we we change any field in account ?? I am not getting this scenario...