I created a trigger that on creation of an OpportunitlyLineItem that looks at the number of OpportunityLineItemSchedule IDs and sets a Duration field (on OpportunityLineItem as the size of the list. This was deployed to production in Nov. 2015 and worked. Now the trigger only fires when the OpportunityLineItem is edited. I can't trace through to figure out what is going on. I tried changing to after insert but it is throwing a System.FinalException: Record is read-only error.
Here is the trigger if someone could please point me in the right direction. Thank you!
trigger NumInstallments on OpportunityLineItem (before insert, before update) {
for(OpportunityLineItem lineItem : Trigger.new){
List<OpportunityLineItemSchedule> numSched = [Select Id From OpportunityLineItemSchedule
Where OpportunityLineItemId = :lineItem.Id];
if (numSched.size() > 0){
lineItem.Duration__c = numSched.size();
} else {
lineItem.Duration__c = null;
}
}
}