0

I have an sObject called Edition, it has the related PriceBook id, how to get the items of the PriceBook when querying the Edition details:

What I tried:

  query = f"""
           SELECT
                Id,
                Name,
                Edition_Name__c,
                End_Date__c,
                Start_Date__c,
                Legal_Entity__c,
                CurrencyIsoCode,
                Status__c,
                Price_Book__c,
                (SELECT Id, Name FROM PricebookEntry WHERE Pricebook2Id = Price_Book__c)
            FROM Edition__c
            WHERE Id = '{edition_id}'
        # """

2 Answers 2

1

Assuming your schema is

       Pricebook2
   ------------------
   |                | 
   v                v
  Edition__c       PriceBookEntry

then ... you can't query on Edition to get the PricebookEntries because that requires an up-then-down traversal.

You need two queries

Edition__c edition = [SELECT Id, Price_Book__c, ...
                       FROM Edition__c
                       WHERE Id = :edition_id][0];
PriceBook2 editionPriceBookWithEntries =
   [SELECT Id, (SELECT Id, Name FROM PriceBookEntries)
     FROM PriceBook2
     WHERE Id = :edition.Price_Book__c];
2
  • Thank you, this is what I ended doing, there another requirement, and Edition__c can have multiple Pricebook2, so I need to loop in the pricebook ids to get the items of each? Commented Mar 15 at 8:46
  • How can an Edition have multiple pricebooks? the assumed schema has a lookup from Edition to Pricebook2. Do you have multiple lookup fields to Pricebook2 on Edition?
    – cropredy
    Commented Mar 15 at 16:55
0

I ended up getting the wanted result with one query:

query = f"""
                SELECT Id, 
                Name, 
                Edition_Name__c, 
                End_Date__c, 
                Start_Date__c, 
                Legal_Entity__c, 
                CurrencyIsoCode, 
                Status__c,
                (SELECT Id, Name,
                        ( SELECT Id, Name,CurrencyIsoCode,UnitPrice,IsActive FROM PricebookEntries) 
                        FROM Price_Books__r) 
                    FROM Edition__c WHERE Id ='{edition_id}'"""

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .