0

I have a few open read/write number fields that are being updated through counters in a workflow. New activity comes in, the field will be increased by one. But I want to reset the counts every week.

So I'm using a scheduler class:

    global class Reset7DayActivityCounterScheduler implements Schedulable{

        global void execute(Schedulablecontext sc)
          {
             Reset7DayActivityCountersBatch lead = new Reset7DayActivityCountersBatch();
             Database.executeBatch(lead,200);
          }


   }

And a batch class:

global class Reset7DayActivityCountersBatch implements Database.Batchable<sObject>{

    global Database.querylocator start(Database.BatchableContext BC){
        String query = 'SELECT Id, '
                + 'of_Calls_Last_7_Days__c, '
                + 'of_Emails_Sent_Last_7_Days__c, '
                + 'of_Replies_Last_7_Days__c '
            + 'FROM Lead';  

        return Database.getQueryLocator(query);
    }

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

        for(Lead ob1 : scope){
            ob1.of_Calls_Last_7_Days__c = 0;
            ob1.of_Emails_Sent_Last_7_Days__c = 0;
            ob1.of_Replies_Last_7_Days__c = 0;

        }                                                                                                                                                                                                                                                                                                                                                                                                                                    

        update scope;
    }

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

Then I'm using "Schedule Apex" to run the thing. Though I'm seeing no changes to the aforementioned fields. They still maintain their previous numbers.

Not sure what to do here.

5
  • 1
    I'd recommend you consider DLRS for this use case. It's open source and lets you define real-time and scheduled, filtered roll-ups. It might allow you solve this with no code. – David Reed Mar 25 '20 at 0:48
  • 4
    As to your question - what do the logs tell you when your job runs? Are there errors? Is it running on schedule? – David Reed Mar 25 '20 at 0:49
  • @DavidReed --- I'm now seeing this error after the batch completes and goes to update: "First error: Update failed. First exception on row 153 with id 00Q6A000002Gn04UAC; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please list the No Support Reason Details: [No_Support_Reason_Details__c]" – Kyle Schiess Mar 25 '20 at 14:45
  • Not sure why this field is throwing an error. It's an open read/write text field used in a couple reports and layouts and one validation rule. That rule has nothing to do with the fields I'm trying to update. – Kyle Schiess Mar 25 '20 at 15:14
  • I think this looks like you have a validation rule that fires on some of these fields but is looking for a comment in another field. – SeanGorman Mar 25 '20 at 15:45
0

Your class is not taking effect because it is failing, as your logs show.

This error

First error: Update failed. First exception on row 153 with id 00Q6A000002Gn04UAC; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please list the No Support Reason Details: [No_Support_Reason_Details__c]

indicates that your database has bad data, or clumsy validation rules or triggers, or both.

Your code is not touching this field, but some validation rule or trigger that is trying to enforce data integrity is throwing this error because existing records do not populate it correctly - even though you're not changing this field.

You need to either correct the data in your database, or fix the validation logic so that it checks for fields being changed before it fires the validation.

1
  • Ended up just deactivating that validation rule. I'm guessing it was a lead that fit the conditions of that validation rule, and was edited as such before the validation rule was implemented. – Kyle Schiess Mar 26 '20 at 14:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.