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
Bharat SBharat S 

Can anyone please help me write logic for trigger to populate title on event whenever event is updated. I have code written to populate title when event is created which is working perfectly.

I am attaching the trigger and triggerhandler class below. Please help creating trigger handler method when event is updated.

Event Trigger:-
 
trigger EventTrigger on Event (before insert, before update, after insert, after update) {
 
    if(Trigger.IsAfter)
    {
        if (Trigger.isInsert) {
            EventTriggerHandler.AfterInsert(trigger.newMap);
        }
        
        if (Trigger.isUpdate) {
            EventTriggerHandler.AfterInsert(trigger.newMap);
        }
    }
    
}

EventTriggerHandler:-

public class EventTriggerHandler {
    
    public static String comma = ',' ;
 public static void AfterInsert(Map<id, Event> EventMap)
    {
        try
        {
            String NameTitle ='';
            String Names;
            
            List<EventRelation>EveRelationList=New List<EventRelation>();
            EveRelationList = [SELECT Id, RelationId,EventId FROM EventRelation WHERE EventId In : eventMap.keyset() and RelationId != null];
            system.debug('AfterInsert count_____'+EveRelationList.size());
            Set<Id> WhoIds = New set<Id>();
            for(EventRelation eveRel : EveRelationList){
                WhoIds.add(eveRel.RelationId);
                system.debug('WhoIds after insert_____'+WhoIds);
            }
            List<Contact> ConList = New List<Contact>();
            ConList=[Select Id,Title,FirstName,LastName, Name FROM Contact WHERE Id In : WhoIds and Title != null and Name != null];
            for(Contact c : ConList){
                if(c.Title!= null)
                {
                    NameTitle = NameTitle+c.Name + '(' + c.Title + ')' + comma ;
                }
            }
            Names = NameTitle.removeEnd(comma);
            System.debug('Names'  + Names);
            List<Event> eventList = new List<Event>();
            
            for (Event e : [select id, Title__c from Event where Id in: eventMap.keyset()])
            {
                e.Title__c = Names;
                eventList.add(e);
            }
            
             if(eventList.size()>0){
                update eventList;
        }
        }
        catch(exception e)
        {
            throw e;    
        }
    }      
Bharat SBharat S
Can anyone help in this asap.
Ajay K DubediAjay K Dubedi
Hi Bharat,

Try the following code, I hope it may be helpful for you:
public class EventTriggerHandler {
    
    public static void AfterInsert(List<Event> EventList){
        if(EventList.size()>0){
        try
        {
            String NameTitle ='';
            String Names='';
            Set<Id> WhoIds = New set<Id>();
            Set<Id> evntIds = New set<Id>();
            List<EventRelation>EveRelationList=New List<EventRelation>();
            List<Event> eventListNew = new List<Event>();
            List<Contact> ConList = New List<Contact>();
            
            for(Event eventI:EventList){
                evntIds.add(eventI.Id);
            }
            EveRelationList = [SELECT Id, RelationId,EventId FROM EventRelation WHERE EventId In : evntIds and RelationId != null];
            if(EveRelationList.size()>0){
            
            for(EventRelation eveRel : EveRelationList){
                WhoIds.add(eveRel.RelationId);
                system.debug('WhoIds after insert_____'+WhoIds);
            }
            }
            
            ConList=[Select Id,Title,FirstName,LastName, Name FROM Contact WHERE Id In : WhoIds and Title != null and Name != null];
            
            if(ConList.size()>0){
            for(Contact c : ConList){
                if(c.Title!= null)
                {
                    NameTitle = NameTitle+c.Name + '(' + c.Title + ')' + ',' ;
                }
            }
            }
            
            if(NameTitle!=null){
                Names = NameTitle.removeEnd(',');
                }
            
            for (Event e : [select id, Title__c from Event where Id in:evntIds])
            {    if(Names!=null){
                e.Title__c = Names;
                eventListNew.add(e);
                }
            }
            
             if(eventListNew.size()>0){
                update eventListNew;
                }
        }
        catch(exception e){
            System.debug('InLineNo->'+e.getLineNumber() + 'Due to following->' +
                         e.getMessage());   
        }
    }      
    }
}
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
Bharat SBharat S
Hi Ajay my AfterInsert code is working fine, I needed help in AfterUpdate. Anyways thanks for replying.
Ajay K DubediAjay K Dubedi
Hi Bharat,

There is no problem in your code and I hope it also work for after update but your code is not fully bulkified, so I add some null checks.Try the following code and add debug-logs to track the problem and make ensure you will be update Event as per the code requirement:
public class EventTriggerHandler {
    
 public static void AfterInsert(Map<Id, Event> EventMap){    
        if(EventMap.size()>0){
        try
        {
            String comma = ',' ;
            String NameTitle ='';
            String Names='';
            List<EventRelation>EveRelationList=New List<EventRelation>();
            Set<Id> WhoIds = New set<Id>();
            List<Contact> ConList = New List<Contact>();
            List<Event> eventList = new List<Event>();
            
            EveRelationList = [SELECT Id, RelationId,EventId FROM EventRelation WHERE EventId In : eventMap.keyset() and RelationId != null];
            if(EveRelationList.size()>0){
            for(EventRelation eveRel : EveRelationList){
                if(eveRel.RelationId!=null){
                WhoIds.add(eveRel.RelationId);
                }
            }
            }
            
            if(WhoIds.size()>0){
            ConList=[Select Id,Title,FirstName,LastName, Name FROM Contact WHERE Id In : WhoIds and Title != null and Name != null];
            if(ConList.size()>0){
            for(Contact c : ConList){
                if(c.Title!= null)
                {
                    NameTitle = NameTitle+c.Name + '(' + c.Title + ')' + comma ;
                }
            }
            }
            }
            if(NameTitle!=null){
            Names = NameTitle.removeEnd(comma);
            }
            
            for (Event e : [select Id, Title__c from Event where Id In: eventMap.keyset()]){
                if(Names!=null){
                e.Title__c = Names;
                eventList.add(e);
                }
            }
            
             if(eventList.size()>0){
                update eventList;
                }
        }
        catch(exception e){
            System.debug('InLineNo->'+e.getLineNumber() + 'Due to following->' +
                         e.getMessage());       
        }
        }
    }
}
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi