Apex trigger for update totalAmount and totalCogs, SUM amount and cogs from OpportunityLineItem (child) and store the value using AggregateResult
trigger updateTotalAmountAndCogs on Opportunity (before insert, before update) {
for (Opportunity opp : Trigger.new) {
List<Opportunity> opportunities = new List<Opportunity>();
AggregateResult result = [SELECT OpportunityId, SUM(TotalPrice) totalSumAmount, SUM(COGS__c) totalSumCogs
FROM OpportunityLineItem WHERE OpportunityId = :opp.Id GROUP BY OpportunityId];
opp.Total_Amount_2__c = (Decimal) result.get('totalSumAmount');
opp.Total_COGS_2__c = (Decimal) result.get('totalSumCogs');
}
}