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
Sivasankari MuthuSivasankari Muthu 

Email template for leads fields

Hi, 
how to create email template for lead fields 
I tried ,
Step 1:
User-added image
Step 2:
User-added image

Please tell me ,If anybody Know
Best Answer chosen by Sivasankari Muthu
Maharajan CMaharajan C
Hi,

Steps for Visualforce Template

NewEmailTemplate->VisualforceTemplate->Enter the Details(Recipient Type -USER , Related To Type -None)->Edit Template ->Copy and paste the Above Code->Send and Test verify merge fields->Choose User(Recipient Type) , Chose Lead Record(Related To Type)->Send Email to View->Ok


Use the Above Steps to get the Proper Output.


Thanks,

Thanks,
Raj
(Sweet Potato Tec)  
 

All Answers

Maharajan CMaharajan C
Hi Siva,

It's not possible because Lead,Contact&Users are comes under the Recipient Records,

In the Recipient rceord you can declare the Lead Records so that  you can retrieve the Lead merge Fields.

There is a no need to declare the Lead Records in Related To.

Use the Below Link to Refer-:
https://help.salesforce.com/apex/HTViewSolution?id=000175913&language=en_US

Let us know if there any further doubts you have?

If the answer satisfy your requirement pls choose as best Answer.


Thanks,
Raj
(Sweet Potato Tec)  
Maharajan CMaharajan C
Hi siva,

I think this also gives a Some Help to you.

Instead of using the Text  type Email type use the Visualforce Type Email Template. 
 
You can do this using a visualforce email template: when creating the template, make the "Related To Type" = "Lead". Then you can pull in any of the Lead fields {For Custom Fields by using their API names in this format: {!RelatedTo.FieldAPIName__c}}

For example:
<messaging:emailTemplate subject="this is the subject" recipientType="User" relatedToType="Lead">
<messaging:plaintextEmailBody >

Full Name            : {!RelatedTo.LastName}
Annual Revenue:  {!RelatedTo.AnnualRevenue}
Lead Owner         : {!RelatedTo.Owner}


</messaging:plainTextEmailBody>
</messaging:emailTemplate>
Maharajan CMaharajan C
Hi,

Steps for Visualforce Template

NewEmailTemplate->VisualforceTemplate->Enter the Details(Recipient Type -USER , Related To Type -None)->Edit Template ->Copy and paste the Above Code->Send and Test verify merge fields->Choose User(Recipient Type) , Chose Lead Record(Related To Type)->Send Email to View->Ok


Use the Above Steps to get the Proper Output.


Thanks,

Thanks,
Raj
(Sweet Potato Tec)  
 
This was selected as the best answer
Sivasankari MuthuSivasankari Muthu
Hi Maharaj,
Thank you for your reply,
Its awesome,working

Regards,
M. sivasankari
mac adminmac admin
Hi Maharaj,
I follwed the steps which you mentioned in the previous. I written a trigger to sent an email to the user, i added the emial template in the trigger which i have creeated  but ehen an email is sent through trigger i was unable to show the lead field values in that mail. Can you help me over here.

Thanks in Advance.

Regards,
Mac.
Maharajan CMaharajan C
Hi,

Copy your trigger and template please !!!

Thanks,
Raj
(Sweet Potato Tec)
mac adminmac admin
Trigger
rigger postallead on Lead (after insert) {

    Map<String,String> mapZipForEmail = new Map<String,String>();
    //Postal code from Lead object and Zip code from Zip_code__c
    for(Postal_Code__c thisZipCode : [Select id,ZipCode__c,Counselors__c from Postal_Code__c ]){
        mapZipForEmail.put(thisZipCode.ZipCode__c, thisZipCode.Counselors__c);
    }
        EmailTemplate templateId = [Select id from EmailTemplate where name ='lead notification template'];
 
  //  Create a master list to hold the emails
  List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();
  
  for (Lead myLead : Trigger.new) {
    if(mapZipForEmail.containsKey(myLead.PostalCode__c)){
          Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();

      //  list of people who should get the email
          List<String> sendTo = new List<String>();
          
          sendTo.add(mapZipForEmail.get(myLead.PostalCode__c));
          mail.setToAddresses(sendTo);
        
          // Set email is sent from
          mail.setReplyTo('test@gmail.com');
          mail.setSenderDisplayName('New Notification');
          mail.setSaveAsActivity(false);
        
            mail.setTargetObjectId(UserInfo.getUserId());
            
          // Set email contents
          mail.setTemplateID(templateId.Id);//This is the template we are setting
       
        
          // Add your email to the master list
          mails.add(mail);
      }
    
  }
  // Send all emails in the master list
  if(mails.size()>0)    Messaging.sendEmail(mails);
}
Email Template
<messaging:emailTemplate subject="Regarding Student" recipientType="User" relatedToType="Lead">
<messaging:plainTextEmailBody >
Congratulations!
This is your new Visualforce Email Template.
Full Name            : {!RelatedTo.LastName}
Mobile           : {!RelatedTo.MobilePhone}
Email           : {!RelatedTo.Email}
</messaging:plainTextEmailBody>
</messaging:emailTemplate>


 
Maharajan CMaharajan C
Hi,

In trigger this line instead of using this  EmailTemplate templateId = [Select id from EmailTemplate where name ='lead notification template'];

Try

  EmailTemplate templateId = [Select id from EmailTemplate where Id='18 Digit Id of email template' ];

Thanks,
Raj
(Sweet Potato Tec)

 
mac adminmac admin
Hi Maharaja,
Still i'm not getting the field values in the mail. Can  you help me here...
mac adminmac admin
My  email template id is 15 digit.
Maharajan CMaharajan C
Convert it to 18 digit id and use in this line.

EmailTemplate templateId = [Select id from EmailTemplate where Id='18 Digit Id of email template' ];
Maharajan CMaharajan C
Hi Mac,

What is your requiremant why are you writing trigger?

Can you please explain ? 

Thanks,
Raj
(Sweet Potato Tec)
Maharajan CMaharajan C
Hi Mac,

Add a one more line after set target object id in your trigger.

mail.setwhatId(Lead.id);

Then try again and let me know what happens!!! 

Thanks,
Raj
(Sweet Potato Tec)
mac adminmac admin
Hi Maharaja,
Getting error as
method does not exist or incorrect signature: [Messaging.SingleEmailMessage].setwhatId(Schema.SObjectField)
Maharajan CMaharajan C
Hi,

use the mail.setTargetObjectId(leadid);
mac adminmac admin
Getting error asVariable does not exist: leadid at line 30 column 36.
Maharajan CMaharajan C
Hi Mac,

Try the below codes in your trigger.

After the 1st line insert the below one:
 Set<Id> LeadIds = new Set<ID>();   

Replace your mail sections by the below one:
List<Messaging.SingleEmailMessage> mails =new List<Messaging.SingleEmailMessage>();   
    for(Lead ld : [select id, Status,Lead_age__c, owner.email from Lead where id IN : LeadIds])
   if(mapZipForEmail.containsKey(myLead.PostalCode__c))
{
 
 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();// For Email
                             
      List<String> sendTo = new List<String>();
      sendTo.add(ld.Owner.Email);//sending message via Email to the Owner of the lead
      mail.setToAddresses(sendTo);
      mail.setReplyTo('test@gmail.com');
      mail.setSenderDisplayName('New Notification');
      mail.saveAsActivity = false;
      mail.setTemplateId(templateId.Id);
      mail.setTargetObjectId(ld.OwnerId);    
      mail.setWhatId(ld.id);
      mails.add(mail);
      Messaging.sendEmail(mails);
  
}

let me know what happens!!! 

Thanks,
Raj
(Sweet Potato Tec)
mac adminmac admin
Variable does not exist: myLead.PostalCode__c at line 14 column 34
mac adminmac admin
Changed the Trigger as follow
trigger postallead on Lead (after insert) {
Set<Id> LeadIds = new Set<ID>();   

    Map<String,String> mapZipForEmail = new Map<String,String>();
    //Postal code from Lead object and Zip code from Zip_code__c
    for(Postal_Code__c thisZipCode : [Select id,ZipCode__c,Counselors__c from Postal_Code__c ]){
        mapZipForEmail.put(thisZipCode.ZipCode__c, thisZipCode.Counselors__c);
    }
       EmailTemplate templateId = [Select id from EmailTemplate where Id ='00X28000001JTuq'];
 
  //  Create a master list to hold the emails
  List<Messaging.SingleEmailMessage> mails =new List<Messaging.SingleEmailMessage>();   
    for(Lead ld : [select id, Status, PostalCode__c, owner.email from Lead where id IN : LeadIds])
   if(mapZipForEmail.containsKey(myLead.PostalCode__c))
{
 
 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();// For Email
                             
      List<String> sendTo = new List<String>();
      sendTo.add(ld.Owner.Email);//sending message via Email to the Owner of the lead
      mail.setToAddresses(sendTo);
      mail.setReplyTo('test@gmail.com');
      mail.setSenderDisplayName('New Notification');
      mail.saveAsActivity = false;
      mail.setTemplateId(templateId.Id);
      mail.setTargetObjectId(ld.OwnerId);    
      mail.setWhatId(ld.id);
      mails.add(mail);
      Messaging.sendEmail(mails);
  
}
  // Send all emails in the master list
  if(mails.size()>0)    Messaging.sendEmail(mails);
}

 
Maharajan CMaharajan C
Hi,

use ld.PostalCode__c insteadof myLead.PostalCode__c in the line 14
mac adminmac admin
Hi Maharaja,
I'm not getting any mail after creating a lead .
mac adminmac admin
I'm sending an email after creating the lead to the given postal code assigned user. But now i'm not geeting any emails to the particular zip code assigned users.
 
Maharajan CMaharajan C
Use the same line from your trigger in the new one mail.setTargetObjectId(UserInfo.getUserId());
mac adminmac admin
Even after change also i'm not getting email alerts.