This post is a part of the daily blog series
Day #9 – Workaround for PricebookEntry Trigger
Salesforce has quite a few limitations on Product, Pricebook and Pricebookentry objects. The one that I recently faced on Pricebookentry is that we are not allowed to write triggers on Pricebookentry which is a big deal for our requirement. The business wanted to perform some actions every time a Standard Pricebook is added to Products i.e., a Pricebookentry is inserted in Products
Before I talk about the workaround, please vote for the Idea first – Allow Triggers on Pricebook2 and PricebookEntry objects
Solution: Instead of trigger I used a apex class and scheduled to run it every 15 mins. That is not a big load on Salesforce. The apex class holds one important query – It gets all the PriceBookEntry which are “modified in the last 15 minutes”. And then we perform all the updates required based on that.
I used the field SystemModStamp. So that includes both “Inserts and Updates” done on PriceBookEntry
List<PriceBookEntry> PBEUpdated15mins = [SELECT Id,Pricebook2Id FROM PriceBookEntry WHERE SystemModStamp >:Datetime.now().addMinutes(-15)];
If you want the scheduler to run only on Insert of Pricebookentry, then instead of SystemModStamp, use CreatedDate in the query WHERE condition.
List<PriceBookEntry> PBEUpdated15mins = [SELECT Id,Pricebook2Id FROM PriceBookEntry WHERE CreatedDate >:Datetime.now().addMinutes(-15)];
Read all other tips of the blog series here – A Tip A Day, daily dosage of learning!