You need to sign in to do that
Don't have an account?
ApexDev
Variable does not exist: Apex Batch
Hi Guys!
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...
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...
You don’t have to declare the class’s access modifier as ‘global’ unless you’re developing a managed package, otherwise use ‘public’.
For the second argument in the ‘execute’ method you’d want to define the sObject type for the scope list instead of using the generic ‘sObject’ like so: I saw you tried assigning the variable ‘query’ a value outside of the batch class. The class variable ‘query’ is set to final and cannot be changed. I recommend immediately assigning a value to this variable within the class and using it inside the ‘start’ method directly. I also recommend adding a WHERE clause in your query to get back only records that have ‘Price_Book_2__c’ set to null.
Here's the overall idea of the updated class: As to your error, PBE is an instance of the class 'UpdatePriceBookCustomField'. This class doesn't have a Pricebook2Id variable, only the query variable you defined in it, which is type 'final' and would cause a runtime error when trying to change it. Hope this helps!
As Leamsi said, you have missed the query in start method and you were not passing the PricebookEntry object.
Try with below code. If this helps, Please mark it as best answer.
Thanks!!