0

In my viualforce page I am adding products - I don't want to hardcode the references to the products, I want the button to pull the proper product per Opportunity. Why does my line not work to find the proper pricebook entry? The method:

public PageReference AddJuiceProduct (){

       Opportunity TheOpp = (Opportunity)sc.GetRecord();
       NewLineItem.OpportunityId=TheOpp.id;
       NewLineItem.PricebookEntryId=[select id from PricebookEntry where name =: 'Juice' and Pricebook2id=:op.Pricebook2Id].id;
        NewLineItem.Quantity=1;
       ETC code...
       insert NewLineItem;

If I hardcode the reference to the product the insert works. Example:

NewLineItem.PricebookEntryId='123456798798794654';

Why doesn't this query work? thanks!

    NewLineItem.PricebookEntryId=[select id from PricebookEntry 
                                 where name =: 'Juice' 
                                 and Pricebook2id=:op.Pricebook2Id].id;
4
  • What is it you do that works? It's hard to tell exactly where you're stuck.
    – Adrian Larson
    Jun 22 '17 at 19:16
  • updated to clarify
    – jaw999
    Jun 22 '17 at 19:19
  • Are you sure there is actually an entry whose Name is 'Juice'?
    – Adrian Larson
    Jun 22 '17 at 19:19
  • I can find the pricebook entry if I do the below in the query editor: select id, name from PricebookEntry where name = '(juice or real product name)' and pricebook2id = '(copied id from a Opportunity)'
    – jaw999
    Jun 22 '17 at 19:22
0

I had two errors in this: First, I declared the Opportunity variable TheOpp

 Opportunity TheOpp = (Opportunity)sc.GetRecord();

but then in my query used op.pricebook2id - this compiled b/c OP was declared as a variable elsewhere.

Next, I needed to add the fields to the controller and in the GetRecord:

Controller.addFields(new List<String>{'Name', 'PriceBook2Id'});

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.