1

Not sure what i need to modify in this code to get rid from this error

public void retrieveBundleInfo() {
    bundlewithProduct = [SELECT Id, (select Id, Name, Product__c, Product__r.ProductCode, Product__r.Description, Quantity__c from Product_Quantities__r) FROM Bundle__c WHERE Id =: QBP.Bundle__c];
    try {
        this.ListPrice = string.ValueOf([SELECT Id, Product2Id, UnitPrice FROM PriceBookEntry where Product2Id =: bundlewithProduct.Product_Quantities__r.get(0).Product__c]);
    }
    catch(Exception e) {
        this.ListPrice = '';
    }        
}
8
  • What is bundlewithProduct type is it a List<Bundle__c>?
    – RCS
    Feb 6, 2017 at 11:06
  • @RCS Yes, it is List<Bundle__c> Feb 6, 2017 at 11:08
  • Is the error on this line this.ListPrice = string.ValueOf([SELECT Id, Product2Id, UnitPrice FROM PriceBookEntry where Product2Id =: bundlewithProduct.Product_Quantities__r.get(0).Product__c]);
    – RCS
    Feb 6, 2017 at 11:10
  • Yes. is where clause correct ? Feb 6, 2017 at 11:11
  • Where clause is not correct you need to change the where clause
    – RCS
    Feb 6, 2017 at 11:17

2 Answers 2

2

Try to use below code

Where bundlewithProduct list zero index value is accessed.

public void retrieveBundleInfo() {
    bundlewithProduct = [SELECT Id, (select Id, Name, Product__c, Product__r.ProductCode, Product__r.Description, Quantity__c from Product_Quantities__r) FROM Bundle__c WHERE Id =: QBP.Bundle__c];
    try {
        this.ListPrice = string.ValueOf([SELECT Id, Product2Id, UnitPrice FROM PriceBookEntry where Product2Id =: bundlewithProduct[0].Product_Quantities__r.get(0).Product__c]);
    }
    catch(Exception e) {
        this.ListPrice = '';
    }        
}
3
  • If i want to multiple ListPrice for each Product then do i need for loop ? Feb 6, 2017 at 11:28
  • Yes for sure you'll need a for loop to iterate and store the ListPrice in collection. Create a Map<Prodcut, ListPrice> that stores the Product wise ListPrice.
    – RCS
    Feb 6, 2017 at 11:30
  • Ok. Let me do it that way and let you know :) thanks Feb 6, 2017 at 11:34
0

Assuming there is only one PriceBookEntry for the given product, and you are trying to get the UnitPrice to assign to ListPrice, then you could do this:

PriceBookEntry pbe = [SELECT Id, Product2Id, UnitPrice FROM PriceBookEntry where Product2Id =: bundlewithProduct[0].Product_Quantities__r.get(0).Product__c LIMIT 1];
this.ListPrice = string.ValueOf(pbe.UnitPrice);

or

this.ListPrice = string.ValueOf([SELECT Id, Product2Id, UnitPrice FROM PriceBookEntry where Product2Id =: bundlewithProduct[0].Product_Quantities__r.get(0).Product__c LIMIT 1].UnitPrice);

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.