Introduction
I want to make a price revision on a pricebook, how to do it ?
Custom field you need

Apex
/* This class is used to implement logic to update price line items to update list price for given price book to revised price.
* This is a schedulable class creats schedule job for updating price based on start date set while adding revision entry.
*/
global class RevisionPriceUpdate implements Schedulable {
global Id pbId;
global Decimal rPrice;
global RevisionPriceUpdate(){
String priceBookName = 'Standard Price Book'; // Replace with the actual name of your Price Book
// Query to retrieve the Price Book ID based on the Price Book Name
List<Pricebook2> priceBooks = [SELECT Id FROM Pricebook2 WHERE Name = :priceBookName LIMIT 1];
if (!priceBooks.isEmpty()) {
pbId = priceBooks[0].Id;
System.debug('Price Book ID: ' + pbId);
} else {
System.debug('Price Book not found with the specified name: ' + priceBookName);
}
rPrice = 1.5;
}
global void execute(SchedulableContext sc){
System.debug('Schedulable revision update start');
RevisionPriceUpdateBatch batch = new RevisionPriceUpdateBatch(pbId, rPrice);
Id batchId = Database.executeBatch(batch, 200);
System.debug('Schedulable revision update end');
}
// Method to schedule the job
global static void scheduleJob() {
RevisionPriceUpdate scheduledJob = new RevisionPriceUpdate();
//annually
// Cron Expression: '0 0 0 1 1 ? *'
// - '0 0 0' specifies midnight
// - '1' in the "day of the month" field specifies the first day of the month
// - '1' in the "month" field specifies January
// - '?' is used to represent no specific day of the week
//String cronExp = '0 0 0 1 1 ? *';
//----------Monthly--------------
// Schedule the job to run on the first day of every month at midnight
// Cron Expression: '0 0 0 1 * ?'
// - '0 0 0' specifies midnight
// - '1' in the "day of the month" field specifies the first day of the month
// - '*' in the "month" field means every month
// - '?' is used to indicate no specific day of the week
String cronExp = '0 0 0 1 * ?';
//---------Weekly--------------
// Cron Expression: '0 0 0 ? * 2'
// - '0 0 0' specifies midnight
// - '?' is used to represent any day of the month since we are specifying the day of the week separately
// - '*' in the "month" field means every month
// - '2' in the "day of the week" field means Monday
//String cronExp = '0 0 0 ? * 2';
//--------Daily--------
// Cron Expression: '0 0 0 * * ?'
// - '0 0 0' specifies midnight
// - '*' in the "day of the month" field means every day of the month
// - '*' in the "month" field means every month
// - '?' is used to indicate no specific day of the week
//String cronExp = '0 0 0 * * ?';
//trimestre
// Cron Expression: '0 0 0 1 1/3 ? *'
// - '0 0 0' specifies midnight
// - '1' in the "day of the month" field specifies the first day of the month
// - '1/3' in the "month" field means every 3 months, starting from January
// - '?' is used to represent no specific day of the week
//String cronExp = '0 0 0 1 1/3 ? *';
System.schedule('RevisionPriceUpdateJob', cronExp, scheduledJob);
}
}
/* This class is used to implement logic to update price line items to update list price for given price book to revised price.
* This is a schedulable class creats schedule job for updating price based on start date set while adding revision entry.
*/
global class RevisionPriceUpdateBatch implements Database.Batchable<SObject> {
global Id pbId;
global Decimal rPrice;
global RevisionPriceUpdateBatch(String pricebookId, Decimal revPrice) {
this.pbId = pricebookId;
this.rPrice = revPrice;
}
public Database.QueryLocator start(Database.BatchableContext bc) {
System.debug('Batchable revision update start');
String query = 'SELECT Id, UnitPrice, ' +
'PreviousUnitPrice__c ' +
'FROM PricebookEntry WHERE Pricebook2.Id = :pbId';
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext bc, List<PricebookEntry> scope) {
System.debug('Batchable revision execution start');
List<PricebookEntry> pbeupdated = new List<PricebookEntry>();
for (PricebookEntry p : scope) {
if (p.UnitPrice != null)
p.PreviousUnitPrice__c = p.UnitPrice;
if (p.PreviousUnitPrice__c != null)
p.UnitPrice = p.PreviousUnitPrice__c * rPrice ;
pbeupdated.add(p);
}
if (pbeupdated.size() > 0)
update pbeupdated;
}
public void finish(Database.BatchableContext bc) {
System.debug('Batchable revision execution finish');
}
}
Result
Before :

After:

Here's the git code: https://github.com/AourLegacy/AourFactory/tree/Revision-Price
Comments