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
akkkakkk 

how to send a email when status is approved ?

Hi all ,

i have code
trigger sampletrigger on object(after insert,after update)
{
List<object2>obj2list  = new list<object2>();
for(object1 obj1 : trigger.new)
{
​​​​​If(obj1.status =='approved')
{
Object2 obj2 = new object2();
Obj2.FirstNsme  = obj1.firstname;
Obj2.lastnsme = obj1.lastname;
obj2.city = obj1.city;
obj2.status = obj1.status;
Obj2list.add(obj2);
}
}
Database.insert(obj2list,false);
}
the question is that how to send a email with Template if the Status is Approved ?
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi akhan,
You can use workflow rule instead of trigger to send an email alert based on the based on certain criteria.
Create workflow rule with following formula
ISPICKVAL(status,'Approved')

And create an email alert with required template.

Please refer below link which might help you in this
https://help.salesforce.com/articleView?id=customize_wf.htm&type=5 (https://help.salesforce.com/articleView?id=customize_wf.htm&type=5)
https://success.salesforce.com/answers?id=9063A000000sx0AQAQ

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
akkkakkk
Not using workflow only used the Trigger
SarvaniSarvani
Hi,

As you have the above trigger code you can call apex class from the same code by passing the id as parameter. In the apex class use SingleEmailMessage to send email using email template.
 
trigger sampletrigger on object(after insert,after update)
{
List<object2>obj2list  = new list<object2>();
for(object1 obj1 : trigger.new)
{
​​​​​If(obj1.status =='approved')
{
Object2 obj2 = new object2();
Obj2.FirstNsme  = obj1.firstname;
Obj2.lastnsme = obj1.lastname;
obj2.city = obj1.city;
obj2.status = obj1.status;
Obj2list.add(obj2);
}
ApexclasstoSendEmail(obj1.id);  //Call apex class if you are trying to send email to the record's whose status is approved.
}
Database.insert(obj2list,false);
}

Try refering this code for sending email using Email temaplate
 
public static void ApexclasstoSendEmail(String Id){

Obj1__c actualrecord=[select id,ownerid,email__c from Obj1__c where id=:id];
   Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
   // set other attributes to send email 
  EmailTemplate emailTemplate = [Select Id,Subject,Description,HtmlValue,DeveloperName,Body from EmailTemplate where name = ‘Test Template’];
  message.setTemplateID(emailTemplate.Id); 
  Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message};
  Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
if (results[0].success) 
 {
   System.debug(‘The email was sent successfully.’);
 } else {
   System.debug(‘The email failed to send: ‘ +  results[0].errors[0].message);
 }
}

References:
https://medium.com/@tekie.touch/sending-emails-with-template-using-apex-in-salesforce-1f343b0c532c
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm

Hope this helps! Please mark as best if it does.

Thanks
SarvaniSarvani
Hi,

I missed syntax would be Classname.methodname(); in trigger. You can try bulkifying code once it works.
 
trigger sampletrigger on object(after insert,after update)
{
List<object2>obj2list  = new list<object2>();
for(object1 obj1 : trigger.new)
{
​​​​​If(obj1.status =='approved')
{

}
ApexClasstoSendEmail.MethodSendingEmail(obj1.id);  //Call apex class if you are trying to send email to the record's whose status is approved.
}
}

Apex class would be:
public class ApexclasstoSendEmail{
public static void MethodSendingEmail(String Id){
Obj1__c actualrecord=[select id,ownerid,email__c from Obj1__c where id=:id];
// Singleemailmessage code goes here
 }
}
}
Thanks