function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
travis.truetttravis.truett 

Access a Product's Family in OpportunityLineItem Trigger

I have an OpportunityLineItem trigger, and I am trying to write an IF statement that basicaly says "if this OpportunityLineItem's associated product belongs to the family 'Services', do X.

Right now, my IF statement looks like this:

 if(oli.Product2.Family == 'Services')

When I debug, this IF statement is never being entered, so I know my syntax is wrong somehow, I'm just not sure how to express what I want to in the code.

Thanks in advance for the help 
Best Answer chosen by travis.truett
Vivek DeshmaneVivek Deshmane
Hi Travis,
I got it, you have  to query once again  on Opportunity Line item record  in your trigger outside for loop .looks like below and then iterate result and check codition
Set<Id> setOpptyOLI = new set<Id>()
setOpptyOLI .add(oli.d)
List<OpportunityLineItem> OptyOlilst=[Select Product2.Family FROM OpportunityLineItem WHERE Id IN:setOpptyOLI ];
for(OpportunityLineItem record : OptyOlilst){
if(record .Product2.Family=='Services')
{
 System.debug('=======>Test');
}
}
Mark the answer if it works for you
Best Regards,
-Vivek

All Answers

Vivek DeshmaneVivek Deshmane
Hi Travis,

Please try below code and let me know if it works.
if(oli.PricebookEntry.Product2.Family== 'Services')
{
}
Best Regards,
-Vivek
travis.truetttravis.truett
That did not work. It's still not entering the if statement, and I'm not sure why. If you want to see my code, I can send it, and explain where I'm having the problem.
Terence_ChiuTerence_Chiu
Travis, if you are accessing the field value from the trigger by iterating through the trigger.new list it will not have the Product Family value. You may have to re-query the the opportunitylineitem records first. For example:

List<OpportunityLineItem> olis = [SELECT Id, Name, PricebookEntry.Product2.Family FROM OpportunityLineItem WHERE Id IN trigger.new];

for(OpportunityLineItem oli : olis){
    if(oli.PricebookEntry.Product2.Family== 'Services')
   {
      //do something
   }
}

​Or you can create a formula field of type text on Opportunity Product object that will pull the product family for you. For example you can create an Opportunity Product custom formula field called Product_Family__c with the following formula:

TEXT(PricebookEntry.Product2.Family)

You can then access the custom field which will have the product family value.

if(oli.Product_Family__c == 'Services')
   {

   }
 
Vivek DeshmaneVivek Deshmane
Hi Travis,
I got it, you have  to query once again  on Opportunity Line item record  in your trigger outside for loop .looks like below and then iterate result and check codition
Set<Id> setOpptyOLI = new set<Id>()
setOpptyOLI .add(oli.d)
List<OpportunityLineItem> OptyOlilst=[Select Product2.Family FROM OpportunityLineItem WHERE Id IN:setOpptyOLI ];
for(OpportunityLineItem record : OptyOlilst){
if(record .Product2.Family=='Services')
{
 System.debug('=======>Test');
}
}
Mark the answer if it works for you
Best Regards,
-Vivek
This was selected as the best answer
travis.truetttravis.truett
It was tricky to figure out where to add all of that code in trigger, but I finally figured it out, and it worked perfectly. Thanks so much!