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!


5 Replies to “A Tip A Day #11 – Workaround for PriceBookEntry Trigger/Workflow”

  1. This solution won’t work. Batch jobs are not guaranteed to run when they are scheduled – they may be delayed by many minutes so some of your pricebook entries would be skipped.
    Instead you have to have a persisted value (maybe use customsetting) that stores the last mod timestamp of any processed pricebook entry. Then query for any value later than that.

    Liked by 1 person

    1. Hi Bruce. Good point, yes scheduled jobs may be delayed by few minutes if you have few other jobs scheduled. My org was very new and this was the first scheduled job, so this worked very well for me without issues. Good suggestion on using customsetting and last modified timestamp.

      Another alternative I can think of is, to update a custom field (Updated__c) on pricebookentry after it is processed by the job and then update the query to pickup last 30mins (instead of 15mins) and exclude the Updated__c ones. The scheduled job will still run every 15mins.

      I will include your suggestion as well in the post. Thanks for pointing it out.


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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: