• bitempress
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
 Hi all
I need to write a trigger that would work as a  rollup summary for lookup objects.
The trigger fires after update on objectToRollUp  and  updates  lookup1 lookup2 by adding a sum of updated objectToRollUp related to the lookups.

What is the best approach when if an update to one of lookup fails.
1) simple - in the trigger call
update lookup1list;  
update lookup2list;
and don’t handle exceptions (a batch of 200 records is failed if just one lookupX errors) 
This will automatically roll back all changes to all objects

2)  more efficient way 
Database.update(lookup1list,false) ;
objectToRollUp.addError()  - for  all  records related to lookup1s that failed above

exclude lookup2s related to failed objectToRollUp from the next update.
Database.update(lookup2list,false) ;
Now the problem is that when lookup2list update fails lookup1list  is already committed.
the sum of objectToRollUp is added to lookup1 but not to lookup2.
I would need to wrap the code above around a save point and do a rollback when the second update errors.
But if I do this in a trigger then the calling classes/user interface will not receive any error information because the error will get removed from the objects on rollback. Right?
I think the second solution (the first 2 lines) is fine if there is only one list of reletad records. (though I mostly see the first one)
What do you think?
Hi,

How to terminate schedule jobs which are in queue in schedule apex?

Regards,
Anil