I want to replace a workflow field update with an APEX method to update the fields. The reason being is that I want to be able to add months to a date field and with formulas it only allows days to be added to the date.
I wrote this method to be included in the triggerhandler class for OpportunityLineItem.
private void updateRenewalDates(Map<Id, OpportunityLineItem> newMap, Map<Id, OpportunityLineItem> oldMap)
{
// Create Lists to hold data
List<OpportunityLineItem> renewals = new List<OpportunityLineItem>();
Map<Id,List<OpportunityLineItem>> oppToLineItem = new Map<Id,List<OpportunityLineItem>>();
// Loop through filter records from the api call and see if they meet the criteria
for(OpportunityLineItem oli:newMap)
{
if((oli.Start_Date__c == oldMap.oli.Start_Date__c) || (oli.End_Date__c == oldMap.oli.End_Date__c))
{
if(oppToLineItem.get(oli.OpportunityId)==null)
{
// If they do meet the criteria then add them to the list to update
List<OpportunityLineItem> oliLst = new List<OpportunityLineItem>();
oliLst.add(oli);
oppToLineItem.put(oli.OpportunityId,oliLst);
}
else
{
oppToLineItem.get(oli.OpportunityId).add(oli);
}
}
}
// Get the record fields for records where the id is from our list
renewals = [SELECT Id, Start_Date__c, Start_Date_Renewal__c, End_Date__c, End_Date_Renewal__c, Opportunity.ContractTerm__c
FROM OpportunityLineItem
WHERE Id in:oppToLineItem.keyset()];
// Loop through results of the query and update the record with the new value
for(OpportunityTeamMember oppLineItem:renewals)
{
List<OpportunityLineItem> olis = oppToLineItem.get(oppLineItem.OpportunityId);
for(OpportunityLineItem line:olis)
{
// Update Renewal dates with the start/end date + Contract length
line.Start_Date_Renewal__c = oli.Start_Date__c.addMonths(12);
line.End_Date_Renewal__c = oli.End_Date__c.addMonths(12);
}
}
}
When i try to save the class and have it compile it gives the error:
Initial term of field expression must be a concrete SObject: Map
On the line:
if((oli.Start_Date__c == oldMap.oli.Start_Date__c) || (oli.End_Date__c == oldMap.oli.End_Date__c))
Where am I going wrong here and is there a simplier way to go about it?