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
Abdul RazzaqAbdul Razzaq 

Alert Message on detail page of Lead using apex.

I have a requirement. Where I created a custom email button, when Email button is clicked it sends email to the value present in the email field. And if the value is null it should give an error message. I accomplised one part of sending email, however could anybody help on the error message scenario. 
Best Answer chosen by Abdul Razzaq
Pankaj_GanwaniPankaj_Ganwani
Hi,

You can use onClick javascript type instead of vf page and do the following:

if('{!Lead.Email}'=='')
   alert('Please enter email');
else
 window.open('/apex/pagename?id={!Lead.Id}','_parent');

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi Abdul,

I think you have used onClick javascript on detail page button. You can simply use below mentioned snippet for showing error message:

if('{!Lead.Email}' == '')
{
    alert('Please fill email');
}
else
      //call method to send an email
Abdul RazzaqAbdul Razzaq

Hi Pankaj, :)
 

I used Vfpage pankaj.

Abdul RazzaqAbdul Razzaq
My controller

public class emailleadvf {

    public emailleadvf(ApexPages.StandardController controller) {

    }

public string callfunc{get;set;}
public lead leadmail{get;set;}

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

public pagereference actionlead(){

leadmail = [ Select id,Email from Lead where Id= :apexpages.currentpage().getparameters().get('id')];
system.debug('3333333333333333333333333333333333333333333333333333333333333333333333333'+leadmail.id);
system.debug('3333333333333333333333333333333333333333333333333333333333333333333333333'+leadmail.email);
Pagereference samepage = New Pagereference('/'+leadmail.id);
      
      if (leadmail.Email != null){
      
      Messaging.SingleEmailMessage mail = 
      new Messaging.SingleEmailMessage();
      
      List<String> sendTo = new List<String>();
      sendTo.add(leadmail.Email);
      mail.setToAddresses(sendTo);
      
      
      mail.setSubject('URGENT BUSINESS PROPOSAL');
      String body = 'Dear';
      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);
      
      }
      if(leadmail.Email==null || leadmail.Email==''){
      system.debug('cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc'+leadmail.email);
     callfunc='<script> func(); </script>';
      
      }
      
      
samepage.setRedirect(true);
return samepage;
}


}





Vf:-

<apex:page standardController="lead" extensions="emailleadvf" action="{!actionlead}">

<script>
  function func()
  {
  alert('function calling');
  }
  </script>

<apex:outputText value="{!callfunc}" escape="false"></apex:outputText>

</apex:page>
Pankaj_GanwaniPankaj_Ganwani
Hi,

Declare <apex:pageMessages/> in vf page below the <apex:page> tag.

In controller, you can do like this:

if(leademail.email == null)
{
    Apexpage.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error.'));
    return null;

}
Abdul RazzaqAbdul Razzaq
Pankaj,

Compile Error: Method does not exist or incorrect signature: Apexpage.addMessage(ApexPages.Message)

 
Pankaj_GanwaniPankaj_Ganwani
Hi,

Yes, Sure!

Please add me on fb:
https://www.facebook.com/pankaj.ganwani.9
Abdul RazzaqAbdul Razzaq
Thank you..
Pankaj_GanwaniPankaj_Ganwani
Hi,

You can use onClick javascript type instead of vf page and do the following:

if('{!Lead.Email}'=='')
   alert('Please enter email');
else
 window.open('/apex/pagename?id={!Lead.Id}','_parent');
This was selected as the best answer