You need to sign in to do that
Don't have an account?
magandrez
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
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:
All Answers
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?
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.
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
Any ideas of what's going on?
Thank you,
MGA.
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:
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:
Tack! ;)
MGA.
Looks good. Just as a note, I would do the following instead:
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.
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.