+ Start a Discussion
subodh chaturvedi 17subodh chaturvedi 17 

How to send an Email automatically when the event is created, an email should be sent to the “Assigned To” with a link to the record.

I have a requirement where I want to send an Email to "Assign To"  when an event is Created.

My event is created Automatically when My Parent Object(Support Request ) Record is approved with the standard Approval process.

I have written a trigger where I am able to create an Event automatically through trigger but at the same time, I want to send an Email to the owner of the Event with a link that event Record is created.

Below is my code :

Public class AutoCreateEvent
{

    Public static void createNewEvent(List<DealSupportRequest__c> dsreq, Map<Id, DealSupportRequest__c> oldDsreqMap)
    {
        
        List<Event> EventRec = new List<Event>();
      
        RecordType SuppReqRecordType = [SELECT Id
                                         FROM RecordType
                                         WHERE SobjectType = 'DealSupportRequest__c' AND DeveloperName = 'PSCUOnsiteSupport'
                                         LIMIT 1];

        for(DealSupportRequest__c ds:dsreq)
        {
            
            if((ds.Is_approved__c==true && ds.RecordtypeId==SuppReqRecordType.Id) && (ds.Is_approved__c != oldDsreqMap.get(ds.Id).Is_approved__c) )
            {
               Event e  = new Event();
                e.WhatId = ds.Account__c;
                e.Type   = 'On-Site at PSCU';
                e.Status__c = 'Scheduled';    
                e.OwnerId = ds.Ownerid;
                e.Subject_Custom__c =ds.Purpose__c; 
                e.Description = ds.OtherPurpose__c;
                e.StartDateTime =ds.StartDateTime__c;
                e.EndDateTime = ds.EndDateTime__c;
                e.LocationCallInInfo__c = ds.CampusLocation__c;
                e.Support_Request__c = ds.Id;
                EventRec.add(e);
                
          }
            
      }
      
        If(EventRec.size()>0)
        Insert EventRec;
        
    List<User> u =[select Id,Name,Email from User where ID=:UserInfo.getUserId()];
        
    List<Messaging.SingleEmailMessage> lst = new List<Messaging.SingleEmailMessage>();
    List<EmailTemplate> template = [select Id,Name,Subject,body from EmailTemplate where name = 'Event Created Confirmation After Support Request Approved'];       
     Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
     semail.setTemplateId(template[0].Id);
       List<String> sendTo = new List<String>();
      sendTo.add(EventRec.ownerId);
      semail.setToAddresses(sendTo);
 
     lst.add(semail);
  
      Messaging.sendEmail(lst); 
    
    }

}


 
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Subodh

Use workflow to send email on event creation and use customisable email template as well.

Cheers!!!