You need to sign in to do that
Don't have an account?
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
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
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
Please try below code and let me know if it works.
if(oli.PricebookEntry.Product2.Family== 'Services')
{
}
Best Regards,
-Vivek
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')
{
}
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