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
magandrezmagandrez 

PricebookEntry.Product2Id field is not writeable

Hi all!,

 

I'm trying to add a pricebook entry for each of the products I'm setting as active, but at the time of executing the scheduled code, the System Log gives me the error "PricebookEntry.Product2Id field is not writeable". How can I insert a pricebook entry then? I supose It is possible using Apex...

 

Here is my code:

 

 	List<Product2> prods=Database.query(queryStr1);
         
        for(Product2 p:prods){
            
            p.isActive=TRUE;

            PricebookEntry pricebookEntryItem = new PricebookEntry(Pricebook2Id='01s20000000Hfx5AAC');
            pricebookEntryItem.Product2Id=p.Id;
            pricebookEntryItem.UnitPrice=0;
            pricebookEntryItem.UseStandardPrice=TRUE;
            insert pricebookEntryItem;
            
        } update prods;

 

Any ideas/work arounds are more than welcome.

 

Thank you,

 

MGA

Best Answer chosen by Admin (Salesforce Developers) 
MartinHaagenMartinHaagen

Hi,

 

it's good that you are working to get the code to be batchifyed. In this case, you should remove the inner for-loop. The pbEntryList will be always be empty. You also need to add the created PriceBookEntry to the list when you've create it. Something like this:

 

List<Product2> prods=Database.query(queryStr1);

List<PricebookEntry> pbEntryList = new List<PricebookEntry>();
    
        for(Product2 p:prods){
            p.isActive=TRUE;

	    PricebookEntry pb = new PricebookEntry()
	    pb.Pricebook2Id='01s20000000Hfx5AAC';
	    System.debug(pb.Product2Id=p.Id);
	    pb.Product2Id=p.Id;
	    pb.IsActive=true;
	    pb.UnitPrice=0;
	    pb.UseStandardPrice=false;
	    pbEntryList.add(pb);
        }

system.debug(pbEntryList);
insert pbEntryList;
update prods;

 

All Answers

Nilesh ManeNilesh Mane

I have faced same problem earlier.... but didn't get any solution.. Its not possible to insert PricebookEntry record i guess.

 

Happy Coding :)

MartinHaagenMartinHaagen

Hi,

 

it's a bit strange this. If you look at the Salesforce Schema in Eclipse you can see that the Access for the Product2Id field is "Createable" and "Filterable" (for my user). This implies that you can set the field. I have not created Pricebook entries with APEX but I have via the dataloader and it worked great. 

 

Are you sure the user you are doing this has the correct permissions setup?

magandrezmagandrez

Hi MartinHaagen

 

Yes, it is a bit strange. The user profile I'm using to create this PricebookEntries is the Sys Admin with all the rights, so I guess it shouldn't be a matter of access rights.

 

What do you think about the code? Is there something strange or wrong you can see on it?

 

Br,

 

MGA.

magandrezmagandrez

Hi!,

 

Seems I made some progress. No I don't get any errors on the system log, but the PricebookEntries are not in the system. Here is an update of the code I'm struggling with

 

List<Product2> prods=Database.query(queryStr1);

List<PricebookEntry> pbEntryList = new List<PricebookEntry>();
    
        for(Product2 p:prods){
            p.isActive=TRUE;

	            for(PricebookEntry pb:pbEntryList){
		            //pricebookEntryItem.Name='Standard Price Book';
		            pb.Pricebook2Id='01s20000000Hfx5AAC';
		            System.debug(pb.Product2Id=p.Id);
		            pb.Product2Id=p.Id;
		            pb.IsActive=true;
		            pb.UnitPrice=0;
		            pb.UseStandardPrice=false;
		            //insert pb;
	            }
        }

insert pbEntryList;
update prods;

 Any ideas of what's going on?

 

Thank you,

 

MGA.

MartinHaagenMartinHaagen

Hi,

 

it's good that you are working to get the code to be batchifyed. In this case, you should remove the inner for-loop. The pbEntryList will be always be empty. You also need to add the created PriceBookEntry to the list when you've create it. Something like this:

 

List<Product2> prods=Database.query(queryStr1);

List<PricebookEntry> pbEntryList = new List<PricebookEntry>();
    
        for(Product2 p:prods){
            p.isActive=TRUE;

	    PricebookEntry pb = new PricebookEntry()
	    pb.Pricebook2Id='01s20000000Hfx5AAC';
	    System.debug(pb.Product2Id=p.Id);
	    pb.Product2Id=p.Id;
	    pb.IsActive=true;
	    pb.UnitPrice=0;
	    pb.UseStandardPrice=false;
	    pbEntryList.add(pb);
        }

system.debug(pbEntryList);
insert pbEntryList;
update prods;

 

This was selected as the best answer
magandrezmagandrez

Hi MartinHaagen, 

 

What you wrote above is exactly right :) I modified a bit some things to not hardcode the Pricebook2Id, but you were right, I was trying to insert an empty list, that's why didn't get any error neither any good result. Here is the final code:

 

    List<PricebookEntry> pbEntryList = new List<PricebookEntry>();
    List<Pricebook2> priceBookId = [Select Id From Pricebook2 Where Name=:'Standard Price Book'];
    
        for(Product2 p:prods){
            p.isActive=true;
            
   			PricebookEntry pb = new PricebookEntry();
   			pb.Pricebook2Id = priceBookId[0].Id;
            pb.Product2Id=p.Id;
            pb.IsActive=true;
            pb.UnitPrice=1;
            pb.UseStandardPrice=false;
            pbEntryList.add(pb);

        }
   
    update prods;
    insert pbEntryList;

 

Tack! ;)

 

MGA.

pconpcon

Looks good.  Just as a note, I would do the following instead:

 

    List<PricebookEntry> pbEntryList = new List<PricebookEntry>();
    List<Pricebook2> priceBookId = [Select Id From Pricebook2 Where Name=:'Standard Price Book'];
    
    if (priceBookId.size() != 1) {
        //Use add error to fail out or throw an exception
    }

    for (Product2 p: prods) {
        p.isActive=true;
            
        PricebookEntry pb = new PricebookEntry();
        pb.Pricebook2Id = priceBookId[0].Id;
        pb.Product2Id=p.Id;
        pb.IsActive=true;
        pb.UnitPrice=1;
        pb.UseStandardPrice=false;
        
        pbEntryList.add(pb);
    }
   
    update prods;
    insert pbEntryList;

 

magandrezmagandrez

Hi pcon,

 

Good to see you over here :) I've noticed that once I posted the code I wrote. Of course that piece is subject of improvement and right after publishing I started to improve it adding try/catch and more checks.

 

Thanks for the tip and for your help over the IRC Channel :)

 

Greetings,

 

MGA.

priyankasingla3101.3916265522429534E12priyankasingla3101.3916265522429534E12
Hi,
Whenever I am trying to create a PriceBookEntry using apex code, it is giving me an error, Error: Compile Error: Variable does not exist: Pricebook2Id.
I am using the above written code, but I am not able to create a PricebookEntry. Please help me out.