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
sales@myvarmasales@myvarma 

annual revenue division to opportunities

i want to add annual revenue amount in the  account object to opportunities equally  in that account 
help me with program
urgent now
Best Answer chosen by sales@myvarma
Ajay K DubediAjay K Dubedi
Hi Pradeep,

You can do it by a trigger. Here is the piece of code to show you example.
I will suggest you to try below code:
trigger updateAccountRevenue on Opportunity (after insert, after update) {
 List<Id> toUpdate = new List<Id>();
 List <Account> toUpdateDML = new List<Account>();
   Map<Account,List<Opportunity>> accToOpp = new map <Account,List<Opportunity>>();
   public Decimal annualrevenue;
   for(Opportunity o : trigger.new) {
       if(o.StageName == 'Closed Won') {
       toUpdate.add(o.AccountId);
       }
   }
   for(Account a : [SELECT Id,AnnualRevenue,(SELECT ID,Amount,StageName FROM Opportunities) from Account WHERE Id IN : toUpdate]) {
       accToOpp.put (a, a.Opportunities);
       annualrevenue=0;
       for(Opportunity ooo : accToOpp.get(a)) {
           if(ooo.StageName == 'Closed Won'){
           annualrevenue= annualrevenue + ooo.Amount;
           }
       }
       a.AnnualRevenue = annualrevenue;
       toUpdateDML.add(a);
   }       
   update toUpdateDML;
}
Hope this will help you.
Please mark my answer if it is helpful.
Regards,
Ajay

All Answers

Ajay K DubediAjay K Dubedi
Hi Pradeep,

You can do it by a trigger. Here is the piece of code to show you example.
I will suggest you to try below code:
trigger updateAccountRevenue on Opportunity (after insert, after update) {
 List<Id> toUpdate = new List<Id>();
 List <Account> toUpdateDML = new List<Account>();
   Map<Account,List<Opportunity>> accToOpp = new map <Account,List<Opportunity>>();
   public Decimal annualrevenue;
   for(Opportunity o : trigger.new) {
       if(o.StageName == 'Closed Won') {
       toUpdate.add(o.AccountId);
       }
   }
   for(Account a : [SELECT Id,AnnualRevenue,(SELECT ID,Amount,StageName FROM Opportunities) from Account WHERE Id IN : toUpdate]) {
       accToOpp.put (a, a.Opportunities);
       annualrevenue=0;
       for(Opportunity ooo : accToOpp.get(a)) {
           if(ooo.StageName == 'Closed Won'){
           annualrevenue= annualrevenue + ooo.Amount;
           }
       }
       a.AnnualRevenue = annualrevenue;
       toUpdateDML.add(a);
   }       
   update toUpdateDML;
}
Hope this will help you.
Please mark my answer if it is helpful.
Regards,
Ajay
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Thanks Pradeep for selecting my answer as a best answer.

Regards,
Ajay