You need to sign in to do that
Don't have an account?
Vivek Zanje 5
Email alert to parent using trigger
I want to send an email alert Physician Whenever a Physician-Contact Record is created. Physican-Contact object is having lookup relation ship with Physician. When I am creating a Physician-Contact Record Physician is required. An Email alert should be sent to that particular physician email which is available in Physician object.
I've gone through your requirement, Hope below code will help you:--->
Apex trigger:-->
trigger SendEmailToParent on Contact (after insert)
{
if(Trigger.isAfter && Trigger.isInsert)
{
SendEmailToParentClass.sendingEmail(Trigger.New);
}
}
Apex Class :-->
public class SendEmailToParentClass
{
public static void sendingEmail(List<Contact> allContact)
{
try
{
Set<Id> allPhyscianId=new Set<Id>();
for(Contact opp:allContact)
{
allPhyscianId.add(opp.Physician__c);
}
List<Physician __c> allPhyscians=new List<Physician __c>([select id,EmailField__c from Physician __c where Id IN:allPhyscianId]);
for(Physician __c physcian:allPhyscians)
{
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
string Parentemail = '';
string accName = '';
string emailbody = '';
accName = physcian.Name;
Parentemail = physcian.EmailField__c;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setSaveAsActivity(false);
mail.setSubject('Mail Subject');//Your Subject Of Email
mail.setHtmlBody('Your Email Body');//Your Email Body
mail.toAddresses = new String[] {Parentemail};
mail.setTreatTargetObjectAsRecipient(false);
emails.add(mail);
}
Messaging.SendEmailResult[] result =Messaging.sendEmail(emails);
}
catch(Exception e)
{
System.debug('Error in-->'+e.getLineNumber()+' and Error is='+e.getMessage());
}
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com