+ Start a Discussion

Batch apex code

I've created a trigger on Contact that will roll-up opportunities into given fields. These fields are seperated by FY and other criteria that aren't doable by a formula or roll-up summary.


The problem is, the information only updates when opportunities related to that given contact are updated. For example, if an opportunity is entered it will calculated the $ amount in the "Current FY" field, but if no further updates happen, that field will remain populated.


My thought was to create a scheduled batch apex job to happen each fiscal year to reconcile the information - is this the best way to do this?


Here is the code that I have so far:


global class scheduledMonthly implements Schedulable {

    public static String CRON_EXP = '0 0 0 21 AUG ? *';
    global static String scheduleIt() {
        scheduledMonthly sm = new scheduledMonthly();
        return System.schedule('Monthly Reconciliation', CRON_EXP, sm);
    global void execute(SchedulableContext sc) {
    ExampleBatchClass ebc = new ExampleBatchClass();



global class ExampleBatchClass implements Database.Batchable<sObject>{

    String query;

    global ExampleBatchClass(){
    // Batch Constructor

    // Start Method
    global Database.QueryLocator start(Database.BatchableContext BC){
        query = 'select id from contact';
        return Database.getQueryLocator(query);

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

        List<Contact> conlist = (List<Contact>)scope;
        List<Contact> contoupdate = new List<Contact>();
                //Get all contacts
                for (Contact con : conlist){
                    //execute logic inside for loop


    global void finish(Database.BatchableContext BC){
        // Logic to be Executed at finish

 Does this seem like the best way to go about this? Thank you!


So will this just brute force update all contacts on a periodic basis in order to force a re-calc?  It's feasible, and probably will do no harm - although it does seem like it might be heavy handed.  Is there any other way to narrow the scope?


Hi Josh -  I guess I could narrow the scope to only conacts with opportunities, but I'm not sure of another way around it besides that. The update would only really be necessary once a year a the start of the FY. That way contacts without any new opportunities would (properly) show that current FY amounts are $0.




How about a scheduled batch job (daily/weekly/etc) that would check opportunities modified between current job run and previous run time, pull their contacts and do the  business logic 


I gotya.  Obviously I would test in a full config sandbox against something resembling prod data - but technically totally feasible.  And going brute force once or twice a year is probably reasonable.  Against a large data set going more frequently my cause unexpected results (since time to completion on data might be nearly as long as your time frame ;) ).