So I understand:
You have a formula on OLI.
But only when an OLI is updated does the Opportunity get updated via trigger.
You also need to update the Opp Roll Up when Opp dates are updated.
You should not need to prevent recursion.
On Opp Update you are querying the formulas of the OLI and updating (before update trigger) the Opp.
If you need to update the child (OLI) then this is another story.
In which case you need a trigger handler only allowing 1 pass through.
I can post a raw example code to prevent recursion if you need.
ALSO:
Your first trigger should have helper methods. You should reuse these methods in your 2nd (Opp trigger) if possible to calculate your OLI values.
@Martins Code will work: I would comment on his post but not enough reputation.
To add to martins code:
Within the recalculate method pass the list of Opportunity Ids that need recalculation.
//THIS IS THE HANDLER CLASS TO GATHER THE DATA
OpportunityLineItemHandler{
public map<Id,decimal> recalculate(Set<Id> OppId_Set){
OLIs = [SELECT id, value FROM OpportunityLineItem WHERE OpportunityId IN :OppId_Set];
for(OpportunityLineItem oli :OLIs){
//map each parents summed value Value
OppIdValue_Map.put(oli.opportunityId, sumofchildrensvalue);
}
return OppIdValue_Map;
}
You first Trigger something similar. So if you have to rewrite the first trigger it is worthwhile.
Add to Martins Code the return of the OppIdValueMap and then iterate across Opportunities again to store the values.
//THIS IS IN THE BEFORE TRIGGER TO PROCESS THE DATA
if(oldDateStart != newDateStart || oldDateEnd != newDateEnd){
map<id,decimal> OppIdValue_Map = recalculate(OppId_Set);
}
for(Opportunity opp: trigger.new){
if(OppIdValue_Map.containsKey(opp.Id) opp.sum__c = OppIdValue_Map.get(opp.Id);
}
AfterUpdate trigger
onOpportunity
object to run the similar code you have onOpportunity Line Item
trigger or call the method that does recalculations.