I am newbie in Apex, but I am trying to write some code. The purpose was simple: I have PricebookEntry object on Salesforce, and 2 lookup fields:
- Pricebook2Id
- Price_Book_2__c I want to fill Price_Book_2__c with the same value as Pricebook2Id on each record.
When I opened and run batch in Anonymouse Window I get error: Line: 3, Column: 25 Variable does not exist: Pricebook2Id
Here is the Apex class:
global class UpdatePriceBookCustomField implements Database.batchable<sObject>{
global final String query;
global Database.QueryLocator start (Database.BatchableContext BC) {
return Database.getQueryLocator(query);}
global void execute(Database.BatchableContext BC,List<sObject> scope){
List<PricebookEntry>pbeToUpdate = new List<PricebookEntry>();
for (sObject s : scope){PricebookEntry a = (PricebookEntry)s;
if(a.Price_Book_2__c==null){
a.Price_Book_2__c=a.Pricebook2Id;
pbeToUpdate.add(a);
}
}
update pbeToUpdate;
}
global void finish(Database.BatchableContext BC) {
}
}
Here is the batch:
UpdatePriceBookCustomField PBE = new UpdatePriceBookCustomField();
PBE.query='Select Id, Pricebook2Id, Price_Book_2__c from PricebookEntry';
PBE.Price_Book_2__c=PBE.Pricebook2Id;
ID batchprocessid = Database.executeBatch(PBE);
Anyone has any idea why it don't want to run? I don't know why Apex is not seeing the Pricebook2Id...