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
VisithraVisithra 

Wen an opportunity created, update the highest opportunity on account record.

Here is my code, 
//1/to get parent unique id
Set<Id> accountIDs=new Set<Id>();
            for(Opportunity oppRecord:Trigger.new)
            {
                if(oppRecord.AccountId!=null)
                    accountIDs.add(oppRecord.AccountId);
            }
//2. parent to child relationship 
            List<Account> accountList=[Select ID, Rating, Highest_opp_record__c,
                                       (Select ID, StageName, Amount,AccountID FROM Opportunities Where Amount>=50000)
                                       FROM Account Where ID in:accountIDs
                                       ];
//3. update the amount in account record
            List<Account> accList=new List<Account>();                      
            for(Account accRecord:accountList)
            {
                accRecord.Highest_opp_record__c=(accRecord.Opportunities).Amount;
                accList.add(accRecord);
                
            }
            if(accList.size()>0)
                update acclist;
        }
While creating a record in opportunity am facing the below error:
System.QueryException: List has more than 1 row for assignment to SObject Trigger.OpportunityTriggerAmount: line 27, column 1
 
SwethaSwetha (Salesforce Developers) 
HI ,

Can you modify your code to handle multiple Opportunity records for each Account and determine the highest amount among them as :
Set<Id> accountIDs = new Set<Id>();
for (Opportunity oppRecord : Trigger.new) {
    if (oppRecord.AccountId != null) {
        accountIDs.add(oppRecord.AccountId);
    }
}

List<Account> accountList = [
    SELECT Id, Rating, Highest_opp_record__c,
        (SELECT ID, StageName, Amount, AccountID FROM Opportunities WHERE Amount >= 50000 ORDER BY Amount DESC LIMIT 1)
    FROM Account
    WHERE ID IN :accountIDs
];

List<Account> accList = new List<Account>();
for (Account accRecord : accountList) {
    if (accRecord.Opportunities != null && !accRecord.Opportunities.isEmpty()) {
        accRecord.Highest_opp_record__c = accRecord.Opportunities[0].Amount;
    } else {
        accRecord.Highest_opp_record__c = null;
    }
    accList.add(accRecord);
}

if (!accList.isEmpty()) {
    update accList;
}

If this information helps, please mark the answer as best. Thank you
 
CharuDuttCharuDutt
Hii Sf Dev 
You Can Try Below Code
trigger on Opportunity opportunityTrigger(After Insert){
    Set<Id> accountIDs = new Set<Id>();
    for (Opportunity oppRecord : Trigger.new) {
        if (oppRecord.AccountId != null) {
            accountIDs.add(oppRecord.AccountId);
        }
    }
    
    List<Account> accountList = [SELECT Id, Rating, Highest_opp_record__c,
                                 (SELECT ID, StageName, Amount, AccountID FROM Opportunities ORDER BY Amount DESC LIMIT 1)
                                 FROM Account
                                 WHERE ID IN :accountIDs
                                ];
    
    List<Account> accList = new List<Account>();
    for (Account accRecord : accountList) {
        if (accRecord.Opportunities != null && !accRecord.Opportunities.isEmpty()) {
            accRecord.Highest_opp_record__c = accRecord.Opportunities[0].Amount;
        } else {
            accRecord.Highest_opp_record__c = null;
        }
        accList.add(accRecord);
    }
    
    if (!accList.isEmpty()) {
        update accList;
    }
}
Please Mark it As Best Answer If It Helps You
Thank You!