Monday, April 23, 2012

Salesforce: Custom Button with Javascript

Salesforce custom button with the javascript is a very exciting feature to implement. We received a requirement from our customer for converting Leads. User should convert Lead record only if the status is changed to "Qualified". Typically when a lead is converted, Salesforce automatically changes the lead status to Qualified. But there are some custom validation rules created for lead record which doesn't allow to complete the conversion process and display error message as validation rules on lead page are not fulfilled.

I created a custom "Convert" button with the help of Javascript and replaced with standard button on lead page layout. The javascript validates whether lead status is qualified or not and doesn't allow user to proceed if it is not qualified.

Code:
var status = "{!Lead.Status}";
if (status == "Qualified")
{
window.location = 'https://ap1.salesforce.com/lead/leadconvert.jsp?retURL=%2F{!Lead.Id}&id={!Lead.Id}';
}
else
{
alert('Please change the Lead Status to Qualified then click on convert');
}


Error Message: