You need to sign in to do that
Don't have an account?

Sending a mail using apex trigger
hello i am writting a trigger to send mail. i am having two objects one is custom and one is contact. on custom object there is lookup relationship with contact. means from custom object we can choose contact. contact will be having email as a mandatory field. when i will choose contact from custom object(Sent feedback) using lookup field then mail will be sent to email address of that contact. can you help me with this?? this is my code. i am not getting mail.
trigger SentFbtrigger on Sent_Feedback__c (after insert) {
//Create a master list to hold the emails we'll send
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for(Sent_Feedback__c s : Trigger.new) {
if(s.Contact__r.Email != null && s.Contact__r.Name != null) {
//Create a new Email
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//Set list of people who should get the email
List<String> sendTo = new List<String>();
sendTo.add(s.Contact__r.Email);
mail.setToAddresses(sendTo);
mail.setTargetObjectId(s.Contact__r.Id);
//Set who the email is sent from
mail.setReplyTo('james12@ror.com');
mail.setSenderDisplayName('James R');
// Set email contents - you can use variables!
mail.setSubject('Subject Content');
String body = 'Dear ' + s.Contact__r.FirstName + ', ';
body += 'Email Body';
mail.setHtmlBody(body);
//Add your email to the master list
mails.add(mail);
}
}
// Send all emails in the master list
Messaging.sendEmail(mails);
}
trigger SentFbtrigger on Sent_Feedback__c (after insert) {
//Create a master list to hold the emails we'll send
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for(Sent_Feedback__c s : Trigger.new) {
if(s.Contact__r.Email != null && s.Contact__r.Name != null) {
//Create a new Email
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//Set list of people who should get the email
List<String> sendTo = new List<String>();
sendTo.add(s.Contact__r.Email);
mail.setToAddresses(sendTo);
mail.setTargetObjectId(s.Contact__r.Id);
//Set who the email is sent from
mail.setReplyTo('james12@ror.com');
mail.setSenderDisplayName('James R');
// Set email contents - you can use variables!
mail.setSubject('Subject Content');
String body = 'Dear ' + s.Contact__r.FirstName + ', ';
body += 'Email Body';
mail.setHtmlBody(body);
//Add your email to the master list
mails.add(mail);
}
}
// Send all emails in the master list
Messaging.sendEmail(mails);
}
trigger SendEmail on Sent_Feedback__c (after insert) {
set<id> ContactId = new set<id>();
// Step 0: Create a master list to hold the emails we'll send
List<Messaging.SingleEmailMessage> mails =
new List<Messaging.SingleEmailMessage>();
for (Sent_Feedback__c myContact : Trigger.new) {
if (myContact.Contact__c != null )
{
ContactId.add(myContact.Contact__c);
}
Contact con = [Select firstname,lastname,email,id,name,MobilePhone from Contact where id in :ContactId];
// Step 1: Create a new Email
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// Step 2: Set list of people who should get the email
List<String> sendTo = new List<String>();
sendTo.add(con.email);
mail.setToAddresses(sendTo);
// Step 3: Set who the email is sent from
mail.setReplyTo('utkarsha.up10@gmail.com');
mail.setSenderDisplayName('Utz patil');
// (Optional) Set list of people who should be CC'ed
List<String> ccTo = new List<String>();
ccTo.add('puja.patil@aress.com');
mail.setCcAddresses(ccTo);
// Step 4. Set email contents - you can use variables!
mail.setSubject('URGENT BUSINESS PROPOSAL');
String body = 'Dear ' + con.FirstName;
mail.setHtmlBody(body);
// Step 5. Add your email to the master list
mails.add(mail);
Messaging.sendEmail(mails);
}
}
// Step 6: Send all emails in the master list
All Answers
for this ,Take the logic outside the trigger and write it on an apex class. Now call this class from trigger. Checkout trailhead for asynchronous apex module
Then enable email security compliance
trigger SendEmail on Sent_Feedback__c (after insert) {
set<id> ContactId = new set<id>();
// Step 0: Create a master list to hold the emails we'll send
List<Messaging.SingleEmailMessage> mails =
new List<Messaging.SingleEmailMessage>();
for (Sent_Feedback__c myContact : Trigger.new) {
if (myContact.Contact__c != null )
{
ContactId.add(myContact.Contact__c);
}
Contact con = [Select firstname,lastname,email,id,name,MobilePhone from Contact where id in :ContactId];
// Step 1: Create a new Email
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// Step 2: Set list of people who should get the email
List<String> sendTo = new List<String>();
sendTo.add(con.email);
mail.setToAddresses(sendTo);
// Step 3: Set who the email is sent from
mail.setReplyTo('utkarsha.up10@gmail.com');
mail.setSenderDisplayName('Utz patil');
// (Optional) Set list of people who should be CC'ed
List<String> ccTo = new List<String>();
ccTo.add('puja.patil@aress.com');
mail.setCcAddresses(ccTo);
// Step 4. Set email contents - you can use variables!
mail.setSubject('URGENT BUSINESS PROPOSAL');
String body = 'Dear ' + con.FirstName;
mail.setHtmlBody(body);
// Step 5. Add your email to the master list
mails.add(mail);
Messaging.sendEmail(mails);
}
}
// Step 6: Send all emails in the master list
i want help from you guys to create trigger to send email
scenario is number of tickets available field is a formula field when ever the the number of tickets avialbale field is zero email should be sent to the event manager
i tried to do it but trigger not supporting the formula filed
if anyone have the solution please let me know