You need to sign in to do that
Don't have an account?
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);
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);
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
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);
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.
Hi Ritika,
Its working good. Thanks a lot..
Thanks,
Lakshmi.
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.
how to use map of Object, I have to update all types of fields, integer date, boolean?