Event Notification from Salesforce Event Relation Change Event in Lightning

Event Notification from Salesforce Event Relation Change Event in Lightning

 1. Enable Change Data Capture for Event Relation.

2. Create an Organization-Wide-Email Address.

3. Create the below trigger.

rigger EventRelationChangeEventTrigger on EventRelationChangeEvent ( after insert ) {

    Map < Id, Set < Id > > mapEventIdAttendees = new Map < Id, Set < Id > >();
    List < Messaging.SingleEmailMessage > listMsgs = new List < Messaging.SingleEmailMessage > ();
    
    for ( EventRelationChangeEvent objERCE : trigger.new ) {  
    
        EventBus.ChangeEventHeader header = objERCE.ChangeEventHeader;
        system.debug( 'Change Type ' + header.changetype );
        
        if ( header.changetype == 'CREATE' ) {         
        
            if ( !mapEventIdAttendees.containsKey( objERCE.EventId ) )
                mapEventIdAttendees.put( objERCE.EventId, new Set < Id >() );
                
             mapEventIdAttendees.get( objERCE.EventId ).add( objERCE.RelationId );
         
         }
        
    }
    
    if ( mapEventIdAttendees.size() > 0 ) {
    
        for ( Event objEvent : [ SELECT Id, Subject, Description, StartDateTime, EndDateTime  FROM Event WHERE Id IN: mapEventIdAttendees.keySet() ] ) {
            
            for ( Id attendeeId : mapEventIdAttendees.get( objEvent.Id ) ) {
                
                Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
                String strBody = '<b>Event Details:</b><br/>';
                strBody += 'Start Date and Time: ' + objEvent.StartDateTime + ' GMT <br/>';
                strBody += 'End Date and Time: ' + objEvent.EndDateTime + ' GMT <br/>';
                strBody += 'Additional Information: ' + objEvent.Description;
                msg.setSubject( objEvent.Subject );
                msg.setHTMLBody( strBody );
                msg.setTargetObjectId( attendeeId );  
                msg.setSaveAsActivity( false );
                msg.setOrgWideEmailAddressId( '0D24x000000kAqp' );
                listMsgs.add( msg );  
                
            }
        
        }
        
        if ( listMsgs.size() > 0 )
            Messaging.sendEmail( listMsgs );
        
    }

}

Sample Email:

Leave a Reply