function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
nagalakshminagalakshmi 

How to update the group of fields using batch apex

Hi,

How to update the group of fields using batch apex class. I am update the single field using batch apex class. But am unable to update group of fields. Any one help me for solving these. I am follow the class for single field updation 

 

}

global class BatchUpdateField implements Database.Batchable<sObject>{
global final String Query;
global final String Field;
global final String Value;
global BatchUpdateField(String q, String f, String v){
Query = q;
Field = f;
Value = v;
}
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){  
for(sobject s : scope){
s.put(Field,Value);
}

update scope;
}
global void finish(Database.BatchableContext BC){  
AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed,TotalJobItems,CreatedBy.Email
from AsyncApexJob where Id = :BC.getJobId()];
string message = 'The batch Apex job processed ' + a.TotalJobItems + ' batches with '+ a.NumberOfErrors + ' failures.';
// Send an email to the Apex job's submitter notifying of job completion.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Salesforce BatchUpdateField ' + a.Status);
mail.setPlainTextBody('The batch Apex job processed ' + a.TotalJobItems + ' batches with '+ a.NumberOfErrors + ' failures.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  
}

 

using the query for execution from systemlog:

 

Database.executebatch(batch,200);

string query='select id,website from account';
batchupdatefield batch=new batchupdatefield(query,'website','www.dskvap.com');
Best Answer chosen by Admin (Salesforce Developers) 
ritika@developerforceritika@developerforce

Hi,

 

In order to update multiple fields, you batch class would need some modifications -

Use a map of field names and values, instead of two variables field and value

 

global class BatchUpdateField implements Database.Batchable<sObject>{
global final String Query;
global final Map<String, String> fieldValues;
global BatchUpdateField(String q, Map<String, String> fieldValues){
Query = q;
this.fieldValues = fieldValues
}
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){  
for(sobject s : scope){
for(String fldName : fieldValues.keySet()) {

s.put(fldName, fieldValues.get(fldName));

}
}

update scope;
}
global void finish(Database.BatchableContext BC){  
AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed,TotalJobItems,CreatedBy.Email
from AsyncApexJob where Id = :BC.getJobId()];
string message = 'The batch Apex job processed ' + a.TotalJobItems + ' batches with '+ a.NumberOfErrors + ' failures.';
// Send an email to the Apex job's submitter notifying of job completion.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Salesforce BatchUpdateField ' + a.Status);
mail.setPlainTextBody('The batch Apex job processed ' + a.TotalJobItems + ' batches with '+ a.NumberOfErrors + ' failures.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  
}

 

using the query for execution from systemlog:

 

Database.executebatch(batch,200);

string query='select id,website from account';
Map<String, String> fldValues = new Map<String, String>();
fldValues.put('website', 'www.dskvap.com');
fldValues.put('email' , 'someemail@test.com');
// and so on for all fields you want to udpate
batchupdatefield batch=new batchupdatefield(query,fldValues);



In this example I have used a String map, considering all String values to be udpated. Modify the map to use an 'object' type , for varying type of field updates.

 

Hope this helps.

All Answers

ritika@developerforceritika@developerforce

Hi,

 

In order to update multiple fields, you batch class would need some modifications -

Use a map of field names and values, instead of two variables field and value

 

global class BatchUpdateField implements Database.Batchable<sObject>{
global final String Query;
global final Map<String, String> fieldValues;
global BatchUpdateField(String q, Map<String, String> fieldValues){
Query = q;
this.fieldValues = fieldValues
}
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){  
for(sobject s : scope){
for(String fldName : fieldValues.keySet()) {

s.put(fldName, fieldValues.get(fldName));

}
}

update scope;
}
global void finish(Database.BatchableContext BC){  
AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed,TotalJobItems,CreatedBy.Email
from AsyncApexJob where Id = :BC.getJobId()];
string message = 'The batch Apex job processed ' + a.TotalJobItems + ' batches with '+ a.NumberOfErrors + ' failures.';
// Send an email to the Apex job's submitter notifying of job completion.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Salesforce BatchUpdateField ' + a.Status);
mail.setPlainTextBody('The batch Apex job processed ' + a.TotalJobItems + ' batches with '+ a.NumberOfErrors + ' failures.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  
}

 

using the query for execution from systemlog:

 

Database.executebatch(batch,200);

string query='select id,website from account';
Map<String, String> fldValues = new Map<String, String>();
fldValues.put('website', 'www.dskvap.com');
fldValues.put('email' , 'someemail@test.com');
// and so on for all fields you want to udpate
batchupdatefield batch=new batchupdatefield(query,fldValues);



In this example I have used a String map, considering all String values to be udpated. Modify the map to use an 'object' type , for varying type of field updates.

 

Hope this helps.

This was selected as the best answer
nagalakshminagalakshmi

Hi Ritika,

 

Its working good. Thanks a lot..

 

Thanks,

Lakshmi.

yaminiyamini

Hi Lakshmi and ritika,

 

Thanks for posting the code. I am also seeking for the same thing. 

 

Could you help me please for, How to insert the records in to object through batch apex class..

 

Thanks,

Yamini.

nagalakshminagalakshmi
Hi Ritika, Actually am updating the group of fields through batch apex class. Its working fine and i wrote the test coverage its got 92%. When i am deploying the batch apex class i got the error as 'batch apex is not iterable in test methods'. How to solve this error. Please help me. Thanks, Lakshmi.
CNishantCNishant
Hi @ritika,

how to use map of Object, I have to update all types of fields, integer date, boolean?