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
karthikeya 2karthikeya 2 

update fiels using batch class?

Hi all how to update a field using batchclass.

can any provide a sample code.

thanks in advance.
ManojjenaManojjena
Hi Karthikeya,

Check below link it will help to know some concepts  of  batch apex .Also there is an example which will help you to update record through batch apex .Details are explained in the post .
http://manojjena20.blogspot.in/2015/06/batch-apex-in-depth.html

Let me know if it helps !!
Thanks 
Manoj
Arun KumarArun Kumar

Hi Karthikeya,

Check below code it will help to know some concepts  of  batch apex.
 
global class UpdateAccountBatch implements Database.Batchable<sObject>{

     global Database.QueryLocator start(Database.BatchableContext BC)
    {        
        String query = 'SELECT Id, Name  FROM Account';
        return Database.getQueryLocator(query);
    }
   
    global void execute(Database.BatchableContext BC, List<Account> scope)
    {              
        List<Account> acctToUpdateList =  new List<Account>();
        
        for(Account accObj: scope){
                accObj.Name = 'Test 123';
                acctToUpdateList .add(accObj);             
        }        

        // TO Update account records        
        if(!acctToUpdateList .isEmpty()){
            Database.update(acctToUpdateList , false);
        }              
    }

    global void finish(Database.BatchableContext BC){}    
    
}

Let me know if it helps !!
Thanks 
Arun
jyothsna reddy 5jyothsna reddy 5
Hi Karthikeya,
    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 it helps you.

Regards,
Jyothsna D.