I have an after insert method on OpportunityLineItem that I'm writing a test class for. A problem that I had is I couldn't get the test to assert on the list passed to it because a formula was null when I expected it to be populated.
I thought to myself if I just reproduce the formula into the trigger logic then I can get around the issue but in doing so I've stopped the logic from working entirely where it worked in the UI before.
Consider this example:
I have a formula field "Product Family" that has the following syntax:
Text(PricebookEntry.Product2.Family)
When I put it into this method it works when you create a new OpportunityLineItem record in the UI, the solution consultant field is populated:
public static void populateSolutionsConsultant(List<OpportunityLineItem> newList)
{
List<OpportunityTeamMember> teamMembers = new List<OpportunityTeamMember>();
Map<Id,List<OpportunityLineItem>> oppToLineItem = new Map<Id,List<OpportunityLineItem>>();
for(OpportunityLineItem oli:newList)
{
if(oli.Product_Family__c == 'Analytic Solutions')
{
if(oppToLineItem.get(oli.OpportunityId)==null)
{
List<OpportunityLineItem> oliLst = new List<OpportunityLineItem>();
oliLst.add(oli);
oppToLineItem.put(oli.OpportunityId,oliLst);
}
else
{
oppToLineItem.get(oli.OpportunityId).add(oli);
}
}
}
teamMembers = [SELECT UserID,OpportunityId FROM OpportunityTeamMember where OpportunityId in:oppToLineItem.keyset() AND TeamMemberRole='Solutions Consultant'];
for(OpportunityTeamMember oppTeamMemb:teamMembers)
{
List<OpportunityLineItem> olis = oppToLineItem.get(oppTeamMemb.OpportunityId);
for(OpportunityLineItem line:olis)
{
line.Solution_Consultant__c = oppTeamMemb.UserID;
}
}
}
However, because I can't run my test on the formula I edited line looking for Analytic Solutions to be what the formula is, cutting the formula field out of the logic. Like this the solution consultant is not populated.:
public static void populateSolutionsConsultant(List<OpportunityLineItem> newList)
{
List<OpportunityTeamMember> teamMembers = new List<OpportunityTeamMember>();
Map<Id,List<OpportunityLineItem>> oppToLineItem = new Map<Id,List<OpportunityLineItem>>();
for(OpportunityLineItem oli:newList)
{
if(oli.PricebookEntry.Product2.Family == 'Analytic Solutions')
{
if(oppToLineItem.get(oli.OpportunityId)==null)
{
List<OpportunityLineItem> oliLst = new List<OpportunityLineItem>();
oliLst.add(oli);
oppToLineItem.put(oli.OpportunityId,oliLst);
}
else
{
oppToLineItem.get(oli.OpportunityId).add(oli);
}
}
}
teamMembers = [SELECT UserID,OpportunityId FROM OpportunityTeamMember where OpportunityId in:oppToLineItem.keyset() AND TeamMemberRole='Solutions Consultant'];
for(OpportunityTeamMember oppTeamMemb:teamMembers)
{
List<OpportunityLineItem> olis = oppToLineItem.get(oppTeamMemb.OpportunityId);
for(OpportunityLineItem line:olis)
{
line.Solution_Consultant__c = oppTeamMemb.UserID;
}
}
}
Why can't I use "PricebookEntry.Product2.Family" in the if statement?