Skip to main content
clarified the where each code resides
Source Link
TheArchitecta
  • 1.5k
  • 1
  • 19
  • 53

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);
}
        

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.

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.

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);
}
        

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);
}
        
map data in a handler object and return it for processing
Source Link
TheArchitecta
  • 1.5k
  • 1
  • 19
  • 53

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 popstpost but not enough reputation. To add to martins code: Within the recalculate method pass the list of OpportunitiesOpportunity Ids that need recalculation. You

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 should be doing the samesomething 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.

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);
}
        

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 popst but not enough reputation. To add to martins code: Within the recalculate method pass the list of Opportunities that need recalculation. You first Trigger should be doing the same. So if you have to rewrite the first trigger it is worthwhile.

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.

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.

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);
}
        
added 303 characters in body
Source Link
TheArchitecta
  • 1.5k
  • 1
  • 19
  • 53

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 popst but not enough reputation. To add to martins code: Within the recalculate method pass the list of Opportunities that need recalculation. You first Trigger should be doing the same. So if you have to rewrite the first trigger it is worthwhile.

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.

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 popst but not enough reputation. To add to martins code: Within the recalculate method pass the list of Opportunities that need recalculation. You first Trigger should be doing the same. So if you have to rewrite the first trigger it is worthwhile.

Source Link
TheArchitecta
  • 1.5k
  • 1
  • 19
  • 53
Loading