function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
priyankasingla3101.3916265522429534E12priyankasingla3101.3916265522429534E12 

Unable to create a PricebookEntry using apex code

Hi,
I am trying to create a PricebookEntry in my apex code, but it is giving me the following error:

Error: Compile Error: Invalid constructor syntax, name=value pairs can only be used for SObjects

Here is my code:
 

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

 2. Product2 p1 = new Product2(Name='Prod 1', Family='Container', Description='Prod 1 Description');
 3. insert p1;
            
 // Create a pricebook entry
 4. PricebookEntry pbe = new PricebookEntry (Pricebook2Id=pb.id, Product2Id=p1.id, IsActive=true, UnitPrice=100.0);
 5. insert pbe;
I am getting this error at Line 4. PricebookEntry is a standard object in Salesforce, but I am not able to understand why I am getting this error.

Any help will be appreciated.


 

justin_sfdcjustin_sfdc
Hi Priyanka,

I copied the same code that you provided and it allowed me to save it.
Could you put all the code that you currently have?

Thanks!
Himanshoo SethHimanshoo Seth
Dont see any error in your code. Looks to be some other issue. Do you have an Apex class in your org with the name PricebookEntry?
priyankasingla3101.3916265522429534E12priyankasingla3101.3916265522429534E12
Thanks for the replies. Got the issue!!!
My class name was PriceBookEntry itself, thats why it was not considering it as an sObject. :) Now I changed my class name to PriceBookEntryTest, and it is working fine. Thanks anyways.. :)

Suraj Tripathi 47Suraj Tripathi 47
Hi Priyankasingla,

"For creating PriceBookEntry you can use this code."
public class PriceBooks {
    public static void insertPriceBook()
    {
        try
        {
        PriceBook2 pricebookInstance=new PriceBook2();
            pricebookInstance.Name='Algo Price Book';
            pricebookInstance.Description='This is price book';
            pricebookInstance.IsActive=true;
            insert pricebookInstance;
            
         List<Product2> proList=new List<Product2>();
            for(Integer i=1;i<11;i++)
            {
                Product2 pro=new Product2();
                pro.Name='Mobile'+i;
                pro.IsActive=true;
                proList.add(pro);
            }
            if(!proList.isEmpty())
                insert proList;
            
            PriceBook2 standardPriceBook=[SELECT id FROM PriceBook2 where isStandard=true Limit 1];
            
            List<PriceBookEntry> pbEntry1=new List<PriceBookEntry>();
            Integer j=1;
            for(Product2 pb1:proList)
            {
                PriceBookEntry priceBook1=new PriceBookEntry();
                priceBook1.Product2Id=pb1.Id;
                priceBook1.PriceBook2Id=standardPriceBook.Id;
                priceBook1.UnitPrice=100+j;
                priceBook1.UseStandardPrice=false;
                priceBook1.IsActive=true;  
                pbEntry1.add(priceBook1);
                j++;
            }
           if(!pbEntry1.isEmpty())
            insert pbEntry1;
            
            List<PriceBookEntry> pbEntry2=new List<PriceBookEntry>();
            Integer i=1;
            for(Product2 pb2:proList)
            {
                PriceBookEntry priceBook2=new priceBookEntry();
                priceBook2.Product2Id=pb2.Id;
                priceBook2.Pricebook2Id=pricebookInstance.Id;
                priceBook2.UnitPrice=100+i;
                priceBook2.UseStandardPrice=false;
                priceBook2.IsActive=true;
                pbEntry2.add(priceBook2);
                i++;
            
            }
            if(!pbEntry2.isEmpty())
                insert pbEntry2;
       
        }catch(Exception e)
        {
            System.debug('Exception due to ----> '+e.getLineNumber());
        }
            
    }

}


If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi