Salesforce Custom Button on Click Opens Model/Dialogue box and perform DML Operation - V1

 
{!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="Create Person Account"><p id="dlgbody"></p><b>First Name :</b> <input id="fName"></input><br></br><b>Last Name :</b> <input id="LName"></input><br></br><b>Phone :</b> <input id="phone"></input><br></br><b>Email :</b> <input id="email"></input></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: 280, 
open: function(event, ui){ 
$g('.ui-dialog').css('z-index',103); 
$g('.ui-widget-overlay').css('z-index',102); 
}, 
buttons: { 
"Save": function() { 
if($g("#LName").val()!=''){ 
if($g("#phone").val()!=''){ 
var acc = new sforce.SObject("Account"); 
acc.lastname = $g("#LName").val(); 
acc.firstname = $g("#fName").val(); 
acc.PersonMobilePhone =$g("#phone").val(); 
acc.PersonEmail=$g("#email").val(); 
acc.recordTypeId ='xxxxxxxx'; 
var result = sforce.connection.create([acc]); 
if(result[0].getBoolean("success")){ 
alert('Person account has been created -'+result[0].id); 
var casRes = sforce.connection.query("Select PersonContactId From Account where Id=\'"+result[0].id+"\'"); 
var casRecs = casRes.getArray("records"); 
var cas = new sforce.SObject("Case"); 
cas.id = '{!Case.Id}'; 
cas.contactId = casRecs[0].PersonContactId; 
var caseres = sforce.connection.update([cas]); 
if(caseres[0].getBoolean("success")){ 
alert('Case is updated with Person account'); 
window.location.reload(); 
}else { 
alert('Error : '+caseres); 

} 
$g(this).dialog("close"); 
} else { 
alert('Error : '+result); 

} 
}else { 
alert('Phone number is mandatory'); 
} 
}else { 
alert('Last name is mandatory'); 
} 

} 
} 
}); 
});
  



Comments