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
podgercor1.3917890117104897E12podgercor1.3917890117104897E12 

Event Count COde

I have used some bits of code here to create a Trigger that will count the number of Completed Events on an Account in a certain Fiscal Period - the problem I have is that the code keeps on counting events outside of the quarter - so if I create 2 events - 1 with a due date 28.04.14 and another with 20.02.14 i would expect my code to return only 1 instead it keeps returning 2 - can anyone see what I am doing wrong here?

Trigger

trigger Event_Counter on Event (after insert,after delete,after update,after undelete) {
Set<ID> acctIds = new Set<ID>();
    String prefix =  '001';


datetime myDate = datetime.newInstance (2014, 03,28,00,00,00);
datetime myDate1 = datetime.newInstance (2014, 06,30,00,00,00);

    if (Trigger.new != null) {
        for (Event t : Trigger.new) {
            if (t.WhatId != null && String.valueOf(t.whatId).startsWith(prefix) && t.ActivityDate >= myDate && t.ActivityDate <= myDate1 && t.Meeting_Status__c == 'Completed' && t.Coverage_Report__c == 'Yes' && (t.Meeting_Type__c =='Face to Face Meeting' || t.Meeting_Type__c =='Sales Meeting Call') ) {
                acctIds.add(t.whatId);
            }
        }
    }

    if (Trigger.old != null) {
        for (Event t : Trigger.old) {
            if (t.WhatId != null && String.valueOf(t.whatId).startsWith(prefix)&& t.ActivityDate >= myDate && t.ActivityDate <= myDate1 && t.Meeting_Status__c == 'Completed' && t.Coverage_Report__c == 'Yes' &&( t.Meeting_Type__c =='Face to Face Meeting' || t.Meeting_Type__c =='Sales Meeting Call') ) {
                acctIds.add(t.whatId);
            }
        }
    }

    if (acctIds.size() > 0 )
        Event_Coverage_Count.updateAcctEventCounts(acctIds);
}

Class
public with sharing class Event_Coverage_Count {
    public static Boolean didRun = false;
  
    public static void updateAcctEventCounts(Set<ID> acctIds) {

        if (didRun == false) {
            didRun = true;

            List<Account> acct = [SELECT ID, Completed_Coverage_Count__c, (SELECT ID FROM Events) FROM Account WHERE ID IN :acctIds];
            List<Account> updateAcct = new List<Account>();

            for (Account a : acct) {
                Integer count = a.events.size();

                if (a.Completed_Coverage_Count__c != count) {
                    a.Completed_Coverage_Count__c = count;
                    updateAcct.add(a);
                }
            }
          
            try {
                update updateAcct;
            } catch (Exception e) {
             
            }
        }
    }


thank you
ShashForceShashForce
Hi,

This is happening because of this code in the class:

for (Account a : acct) {
                Integer count = a.events.size();

                if (a.Completed_Coverage_Count__c != count) {
                    a.Completed_Coverage_Count__c = count;
                    updateAcct.add(a);
                }
            }

You are directly assigning the total event count, but not the count of events which satisfy the criteria. Also, you are not firing the trigger when an event does not satisfy the criteria, hence the update on account will not happen to show the correct count.

Please look into it.

1.) Fire the trigger on events which do not satisfy the criteria as well.
2.) for the count, count the number of events which "satisfy the criteria", not the total count of events.

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank