I have created following APEX handler class for salesforce opportunityLineItemSchedule object for the validation of sum of gross revenue will not be grater then the sum of all schedule revenue.
`public class OpportunityLineItemScheduleHandler {
public static void handleBeforeInsertUpdate(List<OpportunityLineItemSchedule> newSchedules) {
Set<Id> oppLineItemIds = new Set<Id>();
Map<Id, OpportunityLineItem> oppLineItems = new Map<Id, OpportunityLineItem>();
// Collect OpportunityLineItem Ids from new schedules
for (OpportunityLineItemSchedule schedule : newSchedules) {
oppLineItemIds.add(schedule.OpportunityLineItemId);
}
// Query for OpportunityLineItems using the collected Ids
oppLineItems.putAll([SELECT Id, Gross_Revenue_Calc__c
FROM OpportunityLineItem
WHERE Id IN :oppLineItemIds]);
for (OpportunityLineItemSchedule schedule : newSchedules) {
OpportunityLineItem oppLineItem = oppLineItems.get(schedule.OpportunityLineItemId);
Decimal sumOfRevenue = getSumRevenueOnSchedule(schedule.OpportunityLineItemId);
System.debug(sumOfRevenue);
if (oppLineItem != null && sumOfRevenue > oppLineItem.Gross_Revenue_Calc__c) {
schedule.addError('Sum of Product Schedule should not exceed Gross Revenue on Opportunity Product.');
}
}
}
private static Decimal getSumRevenueOnSchedule(Id OpportunityLineItemId) {
Decimal scheduleRevenue = 0;
List<OpportunityLineItemSchedule> relatedSchedules = [SELECT Revenue FROM OpportunityLineItemSchedule WHERE OpportunityLineItemId = :OpportunityLineItemId];
for (OpportunityLineItemSchedule innerSchedule : relatedSchedules) {
scheduleRevenue += innerSchedule.Revenue;
}
return scheduleRevenue;
}
}
`
I am not able to understand that why I am getting o from getSumRevenueOnSchedule() function instead of getting sum of all revenue. Need help here
Thanks advance
In before insert
of OpportunityLineItemSchedule stuff is not in the database yet. SELECT Revenue FROM OpportunityLineItemSchedule
will not return "current" records being inserted. It will return what's been inserted in the past (if anything) but nothing similar to trigger.new
will be included. If you know how to capture debug logs you'll probably see that the query returns 0 rows.
before update
is slightly better, it should start returning something - but it'll still be wrong. Your query will return what was in the database before this edit happened. So you will rollup old value of revenue, not what the user actually changed in the edit. If they changed a field other than revenue - great, it'll correct itself. If they changed revenue from 50 to 100 -> your query will report the 50, old value.
"Befores" are typically good for data quality checks (that are too complex for validation rules) and field prepopulation. "Side effects" like rollups, creation of child records, making callouts to external systems are better fit in "after" triggers. Try to rearrange your calls in trigger to execute your function as after insert, after update
(and add the keywords to trigger's top line!) and see if it behaves better.