2

I am trying to insert products to price book entries and getting the following error

"Variable does not exist: Pricebook2Id"

Public class pricebookadd{

    public void pricebookentry(){

    Pricebook2 pb = [select Id, IsActive from PriceBook2 where IsStandard=True];

     Product2 p1 = new Product2(Name='Prod 1', Family='Container', Description='Prod 1 Description');
     insert p1;

     // Create a pricebook entry
     PricebookEntry pbe = new PricebookEntry();

     pbe.Pricebook2Id=pb.id; 
     pbe.Product2Id=p1.id; 
     pbe.IsActive=true; 
     pbe.UnitPrice=100.0;
     insert pbe;
     }
     }

I know this question seems to be silly.But I couldn't figure it out. Thanks in advance,

1
  • are you sure you are getting exception in same line? Jul 6, 2016 at 9:12

2 Answers 2

2

I tried the code in Developer console and its working fine. The error might be because of below reason

  • If there is any apex class with the same name of the standard or global sObjects while creating List, map,set the above error will reflect.

We can solve this problem in two ways:

  1. We have to rename our Apex Class to some other name.

  2. Use 'Schema' before sObject while creating List, Set, Map (Don't use 'schema' before sObject in SOQL Query)

1

You might be having a apex class in your org named as "PricebookEntry". Therefore, you might be getting this issue. Check with all apex classes in your org and try to rename/remove that class.

1
  • Yeah right! thanks @Devendra.But what is the reason behind that? Jul 6, 2016 at 9:48

You must log in to answer this question.

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