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
Inanskar007Inanskar007 

Count of Completed Events of a contact for last one year

Hi All,

I am working on a requirement to compute the count of the completed Events for the past one year and palced the count on a contact. whenever a Event got deleted or added for a contact the count should be updated on a custom field of corresponding contact record

For this, I have written a trigger on Event objest as below

Trriger

trigger Event_updateActivityScore_trigger on Event (after insert, after update, after delete) {

Set<Id> e = new Set<Id>();

for(Event e1 : Trigger.new){

e.add(e1.id);

}

List<EventRelation> er = [select relationid from EventRelation where Eventid = :e and Relation.Type = 'Contact'];

Set<Id> i = new Set<Id>();

for(EventRelation er1 : er){

i.add(er1.id);

}


CountVisitsByContactpast365Controller.updateActivityScore(i);

}


and the follwoing class

Apex Class

public  with sharing  class CountVisitsByContactpast365Controller {
@future
public static void updateActivityScore(Set<Id> erid)
{
    System.debug(LoggingLevel.ERROR, ' event ids are' + erid);
    Set<Id> erid1 = new Set<Id>(erid);
     System.debug(LoggingLevel.ERROR, ' event relation ids are' + erid1);
    List<EventRelation> eventidset=  [select eventid from EventRelation where RelationId in:erid1];
   System.debug(LoggingLevel.ERROR, ' event ids are' + eventidset);
   Map<Id, EventRelation> er = new Map<Id, EventRelation>(eventidset);

   List <Event> finaleventidset= [select id from Event where EndDateTime>=LAST_N_DAYS:365 and Visit_Status__c='Completed' and isDeleted=false and ID in: er.keyset()];
   Map<Id, Event> e = New Map<Id, Event>(finaleventidset);
   
     List<EventRelation> er1 = [select relationid from EventRelation where eventid in:e.keyset()];
      Map<Id, EventRelation> er2 = new Map<Id, EventRelation>(er1);
      List<contact> c = [select id,Activity_Score__c from Contact where id in:er2.keyset()];
      
     
     List<AggregateResult> agr = [select count(id) T1, relationid from EventRelation where relationid in : er2.keyset() group by relationid];
     
         List<contact> ctct1 = new List<contact>();
   
 for(contact ct : c){
       for(AggregateResult ar : agr){
           if (ct.id == (Id)ar.get('expr1')){
            if(ct.Activity_Score__c!= (Integer)ar.get('T1')){
             ct.Activity_Score__c = (Integer)ar.get('T1');
             ctct1.add(ct);
   }}}}
            update ctct1; 
  }
}


We have used the following query in trigger to get the list of relationid's of type  contact
select relationid from eventrelation where eventid in : e and Relation.Type = 'Contact'

But When the trigger is passing the list of relation id's it is coming to the class as set of id's starts with '0RE'
Usually the contact id's will start with '003'

Could you please advise us the correct way to sent the list of contactid's from EventRelation from trigger to class

Thanks In Advance