+ Start a Discussion
VashnarVashnar 

How I update this to include a date constraint?

Hello... Extremely new to Apex code here. Found this online and modified it to work for what I need, but I am missing a date component. I'd like the Class to only count activities that have occurred within the last 90 days, and if there are none reset the count to zero.

Here's what I have so far:

public with sharing class AccountActivityCount {
    public static Boolean didRun = false;
    public static String acctPrefix =  Account.sObjectType.getDescribe().getKeyPrefix();
    public static void updateAccountCounts(Set<ID> acctIds) {
        if (didRun == false) {
            didRun = true;
            List<Account> accts = [SELECT ID, activity_count__c, (SELECT ID FROM Tasks), (SELECT ID FROM Events) FROM Account WHERE ID IN :acctIds];
            List<Account> updateAccts = new List<Account>();
            for (Account o : accts) {
                Integer count = o.tasks.size() + o.events.size();
                if (o.activity_count__c != count) {
                    o.activity_count__c = count;
                    updateAccts.add(o);
                }
            }
            try {
                update updateAccts;
            } catch (Exception e) {
            }
        }
    }
    /*
    * Test method for this class and TaskUpdateOpportunity and EventUpdateOpportunity
    */
    public static testMethod void testCountTask() {
        Account a = new Account(name='Test');
        insert a;
        Task t = new Task(subject='Test Activity', whatId = a.id);
        insert t;
        a = [SELECT ID, activity_count__c FROM Account WHERE ID = :a.id];
        System.assertEquals(1,a.activity_count__c);
        didRun = false; //Reset
        t.whatId = null;
        update t;
        a = [SELECT ID, activity_count__c FROM Account WHERE ID = :a.id];
        System.assertEquals(0,a.activity_count__c);
        didRun = false; //Reset
        Event e = new Event(subject='Test Event', whatId = a.id, startDateTime = System.Now(), endDateTime = System.now());
        insert e;
        a = [SELECT ID, activity_count__c FROM Account WHERE ID = :a.id];
        System.assertEquals(1,a.activity_count__c);
        didRun = false; //Reset
        t.whatId = a.id;
        update t;
        a = [SELECT ID, activity_count__c FROM Account WHERE ID = :a.id];
        System.assertEquals(2,a.activity_count__c);
        didRun = false; //Reset
        e.whatId = null;
        update e;
        a = [SELECT ID, activity_count__c FROM Account WHERE ID = :a.id];
        System.assertEquals(1,a.activity_count__c);
        didRun = false; //reset
        delete t;
        a = [SELECT ID, activity_count__c FROM Account WHERE ID = :a.id];
        System.assertEquals(0,a.activity_count__c);       
    }
}

Thanks in advance, any help is greatly appreciated!
Brian
Kiran RKiran R
In your first query to get the list of accounts, You are querying Task and Event like below:
[SELECT ID, activity_count__c, (SELECT ID FROM Tasks), (SELECT ID FROM Events) FROM Account WHERE ID IN :acctIds];

I believe you can filter Task and Event further to get the last 90 days event only. Like below:

date d = system.today().addDays(-30);
[SELECT ID, activity_count__c, (SELECT ID FROM Tasks WHERE CreatedDate >= :d), (SELECT ID FROM Events WHERE CreatedDate >= :d) FROM Account WHERE ID IN :acctIds];

That should resolve your problem i belive. You can switch out the CretedDate with LastModifiedDate if that's what your requirement is.