+ Start a Discussion
Naveen DhanarajNaveen Dhanaraj 

write a class and trigger to send an email after creating a patient record and once appointment has been created it should send an email

I have patient and appointment  as an object  in  Hospital management system. if the appointment has been created it should send an email that your appoinment has been confirmed
Best Answer chosen by Naveen Dhanaraj
Naveen DhanarajNaveen Dhanaraj
public with sharing class Appointmenttriggerhandler{
    public static void sendemailtoPatient(list<Appointment__c> appointments){
        EmailTemplate et=[Select id from EmailTemplate where name='Appointment: New Customer Email'];
        list<Messaging.SingleEmailMessage> emails = new list<Messaging.SingleEmailMessage>();
        
        //To store patient Id
        
        set<Id> setPatientId = new set<Id>();
            if(appointments.size()> 0) {
                for(Appointment__c iterator : appointments) {
                    if(iterator.Patient__c != null) {
                        setPatientId.add(iterator.Patient__c);
                    }
                }
            }
            //fetch the patient records for whom an email needs to be sent
            
            if(setPatientId.size()>0) {
                list<Patient__c> Patients = [Select Id,Email__c from Patient__c where Id IN : setPatientId];
                for(Patient__c pat : Patients){
                    if(pat.Email__c != null){
                        Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
                        singleMail.toaddresses(pat.Email__c);
                        singleMail.setTemplateId(et.Id);
                        emails.add(singleMail);
                    }             
                }
            }
            Messaging.sendEmail(emails);
    }
}

All Answers

JyothsnaJyothsna (Salesforce Developers) 
Hi Naveen,

You can achieve this functionality by using workflow rule.

Step1. Create a workflow rule

User-added image

Step 2: Create Email Templates:

 setup---->Communication Templates--->Email Templates

User-added image

 

Step3: Add Workflow Action New Email Alert.
User-added image
Here Patient and Appointment objects are in lookup relationship.

Step4: Once you done all steps then click done and Activate Workflow rule.

Same way you can create a one more workflow for the Patient object and create an Email Alert.


Hope this helps you!
Best Regards,
Jyothsna
Naveen DhanarajNaveen Dhanaraj
Hii,Jyothsna
                My Requirement is to create an apex class and trigger to send an email.This should be done by coding part not admin part
JyothsnaJyothsna (Salesforce Developers) 
Hi Naveen,

Please check the below link similar to your requirement.

http://salesforce.stackexchange.com/questions/38947/sending-email-notification-using-trigger


Regards,
Jyothsna
Naveen DhanarajNaveen Dhanaraj
public with sharing class Appointmenttriggerhandler{
    public static void sendemailtoPatient(list<Appointment__c> appointments){
        EmailTemplate et=[Select id from EmailTemplate where name='Appointment: New Customer Email'];
        list<Messaging.SingleEmailMessage> emails = new list<Messaging.SingleEmailMessage>();
        
        //To store patient Id
        
        set<Id> setPatientId = new set<Id>();
            if(appointments.size()> 0) {
                for(Appointment__c iterator : appointments) {
                    if(iterator.Patient__c != null) {
                        setPatientId.add(iterator.Patient__c);
                    }
                }
            }
            //fetch the patient records for whom an email needs to be sent
            
            if(setPatientId.size()>0) {
                list<Patient__c> Patients = [Select Id,Email__c from Patient__c where Id IN : setPatientId];
                for(Patient__c pat : Patients){
                    if(pat.Email__c != null){
                        Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
                        singleMail.toaddresses(pat.Email__c);
                        singleMail.setTemplateId(et.Id);
                        emails.add(singleMail);
                    }             
                }
            }
            Messaging.sendEmail(emails);
    }
}
This was selected as the best answer