You need to sign in to do that
Don't have an account?

Test class for OpportunityProductLineItem Trigger
Could someone point me to or write an example of a test class for a OpportunityProductLineItem? If this was a trigger on an object like Account I would just create Account a = new account and insert a but a OpportunityProductLineItem is dependent on the Opportunity ,Pricebookentry, pricebook2, product2 and more... I tried to create the test class by instansiating all of those objects but I was still unable to find/set every required field.
Trigger class
trigger OpportunityModelNameTrigger on OpportunityLineItem (after insert, after update, after delete) {
OpportunityModelNameTriggerHandler handler = new OpportunityModelNameTriggerHandler();
if(Trigger.isInsert && Trigger.isAfter) {
handler.OnAfterInsert(Trigger.new);
} else if(Trigger.isUpdate && Trigger.isAfter) {
handler.OnAfterUpdate(Trigger.old, Trigger.new, Trigger.oldMap, Trigger.newMap);
}
else if (Trigger.isDelete)
{
handler.OnAfterDelete(Trigger.old,Trigger.oldMap);
}
}
Handler Class :
public with sharing class OpportunityModelNameTriggerHandler {
// update the Opportunity Model field when new records are inserted from trigger
public void OnAfterInsert(List<OpportunityLineItem> newRecords){
updateOpportunity(newRecords);
}
// update the Opportunity Model when records are updated from trigger
public void OnAfterUpdate(List<OpportunityLineItem> oldRecords,
List<OpportunityLineItem> updatedRecords, Map<ID, OpportunityLineItem> oldMap,
Map<ID, OpportunityLineItem> newMap){
updateOpportunity(updatedRecords);
}
public void OnAfterDelete(List<OpportunityLineItem> oldRecords,Map<ID,OpportunityLineItem> oldMap)
{
updateOpportunityAfterDelete(oldRecords);
}
private void updateOpportunityAfterDelete(List<OpportunityLineItem> oldRecords)
{
Set<Id> CurrentOppId = new Set<Id>();
for (OpportunityLineItem OppLnItem : oldRecords){
CurrentOppId.add(OppLnItem.OpportunityId);
}
List<Opportunity> OppId =
[Select o.Id from Opportunity o
where o.Id in: CurrentOppId];
List<OpportunityLineItem> relatedOLIs =
[Select oli.Id, PricebookEntry.Product2.name, PricebookEntry.Product2.Option_Category__c from OpportunityLineItem oli
where oli.OpportunityId in: CurrentOppId];
if(relatedOLIs.size() != 0)
{
for (Opportunity opp2 : OppId){
for (OpportunityLineItem oli : relatedOLIs){
if(oli.PricebookEntry.Product2.Option_Category__c == 'Model')
{
opp2.model__c = oli.PricebookEntry.Product2.name;
}
}
}
}
else
{
for (Opportunity opp2 : OppId)
{
opp2.model__c = '';
}
}
update OppID;
}
private void updateOpportunity(List<OpportunityLineItem> newRecords)
{
Set<Id> CurrentOppId = new Set<Id>();
for (OpportunityLineItem OppLnItem : newRecords){
CurrentOppId.add(OppLnItem.OpportunityId);
}
// Create List of One Opportunity Id to Update
List<Opportunity> OppId =
[Select o.Id from Opportunity o
where o.Id in: CurrentOppId];
List<OpportunityLineItem> relatedOLIs =
[Select oli.Id, PricebookEntry.Product2.name, PricebookEntry.Product2.Option_Category__c from OpportunityLineItem oli
where oli.OpportunityId in: CurrentOppId];
for (Opportunity opp2 : OppId){
for (OpportunityLineItem oli : relatedOLIs){
if(oli.PricebookEntry.Product2.Option_Category__c == 'Model')
{
opp2.model__c = oli.PricebookEntry.Product2.name;
}
}
}
update OppID;
}
}
The test class needs to do the following:
1. Create a pricebook2
2. Create a product2
3. Create a pricebookentry and add it to the standard pricebook (using product2.id from step 2)
4. Create a pricebookentry and add it to the pricebook2 created in step 1 (also using product2.id from step 2)
5. Create an Account
6. Create an Opportunity, accountId = Account.id from step 5
Then, when you create the test OpportunityLineItem, you refer to the pricebookentry.id from step 4, and set opportunityId to Opportunity.id from step 6
All Answers
The test class needs to do the following:
1. Create a pricebook2
2. Create a product2
3. Create a pricebookentry and add it to the standard pricebook (using product2.id from step 2)
4. Create a pricebookentry and add it to the pricebook2 created in step 1 (also using product2.id from step 2)
5. Create an Account
6. Create an Opportunity, accountId = Account.id from step 5
Then, when you create the test OpportunityLineItem, you refer to the pricebookentry.id from step 4, and set opportunityId to Opportunity.id from step 6
Your message did end up leading to a solution for me but could you elaborate on step 3? I ended up writing Pricebookentry pbe = [select name from Pricebook2 where isStandard = true]; which works but I am concerned that that query could possible return null. Is using this query proper or can I reference the Standard price book without querying for it?
BerettaJon:
There should always be exactly one Standard price book. See Web Services API Guide Reference | Standard Objects | Pricebook2 | IsStandard
eric