Salesforce Custom Button on Click invoke apex class using JavaScript - V2

Build ->Customize->Buttons, Links, and Actions -> New button or link

Choose Behavior - Execute Javascript,Content Source - OnClick JavaScript

 

   
{!REQUIRESCRIPT("/soap/ajax/35.0/connection.js")}  
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}  
{!REQUIRESCRIPT('//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js')}  
{!REQUIRESCRIPT('//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js')}  
var html = '<div id="dialog" style="display: none" title="Please Confirm"><p id="dlgbody"></p><b>Are you sure you want to send an email notification?</div> ';  

var $g = jQuery.noConflict();  

$g(function() {  
$g('head').append('<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" type="text/css"/>'); 
$g('body').append(html);  
$g("#dialog").dialog({  
autoOpen: true,  
modal: true,  
width: 500,  
height: 150,  
open: function(event, ui){  
$g('.ui-dialog').css('z-index',103);  
$g('.ui-widget-overlay').css('z-index',102);  
},  
buttons: { "Cancel": function() { 

$g(this).dialog("close"); },  
"OK": function() {  

if ('{!Lead.Preferred_Dealer_Account__c}' == '') { 
alert('Preferred Dealer value is required'); 
$g(this).dialog("close"); 

else if('{!Lead.Your_Date_of_Appointment_1__c}'== ''){ 
alert('Your Date of Appointment 1 value is required'); 
$g(this).dialog("close"); 
}else{ 
var successs = sforce.apex.execute("LeadAgentNotifyDealers","sendMailToDealers",{leadId:"{!Lead.Id}",ownerEmai:"{!Lead.OwnerEmail}"});  
if(successs == 'SUCCESS'){  
alert("Mail has been sent successfully !!");  
$g(this).dialog("close");  
window.location.reload(); 
} else { 
alert("Failure !! - Please contact Your System Administrator");  
$g(this).dialog("close"); 


}  
}  

});  
});

Apex Class:

 

   global class LeadAgentNotifyDealers {

   webservice static string sendMailToDealers(ID leadId,String ownerEmai){
         //Do your logic here.....
    }

  }

Comments