You need to sign in to do that
Don't have an account?
Ravi23
How to write a where clause in Batch class string query?
Hi All,
Below is my Batch class. Is that always we require to pull complete records in databse in query field or can we filter using where condition. If we can filter can some one help me on this as I am getting errors
global class batchContactAccountNameUpdate implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT Id, AccountId,Type__C FROM Contact where Type__c=:Asian Entities';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Contact> scope) {
for(Contact c : scope)
{
if(c.AccountId==null){
c.AccountId = '0011900000BiZzn';
}
}
update scope;
}
global void finish(Database.BatchableContext BC) {
}
}
Below is my Batch class. Is that always we require to pull complete records in databse in query field or can we filter using where condition. If we can filter can some one help me on this as I am getting errors
global class batchContactAccountNameUpdate implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT Id, AccountId,Type__C FROM Contact where Type__c=:Asian Entities';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Contact> scope) {
for(Contact c : scope)
{
if(c.AccountId==null){
c.AccountId = '0011900000BiZzn';
}
}
update scope;
}
global void finish(Database.BatchableContext BC) {
}
}
All Answers
Try this
Thank,
Alex
String query = 'SELECT Id, AccountId,Type__C FROM Contact where Type__c= \'Asian Entities\'';
Using this \', we are escaping the string quotes so it takes your filter value as a string value.
Hope this helps.
Thanks a lot for your help it worked as expected. Can I request your final help how to write a test class and schedule this batch class if we write a schedule class for this batch class do we need to write test class for the schedule class also can you help me on the same?
For run shedule job you can use system.schedule method
This is my example of reshedule job.
I wrote a seperate schedule class as below
global class Scheduler_class implements Schedulable{
public static String sched = '0 00 00 * * ?'; //Every Day at Midnight
global static String scheduleMe() {
Scheduler_class SC = new Scheduler_class();
return System.schedule('UpdateConstantContactAccountswithoutNames', sched, SC);
}
global void execute(SchedulableContext sc) {
batchContactAccountNameUpdate b1 = new batchContactAccountNameUpdate();
ID batchprocessid = Database.executeBatch(b1,50);
}
}
It is working fine can you help me in writing the tezt class for the batch class and schedule class
I tried the similar way it is giving me the below error
Constructor not defined: [batchContactAccountNameUpdate].<Constructor>(String)
Can you help me where I am going wrong?