2

This seems like a "relatively simple" venture to me, but at my experience level, I'm highly challenged to get it done. I've touched on a variation of it before.

I have a lengthy Trigger (appx 500 lines) that processes Opportunities and summarizes them at the Account level (useful for our Email marketing and other processes that cannot easily or at all get to the Opportunity data, especially since it's many-to-one).

One shortcoming in it is when an Account is MERGED (let's just say edited to make it more thorough) an Opps from two accounts end up on ONE ACCOUNT, the "piggyback" data is now only from one of those two accounts -- because the Opportunity trigger does not run.

So... what I need to do is have a Trigger, on an Account edit, create a set/list of the Opps, and then run each Opp through the Opportunity Trigger... which is a simple matter of doing an "Edit/Save" on each Opportunity. It doesn't have to "call" the Opp Trigger directly, just create a set of the opps, and edit/save each opp.

  • I'm confused as to where you're stuck here... just create a list of opportunities and insert them to fire the oppty trigger on them! – Matt Lacey Apr 2 '13 at 22:06
  • 1
    I wnn't bother berating my skills too much further. I sometimes wish I were NOT the Admin here so I could fully CONCENTRATE on my Apex. :-( – AMM Apr 3 '13 at 12:50
1

LaceySnr is right, it should be fairly straightforward. You could just use the account trigger to loop through the associated Opportunities and update a field by setting the value to the already existing value. This will cause the Opportunity Trigger to fire and do the work defined there. Something like should work:

Account[] acctsInTriggerSet = new List<Account>();
Opportunity[] opptysToUpdate= new List<Opportunity>();

for(Account a :Trigger.new){
   acctsInTriggerSet.add(a);
}

For( Opportunity o : [SELECT id, Name
                      FROM Opportunity 
                      WHERE Accountid IN :acctsInTriggerSet] ) {
// o.Name = o.Name; 
opptysToUpdate.add(o); } 

update opptysToUpdate;
  • this will work but is not entirely efficient... you are firing the SOQL to find the opps, then his opp trigger likely fires its own SOQL to find its siblings. Plus you are triggering two DML statements, one on account and one on all of those opps. – greenstork Apr 2 '13 at 22:32
  • you don't actually need to change a field for this to work, simply updating the related opptys will do the trick – Ralph Callaway Apr 3 '13 at 3:03
  • I'm working with this code, but am encountering "unexpected token" at the update command at the end. I don't understand this since my limited view of the code tells me it "looks right". – AMM Apr 3 '13 at 13:14
  • I fixed the FOR reference (curly bracket missing, fixed above) but now it doesn't think acctsInTriggerSet exists in the SELECT statement. Urgh. – AMM Apr 3 '13 at 13:24
  • I tried adding List<Id> acctsInTriggerSet = new List<Id>(); but it says "incompatible element type SOBJECT:Account for collection of Id" in the first FOR statement. – AMM Apr 3 '13 at 13:30
2

The best architecture here is to create a helper class with a method that takes a list of Accounts as an argument. This allows you to use the same helper method from your opportunity trigger and from an account trigger.

The challenge is that you don't want to set up a recursion scenario so you need a way to really quickly short-circuit out of the account trigger. One option is to set a static variable that indicates you're updating the account based an an opp change, the variable could be called oppRollupFiring or something. Another option would be to look at only substantive changes to the account record and only continue processing after checking what's changed on the account record and make sure that change should dictate that the trigger fires. The latter is more nuanced, so I recommend the static variable.

On a merge, two events fire, a delete event on the record that did not win and an update event on the winner so your account trigger would need to fire on After Update:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_merge_statements.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.