+ Start a Discussion
sales@myvarmasales@myvarma 

trigger to split annual revenue in account to different opportunities of that account

how to get annual revenue in account  and split to opportunities under that account 
using trigger plzzzzzzzzz help me
Best Answer chosen by sales@myvarma
Nish321Nish321
Hi Pradeep,
You may receive an error if you try to update the account with Annual revenue as blank value.   Use the below code:
 
trigger SplitRevenueOpps on Account (after update) {

set<id> accids = new set<id> ();
List<opportunity> opps=new List<opportunity>();

for(account acc: trigger.new)

  { 
    accids.add(acc.id);
  
  }
  
list<account> aclist = [select id, annualrevenue, (select id, amount from opportunities) from account where id in: accids];

for(account acc1 : aclist)

  {
  
  integer size = acc1.opportunities.size();
  
  for(opportunity opp : acc1.opportunities)
    {  
    
     if(acc1.annualrevenue!=null)
     {
  
      opp.amount = acc1.annualrevenue/size; 
      opps.add(opp);
     }
  }  
  

   }
 if(opps.size()>0)  
 update opps;   
 }

 

All Answers

Harshit Garg 6Harshit Garg 6
Hi Pradeep,

Please confirm me, you want total opportunity amount sum in annual revenue field which is corresponding to that account.

regards,
Harshit garg
sales@myvarmasales@myvarma
yes bro 
the ammount in account object must split the total amount if 3 opp under that account 100/3
 
sales@myvarmasales@myvarma
no bro the annual revenue of account should split for all opportunities under that account.
Harshit Garg 6Harshit Garg 6

you fill the annual revenue manually?

If I am not wrong, suppose on account have annual revenue is 1000 and 4 opportunities have corresponded to that account.
you want 250 in every opportunity amount.

sales@myvarmasales@myvarma
yes bro 
sales@myvarmasales@myvarma
help me @harshit Gang 6
Harshit Garg 6Harshit Garg 6
Give me 1 hour bro...Don't take tension.
Harshit Garg 6Harshit Garg 6

Please follow the below trigger.

trigger Accounttrigger on Account ( after update) {
    List<Account> accounts=new List<Account>();
    List<opportunity> opps=new List<opportunity>();
    
    accounts= [Select id,AnnualRevenue,(select id,Amount from opportunities) from Account where id in:trigger.new];
    
    for(Account ao: accounts){
        integer size= ao.Opportunities.size();
        
        for (Opportunity child : ao.Opportunities)
        {
           
             child.amount=ao.AnnualRevenue/size;
             opps.add(child);
        }
    }
    if(opps.size()>0)
    update opps;
}

If that helps you..please choose my ans as the best ans.

Regards,
Harshit garg
harshitgarg2591@gmail.com
Harshit Garg 6Harshit Garg 6
if you need any help regarding that...mail me on given email id. 
Nish321Nish321
Hi Pradeep,
You may receive an error if you try to update the account with Annual revenue as blank value.   Use the below code:
 
trigger SplitRevenueOpps on Account (after update) {

set<id> accids = new set<id> ();
List<opportunity> opps=new List<opportunity>();

for(account acc: trigger.new)

  { 
    accids.add(acc.id);
  
  }
  
list<account> aclist = [select id, annualrevenue, (select id, amount from opportunities) from account where id in: accids];

for(account acc1 : aclist)

  {
  
  integer size = acc1.opportunities.size();
  
  for(opportunity opp : acc1.opportunities)
    {  
    
     if(acc1.annualrevenue!=null)
     {
  
      opp.amount = acc1.annualrevenue/size; 
      opps.add(opp);
     }
  }  
  

   }
 if(opps.size()>0)  
 update opps;   
 }

 
This was selected as the best answer