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
Santi Ram RaiSanti Ram Rai 

Trigger to send email to Manager when the new project is created

Hi every body,

I want to write a Trigger to send email to Manager, when the new project is created. So, how can i write it?
B TulasiB Tulasi
Hi Santi,

You can check this code.

Trigger :

trigger EmailSend on New_Leads__c (before insert) {

list<Messaging.SingleEmailMessage> mails=new list<Messaging.SingleEmailMessage>();

for(New_Leads__c newlead:trigger.new){

if (newlead.Email__c != null && newlead.Name!= null){

Messaging.SingleEmailMessage mail =new Messaging.SingleEmailMessage();

List<String> sendTo = new List<String>();

sendTo.add(newlead.Email__c);

mail.setToAddresses(sendTo);

mail.setReplyTo('thulasi.204@gmail.com');

mail.setSenderDisplayName('Official mail from Knightcapitalfunding');

List<String> ccTo = new List<String>();

ccTo.add('thulasi.204@gmail.com');

mail.setCcAddresses(ccTo);

mail.setSubject('URGENT BUSINESS PROPOSAL');
      String body = 'Dear ' + newlead.Name+ ', ';
      body += 'I confess this will come as a surprise to you.';
      body += 'I am John Alliston CEO of the Bank of Nigeria.';
      body += 'I write to request your cooperation in this ';
      body += 'urgent matter as I need a foreign partner ';
      body += 'in the assistance of transferring $47,110,000 ';
      body += 'to a US bank account. Please respond with ';
      body += 'your bank account # so I may deposit these funds.';
      mail.setHtmlBody(body);
      
      
      mails.add(mail);
      }
      }
      Messaging.sendEmail(mails);

}
Amit Chaudhary 8Amit Chaudhary 8
please check below post. I hope that will help you.
http://amitsalesforce.blogspot.in/2015/11/singleemailmessage-vs-massemailmessage.html

Sample code
public void SendEmail()
{
 List<contact> lstcon=[Select id from contact limit 2];
 List<Id> lstids= new List<Id>();
 for(Contact c:lstcon)
 {
  lstids.add(c.id);
 }
 EmailTemplate et=[Select id from EmailTemplate where name = 'EmailTemplatename' limit 1];
 
 Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
 mail.setTargetObjectIds(lstIds);
 mail.setSenderDisplayName('System Admin');
 mail.setTemplateId(et.id);
 Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
}



Please let us know if this will help you
 
Santi Ram RaiSanti Ram Rai
I am looking trigger.
 
Amit Chaudhary 8Amit Chaudhary 8
above code you can put inside the trigger also. Like below
trigger contactTrigger on Contact (After insert, after update) 
{

	 List<Id> lstids= new List<Id>();
	 for(Contact c:Trigger.new)
	 {
		lstids.add(c.id);
	 }
	 
	 if(lstids.size () > 0 )
	 {
		 EmailTemplate et=[Select id from EmailTemplate where name = 'EmailTemplatename' limit 1]; // please add valid email template  name
		 Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
		 mail.setTargetObjectIds(lstIds);
		 mail.setSenderDisplayName('System Admin');
		 mail.setTemplateId(et.id);
		 Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
	 }
}
Please let us know if this will help you

Thanks
AMit Chaudhary


 
Santi Ram RaiSanti Ram Rai
I got this error message "Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger SendEmailToManager caused an unexpected exception, contact your administrator: SendEmailToManager: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.SendEmailToManager: line 10, column 1"
 
Amit Chaudhary 8Amit Chaudhary 8
EmailTemplate et=[Select id from EmailTemplate where name = 'EmailTemplatename' limit 1]; // please add valid email template  name
Santi Ram RaiSanti Ram Rai
Yes, this email template "EmailTemplatename" is the valid one. 
Amit Chaudhary 8Amit Chaudhary 8
Can you please post your full code and error msg