+ Start a Discussion
mojavemojave 

Why would OpportunityLineItem.PricebookEntry.Product2Id = null?

Hi,

 

Does anyone know why I would get a null Product2Id out of the following debug statement?

 

for (OpportunityLineItem oli : Trigger.new){
  system.debug ('product is: ' + oli.PricebookEntry.Product2Id);

  }

 

I hope to one day to go so far as to access the name of the product, not just the ID.

 

I had some advice on how to do that, but my syntax must not be right.

 

Thanks for any tips,

Kevin

 

 

Best Answer chosen by Admin (Salesforce Developers) 
werewolfwerewolf
Trigger.new only feeds you information about the object you're currently working on.  Therefore you can know oli.PricebookEntryId from Trigger.new, but it won't tell you oli.PricebookEntryId.Product2Id from that -- you'll have to query it, like [select Product2Id from PricebookEntry where Id=:oli.PricebookEntryId].  I leave it to you to bulkify that query properly... :)

All Answers

werewolfwerewolf
Trigger.new only feeds you information about the object you're currently working on.  Therefore you can know oli.PricebookEntryId from Trigger.new, but it won't tell you oli.PricebookEntryId.Product2Id from that -- you'll have to query it, like [select Product2Id from PricebookEntry where Id=:oli.PricebookEntryId].  I leave it to you to bulkify that query properly... :)
This was selected as the best answer
mojavemojave

Ah, thanks.  As usual, I was missing something that should have been obvious: the fact that there is no child relationship here to call on directly!

 

This will indeed be fun to bulkify, when I make it that far :smileyhappy:

ShikibuShikibu
See the Bulk Trigger Idioms page -- it's not that tough, and a great skill to have in your toolkit.
mojavemojave

Thanks for the tip and link.  It helps, as I've definitely not mastered sets and maps yet and am still in the dark somewhat when working with multiple object types simultaneously.

 

For the bulkification, I ended up coding in tiny baby steps, perhaps redundantly, but at least I ended up with something that seems to work.

 

for (OpportunityLineItem oli : Trigger.new){ pbeIds.add(oli.PriceBookEntryId);}

prodNames = [Select Product2Id from PricebookEntry Where Id in:pbeIds];

for(Integer i= 0 ; i < prodNames.size(); i++){ prodIds.add(prodNames[i].Product2Id);}

prods = [select ID, Product_Line__c from Product2 where id in:prodIds];

 

Message Edited by mojave on 04-28-2009 05:47 PM
ShikibuShikibu

Looks like you're on the right track now.

 

Next time you post code, can you use the "Code" icon in the formatting bar? It looks like a clipboard with a "C" on it. It's hard to read your code because, unless you use that, the forum breaks indents and turns symbols into smiley faces. But you can go back, even later, and edit your post to fix that.

 

Couple things I might suggest for your code:

 

1. Consider names that more closely reflect the types. For instance, pbeList = [select ... from PricebookEntry]. Your select returns a list of PriceBookEntry SObjects, not a list of ProdNames.

 

2. It's cleaner (less error-prone; easier to read later) to use the colon notation than a counter in a for loop over a collection. Like this:

 

 

for (PriceBookEntry pbe : pbeList) prodIds.add( pbe.Product2Id );

 

 

 

 

 

 

mojavemojave

I like that code-formatting (for this board) feature.  That fixed the happy-face problem I had here...  I was wondering how those happy faces ended up in the code I posted here!