You need to sign in to do that
Don't have an account?

Need help to write a trigger
When a Product is created or edited, create a Price Book Entry in the Standard Price Book with the Advertised Price from the input record.
How can i write a trigger for the above mentioned one.Any suggestions are always welcomed.....
Regards,
Subha
Hi ,
Write trigger like as:
trigger OnProductPriceBookEntry on Product2 (after insert) {
List<PricebookEntry> PBList=new List<PricebookEntry>();
PricebookEntry PB;
for(Product2 Prod:trigger.new){
PB=new PricebookEntry();
PB.UnitPrice=Prod.UnitPrice;
PB.UseStandardPrice=true;
PB.Pricebook2Id=Prod;
PBList.Add(PB);
}
insert PBLIst;
}
I think above sample code will help you.
Thanks for the suggestion Kritin.
but its nt working....
we need it for before insert and before update i think...
any more suggestions are always welcomed
Regards,
Subha
Is there any error are you getting?
It must be after insert because of Once the Product gets added then only we can define PriceBook entry for this.
Please elaborate the errro if you are finding something?
Illegal assignment from SOBJECT:Product2 to Id
This is the error i am getting....
Regards,
Subha
Apex trigger PricebookEntry caused an unexpected exception, contact your administrator: PricebookEntry: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Price Book ID: id value of incorrect type: 01tA0000002PFvZIAW: [Pricebook2Id]: Trigger.PricebookEntry: line 15, column 5
This is the error i am getting when i am trying to save a newly created product......
Regards,
Subha
Below code is just only sample doing of work try this:
trigger OnProductPriceBookEntry on Product2 (before insert, after insert) {
List<PricebookEntry> PBList=new List<PricebookEntry>();
Pricebook2[] pricebook=[Select Id from Pricebook2];
PricebookEntry PB;
if(Trigger.isBefore){
for(Product2 Prod:trigger.new){
}
}
if(Trigger.isAfter){
for(Product2 Prod:trigger.new){
PB=new PricebookEntry();
PB.UnitPrice=100000;
PB.Isactive=true;
//PB.UseStandardPrice=true;
PB.Product2Id=Prod.Id;
PB.Pricebook2Id=pricebook[1].Id;
//PB.Pricebook2Id=Prod.Id;
PB.Pricebook2=pricebook[1];
PBList.Add(PB);
}
insert PBLIst;
}
}
Now above you need to find out how to insert price boom also.
Regards:
kritin