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

Trigger for creating a pricebook entry



I need some suggestions to write a trigger for the following criteria


     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.


Any suggestions are highly appreciated.





Doesnt sound difficult.  You'll need to run on insert and update, and test to see if the trigger is updating or inserting.

Then, if inserting just create your new pricebookentry object, populate the values based on those in the product, point the pricebook to the standardpricebook id and insert.

If updating, run a query to retrieve the existing pricebookentry, update the values and update into the database.




        Thanks ministe2003 but can you explain me in detail whether the trigger should be Before or After?


from the dev guide:


  • Before triggers can be used to update or validate record values before they are saved to the database.
  • After triggers can be used to access field values that are set by the database (such as a record's Id or lastUpdated field), and to affect changes in other records, such as logging into an audit table or firing asynchronous events with a queue.

In this case it probably wont make a difference but I tend to go with after in this case, so you always have the most up to date information