You need to sign in to do that
Don't have an account?
RAMANJINEYULU GOGULA
how to execute if/else statements in batch apex execute method?
You can observe here, it is executing but not affecting any changes in the records, please help me out?
public class ramsBatchTownName implements Database.Batchable<sObject> {
public String field;
public String v1,v2,v3,v4,v5;
public String query;
public ramsBatchTownName(String q){
field='Address__c';
v1='KKM';
v2='MDP';
v3='NAND';
v4='PLVD';
v5='NO ADDRESS';
query=q;
}
public Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext BC, List<Ram__c> batch){
for(Ram__c rc:batch){
if(rc.Districts__c=='Anantapur')
rc.put(field,v1);
else if(rc.Districts__c=='Chittor')
rc.put(field,v2);
else if(rc.Districts__c=='Kurnool')
rc.put(field,v3);
else if(rc.Districts__c=='Kadapa')
rc.put(field,v4);
}
update batch;
}
public void finish(Database.BatchableContext BC){
}
}
public class ramsBatchTownName implements Database.Batchable<sObject> {
public String field;
public String v1,v2,v3,v4,v5;
public String query;
public ramsBatchTownName(String q){
field='Address__c';
v1='KKM';
v2='MDP';
v3='NAND';
v4='PLVD';
v5='NO ADDRESS';
query=q;
}
public Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext BC, List<Ram__c> batch){
for(Ram__c rc:batch){
if(rc.Districts__c=='Anantapur')
rc.put(field,v1);
else if(rc.Districts__c=='Chittor')
rc.put(field,v2);
else if(rc.Districts__c=='Kurnool')
rc.put(field,v3);
else if(rc.Districts__c=='Kadapa')
rc.put(field,v4);
}
update batch;
}
public void finish(Database.BatchableContext BC){
}
}
Rectified Solution:
WHILE PASSING QUERY into start(), I DIDN'T RETRIEVE THE FILED CALLED Districts__c.
String query='SELECT Address__c, Districts__c FROM Ram__c';
ID executebatch=Database.executeBatch(new ramsBatchTownName(query));