I have a requirement inwhich I am calculating field 'Net revenue Impact' field of all Opportunity products and display it in Summary field 'Net Rev Impact on FY' on Opportunity Object.
Below is the apex code that I have written on OpportunityLineItem Trigger:
public with sharing class OpportunityLineItemServices {
public static void getNetRevImpact(Id optId) {
map<id,Decimal> oppidtoopplineitm =new map<id,Decimal>();
Set<Id> opportunitiesId=new Set<Id>();
list<Opportunity> finalOpportunity = new list<Opportunity>();
Decimal sum=0.00;
for(OpportunityLineItem lineItem:[Select Id,OpportunityId,Net_Rev_Impact_on_FY__c from OpportunityLineItem where OpportunityId=:optId ])
{
opportunitiesId.add(lineItem.OpportunityId);
sum =(sum+lineItem.Net_Rev_Impact_on_FY__c).setScale(2);
oppidtoopplineitm.put(lineItem.OpportunityId,sum);
}
Map<Id,Opportunity> idToOpportunity=new Map<Id,Opportunity>([SELECT Id,Net_Rev_Impact_on_FY__c FROM Opportunity WHERE Id =:opportunitiesId]);
for(id oId: oppidtoopplineitm.keyset()){
idToOpportunity.get(oId).Net_Rev_Impact_on_FY__c = oppidtoopplineitm.get(oId);
finalOpportunity.add(idToOpportunity.get(oId));
}
update finalOpportunity;
}
This code is working fine.When I add or Update or delete a product,it calculates accordingly and display it Summary field.
But I have a additional requirement inwhich when I change the ContractStartDate or ContractEndDate on Opportunity Object then it should calculate all the products accordingly and Update the Summary Field. For this I have written another same piece of code on Opportunity Trigger:
public static void getNetRevImpactOpp(Opportunity opt) {
map<id,Decimal> oppidtoopplineitm =new map<id,Decimal>();
list<Opportunity> finalOpportunity = new list<Opportunity>();
Decimal sum=0.00;
for(OpportunityLineItem lineItem:[Select Id,OpportunityId,Net_Rev_Impact_on_FY__c from OpportunityLineItem where OpportunityId=:opt.Id ])
{
sum =(sum+lineItem.Net_Rev_Impact_on_FY__c).setScale(2);
oppidtoopplineitm.put(lineItem.OpportunityId,sum);
}
for(id oId: oppidtoopplineitm.keyset()){
opt.Net_Rev_Impact_on_FY__c = oppidtoopplineitm.get(oId);
}
When I Change the ContractStartDate or ContractEndDate,it does not update the value in OpportunityLineItem and due this it does not show the correct value in Summary field.But when I click on refresh button,it displays the correct result which is strange. I have tested the functionality 15 days before and it was working absolutely fine.But I am not sure what has suddenly updated in salesforce that cause this weird issue.
Anybody has an idea?
What modification needs to be done in this code..