+ Start a Discussion
Terri HenryTerri Henry 

Adding Validation to Custom Lead Button

Hi All!

I currently have a button on the 'Lead' page, that opens a VF page with a Flow inside:

VF Page:
<apex:page standardController="Lead"> <h1>Create A Premier Quick Task</h1> <flow:interview name="QuickPremierTask" finishLocation="/{!id}"> <apex:param name="LeadID" value="{!ID}"/> <apex:param name="UserID" value="{!$User.Id}"/> </flow:interview> </apex:page>

I want to add Validation in the button code that will only allow the button to be clicked (to launch the VF page flow) if the fields 'Lead Source' or 'Initial Lead Source' are equal to the text 'Premier 1'

Normally I would simlpy exclude the button from the page layout with the use of a new Record Type, however this is not an option in this instance.

Can this be done using Javascript in the button code? (Or an alternate option)

Winning answer gets 10 internets!
Best Answer chosen by Terri Henry
Krishna SambarajuKrishna Sambaraju
You can do this using javascript in the button code. Set the behavior of the button as Execute Javascript and add the following code. Change the field names and parameters of your visualforce page accordingly.
{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")} 

var leadSource = "{!Lead.LeadSource}";
var initialLeadSource = "{!Lead.InitialLeadSource}";

if (leadSource == 'Premier 1' || initialLeadSource = 'Premier 1')
{
	window.location.href = '/apex/your_page_goes_here?id={!Lead.Id}';
}
else
{
	alert('Available only for Premier 1 Leads.');
}
Hope this helps.
 

All Answers

Krishna SambarajuKrishna Sambaraju
You can do this using javascript in the button code. Set the behavior of the button as Execute Javascript and add the following code. Change the field names and parameters of your visualforce page accordingly.
{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")} 

var leadSource = "{!Lead.LeadSource}";
var initialLeadSource = "{!Lead.InitialLeadSource}";

if (leadSource == 'Premier 1' || initialLeadSource = 'Premier 1')
{
	window.location.href = '/apex/your_page_goes_here?id={!Lead.Id}';
}
else
{
	alert('Available only for Premier 1 Leads.');
}
Hope this helps.
 
This was selected as the best answer
William TranWilliam Tran
Yes, use run as javascript:

Use the code below as a template, it will need some tweaking
{!REQUIRESCRIPT("/soap/ajax/34.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/34.0/apex.js")}

if("{!LeadSource}" == "Premier 1"){
location.replace('/APEX/YOURPAGE.....');
}
else{
    alert("The Lead Source has to be Premier 1!");
}
As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

Thanks
 
Terri HenryTerri Henry
Many thanks Krishna