+ Start a Discussion

Need batch code to update field on custom object

I have not written any batch apex yet.  I want to update a field on a custom object that will then fire a trigger to rollup sales dollars to managers.  I have the rollup working properly when I manually update the controlling field, but I want to be able to schedule this job to run each day. 


Anyone have a good example of this type of code?



global class UpdateCustomFieldBatch implements Database.Batchable<sObject>, Schedulable {

global Database.QueryLocator start(Database.BatchableContext BC){

return Database.getQueryLocator('select Id, YourCustomField__c from YourCustomObject__c  /* WHERE - Your potential conditions here */);

global void execute(Database.BatchableContext BC, List<sObject> scope){

for(sObject s1 : scope){

s1.put('YourCustomField__c' , 'value2beAssigned');


update scope;


global void execute(SchedulableContext sc) {
UpdateCustomFieldBatch batch = new UpdateCustomFieldBatch();


global void finish(Database.BatchableContext BC){
/* additional code if You want to do smth in the end of the process - ie. send email with the notfiication or summary */


Best regards!