2

I have an opportunity trigger that rolls up the amounts of all the opportunities that have a lookup relationship to a custom object.

  • All opportunities have a lookup field to our custom object: Projects. When on a Project record, can see the roll up of all the Opps that are related to that Project.

  • This rollup is managed by an Opportunity trigger. I have now created a new custom field on the Opportunity, and a new custom field on Projects. I want my new custom fields to also rollup from the Opps to the Projects.

  • Bearing in mind I'm a complete novice with triggers, would be be easier/better for me to edit the original trigger to include the two new fields and duplicate the behaviour, or should I clone the entire trigger and just replace the field names with my new ones, which means I'll have two triggers running simultaneously.

  • 1
    Few triggers is bad practise because you dont know it which order they would be executed, which may cause issues in future. If you use your trigger only for rollups summary, please consider next tool - github.com/afawcett/declarative-lookup-rollup-summaries. That allow to do some rollup things by point to click setup – kurunve Mar 7 '16 at 10:27
2

It is always recommended to have one trigger per object because in the case of multiple triggers, Salesforce does not guarantee the order of execution. This can have unprecedented consequences. You can have a trigger handler class and invoke methods from that class in your trigger, thus it becomes as efficient process to streamline the whole trigger execution context.

This is also important from the context of managing governor limits. All the triggers together are entitled to the total limits for the transaction ex 101 SOQL. This it is easier to manage the impact on governor limit and keep code safe, if we follow one trigger per object pattern.

Please see regarding the best practise of streamlining triggers. - https://developer.salesforce.com/page/Best_Practice:_Streamline_Multiple_Triggers_on_same_Object

Summarising, under any condition go for one trigger per object. Hope this helps

|improve this answer|||||
  • Unintended consequences? I do believe there is precedent. – Adrian Larson Mar 7 '16 at 13:10
  • @AdrianLarson Yeah my bad ;) . There is precident – Prajith Mar 7 '16 at 14:36
0

I believe it will be better to do it in a single trigger as you might have at-least one query in older trigger and cloning the trigger will increase queries, DML's and recursive effect will be large.

I assume that the trigger is written on after insert and update on Opportunity and in the trigger you are creating a List or Set of Project Ids and then querying all the Opportunities under the project Ids. After that you are iterating over the opportunities one by one and summarizing the field.

You can add your logic in the same for loop.

|improve this answer|||||
0

You can do this within the same code. So right now You would we querying Product along with Product.OLD_Field where you will assign the accumulated value of Opportunity.OLD_Field (i.e. Product.OLD_Field=Opportunity1.OLD_Field+Opportunity2.OLD_Field..etc).

now while querying the product you just need to add one more field in Product query Product.NEW_Field where you will assign the accumulated value of Opportunity.NEW_Field which can reuse the same code.

so all u have to do is 1. Add new filed in product query 2. add a line to accumulated the value of Opportunity.NEW_Field(you can use the same line which is doing this job for old field just by replacing opportunity field name and by saving accumulate value to new variable)

  1. Add one more line for assigning the accumulated value of Opportunity.NEW_Field to Product.NEW_Field(you can use the same line which is doing this job for old field just by replacing Product field name and assigning new variable to Product field)
|improve this answer|||||

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.