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
Mark Lewis 21Mark Lewis 21 

Generating Autoresponse from Template when Lead is Created

Hello guys

I have code that automatically generates leads from my email. Now what I need is something that can inform the sender that their query is being processed. Now I have created a tigger to send the response but it doesnt seem to work. Is there something I am missing?
 
trigger EmailToLeadResponseTrigger on Lead (after insert) {
	final String template = 'SalesLeadCreatedWebInquiries';
    Id templateId;
    
    try {
    templateId = [SELECT id FROM EmailTemplate WHERE Name = :template].id;
    } catch (QueryException e) {
    //...handle exception if no template is retrieved, or create condition to set email body in code
    }
    List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
    //Send a single mail to contact each created lead
    for (Lead l : [SELECT Name, Lead.Email FROM Lead WHERE Id in :Trigger.new]) {
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        
        message.setTemplateId(templateId);
        message.setTargetObjectId(l.Id);
        message.setWhatId(l.Id);
        message.setToAddresses(new String[] {l.Email});
        messages.add(message);
    }
    
}

 
Best Answer chosen by Mark Lewis 21
ManojjenaManojjena
Hi Mark ,

try with below code it will help !
 
trigger EmailToLeadResponseTrigger on Lead (after insert) {
    final String template = 'SalesLeadCreatedWebInquiries';
    Id templateId;
    try {
        templateId = [SELECT id FROM EmailTemplate WHERE Name = :template].id;
    } catch (QueryException exp) {
        System.debug(exp);
    }
    List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
    for (Lead led  :Trigger.new) {
       if(led.Email != null){
            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            message.setTemplateId(templateId);
            message.setTargetObjectId(led.Id);
            message.setToAddresses(new String[] {led.Email});
            emailList.add(message);
        }
     }
     Messaging.sendEmail(emailList);
}

If your email field is mandate in Lead then you can remove the condition to check null inside for loop .
Let me know if it helps .

Thanks
Manoj

 

All Answers

Sameer PrasonnSameer Prasonn
Hi Mark,

The entire code is ok, however you didn't write email send logic. Please try this.
trigger EmailToLeadResponseTrigger on Lead (after insert) {
	final String template = 'SalesLeadCreatedWebInquiries';
    Id templateId;
    
    try {
    templateId = [SELECT id FROM EmailTemplate WHERE Name = :template].id;
    } catch (QueryException e) {
    //...handle exception if no template is retrieved, or create condition to set email body in code
    }
    List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
    //Send a single mail to contact each created lead
    for (Lead l : [SELECT Name, Lead.Email FROM Lead WHERE Id in :Trigger.new]) {
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        
        message.setTemplateId(templateId);
        message.setTargetObjectId(l.Id);
        message.setWhatId(l.Id);
        message.setToAddresses(new String[] {l.Email});
        messages.add(message);
		//add email send logic and then mail goes.
		Messaging.sendEmail(new Messaging.SingleEmailMessage[] { messages });
    }
    
}
I've added the last line and it will works.

Please mark it as a best answer if it really help
 
Jayant JadhavJayant Jadhav
Hi Mark,

You can use out of box standard SFDC functionality for this i.e workflow rule. This will help you to send email alert messages to customer without writing any code and test classes. 
  1. Create workflow rule and select lead object.
  2. Evaluation Criteria = created
  3. Rule Criteria should be formula evaluation to true.(enter text value in box as true)
  4. Select action as email alert.
  5. choose template and email field.
  6. Save and activate the rule.

Plz refer link for more info: https://help.salesforce.com/HTViewHelpDoc?id=creating_workflow_rules.htm
ManojjenaManojjena
Hi Mark ,

try with below code it will help !
 
trigger EmailToLeadResponseTrigger on Lead (after insert) {
    final String template = 'SalesLeadCreatedWebInquiries';
    Id templateId;
    try {
        templateId = [SELECT id FROM EmailTemplate WHERE Name = :template].id;
    } catch (QueryException exp) {
        System.debug(exp);
    }
    List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
    for (Lead led  :Trigger.new) {
       if(led.Email != null){
            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            message.setTemplateId(templateId);
            message.setTargetObjectId(led.Id);
            message.setToAddresses(new String[] {led.Email});
            emailList.add(message);
        }
     }
     Messaging.sendEmail(emailList);
}

If your email field is mandate in Lead then you can remove the condition to check null inside for loop .
Let me know if it helps .

Thanks
Manoj

 
This was selected as the best answer
Mark Lewis 21Mark Lewis 21
Hi guys.

Thank you so much for the responses. Now I have the autoresponse going with this code
trigger EmailToLeadResponseTrigger on Lead (after insert) {
    //Query on template object
    EmailTemplate emailTemplate = [SELECT id FROM EmailTemplate WHERE name = :'Sales: Lead Created'];
    //List of emails
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    
    for (Lead lead : [SELECT Id, Lead.Email FROM Lead WHERE Id IN : Trigger.new]) {
        if (lead.Email != null) {
            //Initialize messaging method
            Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
            //Set object Id
            singleMail.setTargetObjectId(lead.Id);
            //Set template Id
            singleMail.setTemplateId(emailTemplate.Id);
            //Flag to false to stop inserting activity history
            singleMail.setSaveAsActivity(false);
            //Add mail
            emails.add(singleMail);
        }
    }
    //Send mail
    Messaging.sendEmail(emails);        
}
However, in my inbox it actually shows the user im using's email adress. Can I make this hidden or one of those "noreply@....com" emails where It just rejects replies to that given email?