You need to sign in to do that
Don't have an account?
Nagma Khan
create a trigger on contact object when the value insert then go to mail to the use to solve with trigger ?
hi please any one help me
i am createing a trigger on the contact object and when the insert the data then how to go to mail to the user just like example
trigger triggerName on Contact(before insert,before update){
list<contact> listContact=new list<contact>();
for(contact cont:trigger.new){
cont.lastName='test one ';
cont.firstName='testTWO';
listContact.add(cont);
}
insert listContact;
}
pelase support now how to send a email with the trigger ?
i am createing a trigger on the contact object and when the insert the data then how to go to mail to the user just like example
trigger triggerName on Contact(before insert,before update){
list<contact> listContact=new list<contact>();
for(contact cont:trigger.new){
cont.lastName='test one ';
cont.firstName='testTWO';
listContact.add(cont);
}
insert listContact;
}
pelase support now how to send a email with the trigger ?
trigger SendEmailToAccount on Contact (after insert,after update)
{
if(Trigger.isAfter)
{
if(Trigger.isInsert )
{
//helper class for single email but bulk messages
HelperContactTrigger.sendEmail(trigger.new);
}
}
if(trigger.isAfter && trigger.isUpdate )
{
HelperContactTrigger.sendEmailafter(trigger.new);
}
}
**********************************Class**************************************************
public with sharing class HelperContactTrigger {
public static List<Contact> sendEmail(List<Contact>Contacts)
{
//query on template object
EmailTemplate et=[Select id from EmailTemplate where name=:'Sales: New Customer Email'];
//list of emails
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
for(Contact con : Contacts)
{
//check for Account
if(con.AccountId != null && con.Email != null){
//initiallize messaging method
Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
//set object Id
singleMail.setTargetObjectId(con.Id);
//set template Id
singleMail.setTemplateId(et.Id);
//flag to false to stop inserting activity history
singleMail.setSaveAsActivity(false);
//add mail
emails.add(singleMail);
}
}
//send mail
Messaging.sendEmail(emails);
return Contacts;
}
public static List<Contact> sendEmailafter(List<Contact>Contacts)
{
//query on template object
EmailTemplate et=[Select id from EmailTemplate where name=:'Sales: New Customer Email'];
//list of emails
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
for(Contact con : Contacts)
{
//check for Account
if(con.AccountId != null && con.Email != null){
//initiallize messaging method
Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
//set object Id
singleMail.setTargetObjectId(con.Id);
//set template Id
singleMail.setTemplateId(et.Id);
//flag to false to stop inserting activity history
singleMail.setSaveAsActivity(false);
//add mail
emails.add(singleMail);
}
}
//send mail
Messaging.sendEmail(emails);
return Contacts;
}
}
You should try this code :
************Apex Class**********
*********Apex Trigger******** Hope it will help you.
Thanks
Ajay
trigger ChromaTest on Account (after insert,after update) {
For(Account acc: trigger.new){
clsEmailSend();
}
public void clsEmailSend(){
Messaging.SingleEmailMessage mail= new Messaging.SingleEmailMessage();
string [] toAdd= new string[]{'xxx@gmail.com', 'yyyy@gmail.com' };
mail.setToAddresses(toAdd);
mail.setSubject('This is for test');
mail.setPlainTextbody('Testing.....');
Messaging.Sendemail(new Messaging.SingleEmailMessage[] {mail});
}
}