+ Start a Discussion
Oksana DovbakOksana Dovbak 

Trigger to Count RelationId on Event

Hello,

I am looking to create a custom field that will update after event insert to count the number of RelationId's on each event. Help please!
<Saket><Saket>
Hi Oksana

I m not getting that what u mean by relation Id's here please elaborate ur problem so that we can help u in a more better way?

Thanks 
Saket
Oksana DovbakOksana Dovbak
By relationId I mean any related Contacts (WhoId).
Waqar Hussain SFWaqar Hussain SF
You can run a Report for "Events with Invitees" that will show you the number of people at an Event. 
<Saket><Saket>
Hi Oksana 
What I understand is u want to make a field on Event and want to store number of contact associated with that particular event ?
If Yes -> Event is a child and Contact is a parent so maximum of 1 contact will be there for each event so I think it is not making any sense :) still u want to count this then u can make a formula field for it no need to wirte a trigger :D
if no -> Please make me clear the requirement :P
Oksana DovbakOksana Dovbak
Yes, I am aware of that report, but I would like a field on the actual event to reference the number of invitees to then do some rollups on account and opportunity objects on number of mutli-invitee events.
Oksana DovbakOksana Dovbak
Saket, there is a setting under Activity Settings that allows you to relate multiple contacts to tasks and events which we have enabled in our org. We have large events with multiple contacts on occassion.
<Saket><Saket>
Hi Oksana,

I m very Sorry I was unknown of this that this can be done :), I learned from u thanks

Oksana after doing some digging I wrote a trigger on Event pls find it below 
trigger EventCountInvitee on Event (before insert, before update) {
	
	Map<Id, Integer> allCount = new Map<id,Integer>();
    
    for(AggregateResult ar : [SELECT count(Id) cou, EventId FROM EventRelation WHERE EventId in :Trigger.newMap.keySet() AND isParent = true AND isWhat = false Group by EventId]){
        
        allCount.put(Id.valueOf(ar.get('EventId')+''), Integer.valueOf(ar.get('cou')));
        
    }
    
    for(Event eve : trigger.new){
        
        if(eve.WhoId != null && allCount.containsKey(eve.Id))
        {
            
            eve.CountOfInvitees__c = allCount.get(eve.Id);
            
        }
        
    }
    
}

Actually I should suppose to write this on EventRelation but Salesforce dosen't allow for this so I am hoping that u will add the invities from the Salesforce Event UI for ex see Image below 


User-added image


Please update me that this solution works for u or not ?

If u r seeking any problem or question then plss let me know?

Thanks
Saket Sharma
sharmasaket703@gmail.com
<Saket><Saket>
Hi Oksana

Please Let us know whether your problem is solved or not ?

Thanks 
Saket Sharma
<Saket><Saket>
Hi Oksana

Please Let us know whether your problem is solved or not?

Thanks 
Saket Sharma
Oksana DovbakOksana Dovbak
Hi Saket,

When I added this trigger to my sandbox and tried creating an event with 3 invitees, I ended up getting this error - 

Apex trigger EventCountInvitee caused an unexpected exception, contact your administrator: EventCountInvitee: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: ()