You need to sign in to do that
Don't have an account?
mojave
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
All Answers
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:
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];
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 );
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!