Showing posts with label AJAX. Show all posts
Showing posts with label AJAX. Show all posts

Monday, April 6, 2015

Convert Lead using Custom Button

To create a custom button for Lead Go Setup > Build > Customize > Lead > Buttons, Links, and Actions

Custom Button Code

{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")}

var r = confirm("Converting this Lead will create Account, Contact and Opportunity? Are you sure you would like to do this?");

if (r == true) {

   var leadToUpdate = new sforce.SObject("Lead");
   leadToUpdate.Id = "{!Lead.Id}";

   var result = sforce.connection.update([leadToUpdate]);
   
   var l = "{!Lead.Id}";
   var q = sforce.connection.query(
   "Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1");
   
   records = q.getArray("records");
   var record = records[0].MasterLabel;

   var convert = new sforce.LeadConvert();
   convert.leadId = l;
   convert.convertedStatus = record;
   
   result = sforce.connection.convertLead([convert]);

   if (result[0].getBoolean("success")) {
      var o = result[0].accountId;
      window.location.href="/"+o;
   } 
   else {
      alert("Sorry Unable to convert Lead that is in use by workflow.")
   }

}

Tuesday, May 21, 2013

Delete Custom Button in List Views

Salesforce.com does not provide the functionality to delete records in batch / bulk size in List Views. Like in Salesforce.com we have standard / out-of-the box buttons "Change Owner", "Accept" and "Change Status". Unfortunately we do not have "Delete Lead" button in List Views but you can add it in List Views by just writing a small code snippet in JavaScript.

Sample Code:



{!REQUIRESCRIPT("/soap/ajax/27.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/27.0/apex.js")}

//To show the alert pop up on the page
function log(message) {
alert(message);
}

//string for the URL of the current page
var url = parent.location.href;
//grabs the Lead records that the user is requesting to delete
var records = {!GETRECORDIDS($ObjectType.Lead)};
//if the button was clicked but there was no record selected
if (records[0] == null) {
//alert the user that they didn't make a selection
alert("Please select at least one record to update.");
} else {
//otherwise, there was a record selection
var delResult = sforce.connection.deleteIds(records);
if (delResult[0].getBoolean("success")) {
log(records.length+" record(s) have been deleted.");
}
else{
log("Failed to delete records.");
}
//refresh the page
parent.location.href = url;
}