A Tip A Day #11 – Workaround for PriceBookEntry Trigger/Workflow

This post is a part of the daily blog series 

A Tip A Day, daily dosage of learning!

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)];
Similarly if you want only Updates, you can replace with LastModifiedDate

Read all other tips of the blog series here – A Tip A Day, daily dosage of learning!


One Reply to “A Tip A Day #11 – Workaround for PriceBookEntry Trigger/Workflow”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s