Skip to main content
Andrew Perez ha fatto una domanda in #Data Management
I am trying to count the number of Events related to a Contact that is displayed in a Contact field (Activity_Count__c).  The count works when the Event is created but if an Event is deleted the count won't subtract 1 until a new Event is created.  Here is what I have:

 

trigger CountEvents on Contact (before update, after delete) {

 

    List<Id> idList =  new List<Id>();

 

    Integer eventCount = 0 ;

 

  

 

    for(contact co : trigger.new){

 

    idList.add(co.id);   

 

    }

 

       

 

    List<aggregateResult> eventresults = [SELECT count(id) FROM Event WHERE whoid in:idList and (Meeting_Status__c = 'Converted' or Meeting_Status__c = 'Not Converted')];

 

   

 

    for (AggregateResult ar : eventresults )  {

 

  

 

    eventCount = (Integer)ar.get('expr0');

 

  

 

}

 

     for(contact con : trigger.new){

 

       con.Activity_Count__c =  eventCount;  // field on which event count needs to be updated

 

    }

 

   

 

    for(contact tact : trigger.old)

 

        if(trigger.isDelete){

 

      

 

            tact.Activity_Count__c = tact.Activity_Count__c - 1;

 

       

 

    }

 

}
5 risposte
  1. 9 lug 2014, 17:29
    Andrew,

     

    So this actually not a post to debug that code but a humble suggestion...

     

    Rolling up of records....Just another trivial problem!

     

    I have had many situtions like this before and I have had ocassions wherein I went to that extremes where I had so many Rollup Triggers and Logics and finally went to a situation where it became completely un-manageable. It was then that a Force.com MVP  Andrew Fawcett came up with an awesome App!

     

    This an app very similar to Rollup Helper on the AppExchange but created by a Force.com MVP in the name - Andrew Fawcett(from Financial Force )[1] and it's called - Declarative Rollups for Lookups! So the work initially started way back in July, 2013 and the app has evolved so much and moreover it's an open source initiative too! So you'll not find it on AppExchange(but very recently it had passed the security review from AppExchange too) but you have to go to the GitHub [2] where you'll find the source code and everything else that you need to set it up and running.

     

    So just like the Rollup Helper, this one too supports - Filter Criteria on Rollups, both Real-time and Scheduled Rollups etc. all available free of cost (huge thanks to the brains behind it!)

     

    Here is the Developer Force blog[3] which you can use to read more about this but it's a bit technical in nature plus you can visit Andy's blogs[4] to get the grip. Just like any AppExchange app, this can be used/maintained by any one since it's all just point-and-click to configure it.

     

    [1]: Andrew Fawcett(from Financial Force) (https://developer.salesforce.com/forums/ForumsProfileAboutMe?userId=005F0000003FjYQIA0)

     

    [2]: GitHub repo (https://github.com/afawcett/declarative-lookup-rollup-summaries)

     

    [3]: Developer Force blog (https://developer.salesforce.com/page/Declarative_Rollup_Summary_Tool_for_Force.com_Lookup_Relationships)

     

    [4]: Andy's blogs (http://andyinthecloud.com/2014/02/09/new-release-spring14-declarative-rollup-summary-tool/)

     

    Now to your Apex Code, you probably will have to isolate section dealing with after Delete to a different if block [if (trigger. isAfter && trigger. isDelete )] and then collect all the Contact records into a List Collection with a for loop as you have already done and finally you'll need to run an UPDATE on this LIST.
0/9000