I'm trying to create a batch class that updates a set of number fields based on a particular profile that edits them and a schedule class that updates those changes every night at midnight. So far with testing it doesn't look like the code is making the updates and I was wondering if someone could take a look and tell me what I'm doing incorrectly

Batch Class:

public class LAM_RollupBatch implements Database.Batchable<SObject> {

    private Integer recordsProcessed = 0;

    profile userProfile = [SELECT Id FROM Profile WHERE Name = 'Informatica_Connector'];

    public Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id, Parent_Account_for_LAM__c, Rolling_12_Month_Total_CCF__c, Rolling_12_Month_Total_Revenue__c, Rolling_12_Month_Total_Usage_kWh__c, Account_Max_Demand_kw__c, AnnualRevenue FROM Account WHERE LastModifiedDate = YESTERDAY';
        return Database.getQueryLocator(query);

    public void execute(Database.BatchableContext BC, List<Account> scope) {
       List<Account> acc = new List<Account>();
        for (Account a : scope) {

           if(userProfile.Id == UserInfo.getProfileId()){

                a.Rolling_12_Month_Total_CCF__c =  a.Rolling_12_Month_Total_CCF__c;
                a.Rolling_12_Month_Total_Revenue__c = a.Rolling_12_Month_Total_Revenue__c;
                a.Rolling_12_Month_Total_Usage_kWh__c  = a.Rolling_12_Month_Total_Usage_kWh__c;       
                a.AnnualRevenue = a.AnnualRevenue;
                a.Account_Max_Demand_kw__c = a.Account_Max_Demand_kw__c;                                   


        update scope;

    public void finish(Database.BatchableContext BC) {
        System.debug(recordsProcessed + ' records processed');

Schedulable Class:

global class LAM_RollupSchedulable implements Schedulable {

   global void execute(SchedulableContext SC) {
      LAM_RollupBatch B = new LAM_RollupBatch();        

    LAM_RollupSchedulable s = new LAM_RollupSchedulable();
    String sch = '0 0 0 * *';
    String jobID  = system.schedule('Update Batch', sch , s);

  • I have a theory the issue lies in your WHERE clause. LastModifiedDate is actually a date time and when you use the = operator its looking for records matching the exact day/time. You might want to change it to WHERE LastModifiedDate > YESTERDAY AND LastModifiedDate < TODAY – TemporaryFix Mar 7 '19 at 15:58
  • You could also add schedulable to your batch class instead of having a separate class just to make it schedulable. – Logie C Mar 7 '19 at 16:13
  • N.B. your batch class needs to implement Database.Stateful as otherwise userProfile won't have any value when execute() is executed – cropredy Mar 7 '19 at 16:26

You are missing to execute the Batch class in your Scheduled class. The execute in the LAM_RollupSchedulable schedulable class should look like as below:

global void execute(SchedulableContext SC) {
      LAM_RollupBatch B = new LAM_RollupBatch();       

      // now execute the batch

Additionally, make sure that the User you are using to schedule the batch class has the same Profile Id as expected in the condition in your batch class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy