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
Max_gMax_g 

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?

 

empucempuc

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();

database.executebatch(batch,YourSizeOfTheBatch);
}

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!