0

bundlewithProduct = [SELECT Id, (select Product__c from Product_Quantities__r) FROM Bundle__c WHERE Id =: QBP.Bundle__c];

from the above mentioned query i am getting the list of bundle with it's Product Quantity

Now, i have one string ListPrice and fill this string with ListPrice of selected Product from bundlewithProduct

i am bit confuse about how to put [select Product__c from Product_Quantities__r] in where clause of the following query

this.ListPrice = string.ValueOf([SELECT Id, Product2Id, UnitPrice FROM PriceBookEntry where Product2Id =: ______________________);

can anyone please tell me how to achieve this ?

1
  • How Product_Quantities__c and PriceBookEntry are related?
    – Reshma
    Feb 6, 2017 at 11:29

1 Answer 1

2

If you are trying to get PricebookEntry records with the sme Product2Id, note that you can have multiple matches since it is a many-to-one relationship (PricebookEntry to Product2). If you wanted to get the most recently created PricebookEntry, for example, you can use the below code. There are a few steps you can take to make it more efficient, but this is the spelled out version.

bundlewithProduct = [
    SELECT Id, (
        SELECT Product__c FROM Product_Quantities__r
        ORDER BY CreatedDate DESC LIMIT 1
    )
    FROM Bundle__c WHERE Id =: QBP.Bundle__c
];

Set<Id> productIds = new Set<Id>();
for (Product_Quantity__c child : bundleWithProduct.Product_Quantities__r)
{
    productIds.add(child.Product__c);
}

List<PricebookEntry> mostRecentEntry = [
    SELECT UnitPrice FROM PricebookEntry
    WHERE Product2Id IN :productIds
];

Decimal mostRecentPrice;
if (!mostRecentEntry.isEmpty)
{
    mostRecentPrice = mostRecentEntry.UnitPrice;
}

You must log in to answer this question.

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