1

I have been looking all over for a solution. What i have seen in other similar issues online is the error was related to code errors/user errors. I have looked over my code several times and cant see any errors. Im thinking its a bug with SF but not sure, it could just be something im not seeing. Any insight here would be helpful.

So heres the issue; when i run this on a UnitTest (with SeeAllData=true):

[
Select 
    Id, 
    PriceBookEntryId,
    PriceBookEntry.Id, 
    PriceBookEntry.Product2Id, 
    PriceBookEntry.Product2.Id
FROM OpportunityLineItem
LIMIT 2
]

None of the relationship data is brought back. I only get:

(
  OpportunityLineItem:{
    Id=00km0000005CukEAAS, 
    PricebookEntryId=01u60000008g7u9AAA
  }, 
  OpportunityLineItem:{
    Id=00km0000005GuebAAC, 
    PricebookEntryId=01u3200000DKMvZAAX
  }
)

For the life of me, i can't figure out why the relationship data is not being returned. I have also tried to create my own pricebook, products, pricebook entires and opportunity-line-items and i still get the same results.

The closet i can think of is the issue is related to (SeeAllData=true), but unfortunately we are using a managed packaged that effects our opportunities and the package requires (SeeAllData=true) for any UnitTest we create. If we have SeeAllData off and create an opportunity on the UnitTest, the manage package throws errors.

Any thoughts on why im not getting the related data returned when im in the scope of a UnitTest?

P.S. This code works as expected when im not in the scope of a UnitTest.

7
  • 1
    does the test's running user have CRUD access to Product2?
    – cropredy
    Apr 23 '16 at 5:39
  • Yes, i also test it by run the UnitTest as a user with System Admin privileges, same issue.
    – Mike
    Apr 23 '16 at 15:28
  • why a managed package should force seealldata=true in unit test methods seems bizarre. You sure you can't mock data that MP needs?
    – cropredy
    Apr 23 '16 at 15:56
  • I have tried, but because i would need to create an Opp to create line items the third party package is trigger (i think its an actual trigger causing the error from it). My guess is the tool tries to load data from an object and since (seealldata=false) the third party tigger gets no results and then freaks out and gives errors causing the UnitTest to stop. If i know what their system was needing, i could also re-create the records its needing and then i could have (seealldata=false), but that could take a bit of time to work out.
    – Mike
    Apr 23 '16 at 22:18
  • @Mike - How are you viewing the data? If you are debugging the data it will not show the child values, only the ID of the child record. If you specifically debug PriceBookEntry.Product2Id you will get the value as expected. Otherwise you will have to use getsObject method to get the child records and fields when not specifically referencing them. - As for the managed package trigger, reach out to the vendor as you probably need to created some setup item. Although they should be handling that and the fact they are not is a sign of poorly written code on their part
    – Eric
    Apr 24 '16 at 4:12
0

How are you viewing the data?

If you are debugging the data it will not show the child values, only the ID of the child record.

If you specifically debug the field PriceBookEntry.Product2Id you will get the value as expected.

Otherwise you will have to use getsObject() method to get the child records and fields when not specifically referencing them.

0

[ Select Id, PriceBookEntryId, (select PriceBookEntry.Id, PriceBookEntry.Product2Id, PriceBookEntry.Product2.Id from PriceBookEntry) FROM OpportunityLineItem ]

this is the correct format to write relation based queries. refer this link.

3
  • Thanks for the response, not sure why i would need to preform the relationship select like a 'parent-to-child' relationship. The relationship is a 'child-to-parent'. PriceBookEntry is a child of Product2 and OpportunityLineItem is a child of PriceBookEntry. Am i understanding that relationship wrong? Also, the code works as expected, in the 'child-to-parent' format when im not in the scope of a UnitTest.
    – Mike
    Apr 23 '16 at 15:33
  • Also, in the link you posted, there is an example of exactly what im trying to do. Its the second from the bottom, they just have it as a subselect to opportunities.
    – Mike
    Apr 23 '16 at 15:39
  • patelkiran3090 -- There is nothing wrong with the query as @Mike defined it. PricebookEntry is not a child of OpportunityLineItem, it is a lookup object. Your example tries to do a relationship query on PriceBookEntry but that is not a child relationship of OLI
    – cropredy
    Apr 23 '16 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.