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
Chandrika SuraneniChandrika Suraneni 

I want to send an email to patient using trigger when a patient object is created with email

I want to send an email to patient using trigger when a patient object is created with email.please send me the solution
Best Answer chosen by Chandrika Suraneni
CharuDuttCharuDutt
Hii Chandrika
Try Below Trigger
trigger SendEmailtoPatient on Patient__c (Afetr Insert) {
    set<String> lstConEmail=new set<String>();
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
	Set<Id> setPatientIds = new Set<Id>();
    List<String> sendTo = new List<String>();
         
		If(Trigger.IsAfter){
		if(Trigger.IsInsert){
        For(Patient__c p : trigger.new){
            If(p.Email__c != null){
              setPatientIds.Add(p.Id);
			  }
            }
        }
    }

        if(!setPatientIds.isEmpty()) {
            for(Patient__c p : [SELECT Id,Email__c FROM Patient__c  WHERE Id IN:setPatientIds]){
                if(string.IsNotBlank(p.Email__c)){
                    lstConEmail.Add(p.Email__c);
                }
            }
        }
    sendTo.addAll(lstConEmail);

        if(!sendTo.isEmpty()){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSenderDisplayName('Email Alert');

            mail.setSubject('Patient Admitted');
            String body = 'Test Subject';
            mail.setToAddresses(sendTo);
            mail.setHtmlBody(body);
            mails.add(mail);
            try{
                Messaging.SendEmail(mails);
            }
            catch(Exception e){
                System.debug('-----Exception------' +e);        
            }
        }
}
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Chandrika
Try Below Trigger
trigger SendEmailtoPatient on Patient__c (Afetr Insert) {
    set<String> lstConEmail=new set<String>();
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
	Set<Id> setPatientIds = new Set<Id>();
    List<String> sendTo = new List<String>();
         
		If(Trigger.IsAfter){
		if(Trigger.IsInsert){
        For(Patient__c p : trigger.new){
            If(p.Email__c != null){
              setPatientIds.Add(p.Id);
			  }
            }
        }
    }

        if(!setPatientIds.isEmpty()) {
            for(Patient__c p : [SELECT Id,Email__c FROM Patient__c  WHERE Id IN:setPatientIds]){
                if(string.IsNotBlank(p.Email__c)){
                    lstConEmail.Add(p.Email__c);
                }
            }
        }
    sendTo.addAll(lstConEmail);

        if(!sendTo.isEmpty()){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSenderDisplayName('Email Alert');

            mail.setSubject('Patient Admitted');
            String body = 'Test Subject';
            mail.setToAddresses(sendTo);
            mail.setHtmlBody(body);
            mails.add(mail);
            try{
                Messaging.SendEmail(mails);
            }
            catch(Exception e){
                System.debug('-----Exception------' +e);        
            }
        }
}
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
Chandrika SuraneniChandrika Suraneni
Thank you.
CharuDuttCharuDutt
Hii Chandrika
Please Close your Query By Marking It As Best Answer  If It Helps So It Also Helps Others In Future