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
MaggieSumitMaggieSumit 

I want to create a button on Lead object, when i clicked the button an email goes to the mail id which mentioned there??

Best Answer chosen by MaggieSumit
Siddharth ManiSiddharth Mani
Create a new custom button on the Lead Page Layout selecting the option as "Execute Javascript" and paste the below code:
{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")} 
var result = sforce.apex.execute("SendEmailToLead","sendEmail",{leadId:"{! 
Lead.Id}"}); 
alert(result); 
window.location.reload()

The controller class would be:
global class SendEmailToLead {
	
  webservice static String sendEmail(String leadId) {

      List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
      for (Lead myLead : [SELECT Id, FirstName, Email FROM Lead WHERE Id = :leadId]) {
	    if (myLead.Email != null && myLead.FirstName != null) {

    		Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    
      List<String> sendTo = new List<String>();
      sendTo.add(myLead.Email);
      mail.setToAddresses(sendTo);
    
      mail.setReplyTo('abc@def.com');
      mail.setSenderDisplayName('DoNotReply');
    
      mail.setSubject('Test Email');
      String body = 'Dear ' + myLead.FirstName + ', ';
      body += 'This is a Test Email';
      mail.setHtmlBody(body);
    
      mails.add(mail);
    }
  }
  Messaging.sendEmail(mails);
	 return 'Email sent successfully';
    }
}

Let me know if it works for you!
 

All Answers

Siddharth ManiSiddharth Mani
Create a new custom button on the Lead Page Layout selecting the option as "Execute Javascript" and paste the below code:
{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")} 
var result = sforce.apex.execute("SendEmailToLead","sendEmail",{leadId:"{! 
Lead.Id}"}); 
alert(result); 
window.location.reload()

The controller class would be:
global class SendEmailToLead {
	
  webservice static String sendEmail(String leadId) {

      List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
      for (Lead myLead : [SELECT Id, FirstName, Email FROM Lead WHERE Id = :leadId]) {
	    if (myLead.Email != null && myLead.FirstName != null) {

    		Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    
      List<String> sendTo = new List<String>();
      sendTo.add(myLead.Email);
      mail.setToAddresses(sendTo);
    
      mail.setReplyTo('abc@def.com');
      mail.setSenderDisplayName('DoNotReply');
    
      mail.setSubject('Test Email');
      String body = 'Dear ' + myLead.FirstName + ', ';
      body += 'This is a Test Email';
      mail.setHtmlBody(body);
    
      mails.add(mail);
    }
  }
  Messaging.sendEmail(mails);
	 return 'Email sent successfully';
    }
}

Let me know if it works for you!
 
This was selected as the best answer
MaggieSumitMaggieSumit
Thanku Sir
 
MaggieSumitMaggieSumit
Sir i need one more help, can i send a feedback form to mail.
Siddharth ManiSiddharth Mani
Sorry, I dont know how to do that. Maybe someone in the forum will help you out!