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
Arjun Turke 8Arjun Turke 8 

I have below objects Account Object. Field 1: Total Amount | Total_Amount__c Oppotunity Object. Field : Amount | Amount | Currency Account(Master), Oppotunityetail) My usecase. i want to show total of all Amount(opp) in Total_Amount__c(Account)

Hello,
I have below objects
Account Object.
Field 1: Total Amount | Total_Amount__c 
Oppotunity Object.
Field : Amount | Amount | Currency 
Account(Master), Oppotunityetail)
My usecase.
i want to show total of all Amount(opp) in to field Total_Amount__c(Account)
AnkaiahAnkaiah (Salesforce Developers) 
Hi Arjun,

You can create a rollup summary field(Total_Amount__c ) on Account object to get sum of all opp related to account.

If this helps, Please mark it as best answer.

Thanks!!
Arjun Turke 8Arjun Turke 8
Thank You Ankaia, But what if we do not have any master detail relationship between above 2 objects suppose we have lookuop relationship between Account and Opportunity. Could you please tell me in how to do is in coding not in configiration?
AnkaiahAnkaiah (Salesforce Developers) 
Hi Arjun,

In the backend you have master-detail relationship between Account and opportunity and you can able to create rollup summary fied on Account to get the sum of all opportunities.

Can check and verify.

If you still require code then let me know.

Thanks!!
AnkaiahAnkaiah (Salesforce Developers) 
Hi Arjun,

You can try with below code.
trigger OppAmountonAccount on Opportunity ( after insert, after update, after delete) {

    Map<Id, List<Opportunity>> acctoopp = new Map<Id, List<Opportunity>>();
    Set<Id> acctIds = new Set<Id>();
    List<Opportunity> opptyList = new List<Opportunity>();
    if(trigger.isUpdate || trigger.isInsert){
        for(Opportunity oppty : trigger.New){
            if(oppty.AccountId != null){
                acctIds.add(oppty.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Opportunity oppty : trigger.old){
            if(oppty.AccountId != null){
                acctIds.add(oppty.AccountId);
            }
        }    
    }
    if(acctIds.size() > 0){
        opptyList = [SELECT Amount, AccountId FROM Opportunity WHERE AccountId IN : acctIds];
        for(Opportunity oppty : opptyList){
            if(!acctoopp.containsKey(oppty.AccountId)){
                acctoopp.put(oppty.AccountId, new List<Opportunity>());
            }
            acctoopp.get(oppty.AccountId).add(oppty); 
        }   
        List<Account> acctList = new List<Account>();
        acctList = [SELECT Total_Amount__c  FROM Account WHERE Id IN: acctIds];
        for(Account acct : acctList){
            List<Opportunity> opplist = new List<Opportunity>();
            opplist = acctoopp.get(acct.Id);
            Double oppyamount = 0;
            for(Opportunity oppty : opplist){
                if(oppty.Amount != null){
                    oppyamount += oppty.Amount;
                }
            }
            acct.Total_Amount__c  = oppyamount;
        }
        update acctList;
    }

}

https://developer.salesforce.com/forums/?id=9062I000000BkJwQAK

If this helps, Please mark it as best answer.

Thanks!!
CharuDuttCharuDutt
Hii Arjun
Try Below Trigger
trigger SumAmount on Opportunity (After insert,After Update){
	List<Account> accList=new List<Account>();
    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isAfter){
         if(trigger.isInsert){
        for(Opportunity con : Trigger.new){
            if(Con.AccountId != null){
            setAccIds.add(con.AccountId);
				
            	}
			}
		}else if(trigger.isupdate ){
        for(Opportunity con : Trigger.new){
            if(Con.AccountId != null && (Con.AccountId != trigger.OldMap.get(Con.Id).AccountId)||Con.Amount != trigger.OldMap.get(Con.Id).Amount)){
            setAccIds.add(con.AccountId);
				
            	}
			}
		}
    } 
  
    for(Account acc :[Select id, Total_Amount__c,(Select id,AccountId,Amount from Opportunities) from Account where Id in : setAccIds]){
 		decimal sumAmount;
        for(Opportunity Con :acc.Opportunities){
			sumAmount+=Con.Amount;

        }
		acc.SumCLosedAmount = sumAmount;
		acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList;     
    }
   
}
Please Mark It As Best Asnwer If It Helps
Thank You!
Arjun Turke 8Arjun Turke 8
Hi Ankaish,

Yes it's master detail relationship between Account and Opportunity. And i know it is possible by configuration.
But i want a code suppose we have lookup relationship between any 2 objects.
Then we need to do it by customization not by configuration.That's why asking for code.

Thanks!