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
dd 

there are two field in contact Amount1 , Amount 2.The same fields are there in account .When a contact with created/edit/delete/undelete,update contacts amount1,amount2 on its related accounts amount1,amount2

 two field in contact Amount1 , Amount 2.The same fields are there in account .When a contact with created/edit/delete/undelete,update contacts amount1,amount2 on its related accounts amount1,amount2
SubratSubrat (Salesforce Developers) 
Hello ,

Can you specify the Exact Requirement .

Thank you.
dd
When amount1,amount2 fields in contact is created or update or deleleted its should update related account amount1 and amount2 field
 
SubratSubrat (Salesforce Developers) 
Hello ,

Please try with the below code and let me know further.
trigger UpdateAccountFieldsOnContact on Contact (after insert, after update, after delete) {
    
    // Collect all account IDs related to the changed contacts
    Set<Id> accountIds = new Set<Id>();
    
    for (Contact c : Trigger.new) {
        accountIds.add(c.AccountId);
    }
    
    // Query for the related accounts and update their fields
    List<Account> accountsToUpdate = new List<Account>();
    
    for (AggregateResult result : [SELECT AccountId, SUM(Amount1_c) totalAmount1, SUM(Amount2_c) totalAmount2
                                   FROM Contact
                                   WHERE AccountId IN :accountIds
                                   GROUP BY AccountId]) {
        
        accountsToUpdate.add(new Account(
            Id = (Id)result.get('AccountId'),
            Amount1__c = (Decimal)result.get('totalAmount1'),
            Amount2__c = (Decimal)result.get('totalAmount2')
        ));
    }
    
    update accountsToUpdate;
}

If it helps please mark this as Best Answer.
Thank you.​​​​​​​