You need to sign in to do that
Don't have an account?
Update PriceBook Entry when Product is updated
Hi all,
I am trying to create a trigger that will insert and/or update a PricebookEntry when the Product is inserted or updated. I have limited Apex knowledge and have put together the part that inserts the Pricebook Entry on Insert of the Product and that seems to be working correctly, however I cannot seem to get the update side of things working.
Here is my code so far:
trigger UpsertPBE on Product2 (after insert, after update) {
sObject s = [select ID from Pricebook2 where IsStandard = TRUE];
list<PricebookEntry> entries=new list<PricebookEntry>();
if(trigger.isinsert){
for (Product2 p : Trigger.new) {
entries.add(
new PricebookEntry(
Pricebook2Id=s.ID,
Product2Id=p.ID,
UnitPrice=p.Item_Cost__c,
IsActive=p.IsActive,
UseStandardPrice=FALSE)
);
}
insert entries;
}
list<PricebookEntry> pbe = [SELECT Id FROM PricebookEntry WHERE Product2ID =:trigger.new[0].id];
if(trigger.isupdate){
for (Product2 p : Trigger.new) {
if (pbe != null) {
PricebookEntry.UnitPrice=p.Item_Cost__c;
}
}
}
}
The error message I am getting is :
Error: Compile Error: Expression cannot be assigned at line -1 column -1
I'm hoping someone has come accross this before and has some idea on how I can make it work.
Any advice would be greatly appreciated!
Thank you!!
Hi, Try this
trigger UpsertPBE on Product2 (after insert, after update) {
Set<ID> ids = Trigger.new.keySet();
sObject s = [select ID from Pricebook2 where IsStandard = TRUE];
list<PricebookEntry> entries=new list<PricebookEntry>();
if(trigger.isinsert){ for (Product2 p : Trigger.new) { entries.add( new PricebookEntry( Pricebook2Id=s.ID, Product2Id=p.ID, UnitPrice=p.Item_Cost__c, IsActive=p.IsActive, UseStandardPrice=FALSE) ); } insert entries; }
list<PricebookEntry> pbe = [SELECT Id FROM PricebookEntry WHERE Product2ID in =ids];
if(trigger.isupdate){ for (Product2 p : Trigger.new) { if (pbe != null) { PricebookEntry.UnitPrice=p.Item_Cost__c; } } } }
Hi Wik,
Thanks so much for your reply!
I tried your suggestion but I am now getting this error:
Error: Compile Error: Method does not exist or incorrect signature: [LIST<Product2>].keySet() at line 3 column 15