You need to sign in to do that
Don't have an account?
satakshi
How to validate Show Selected Button on vf page?
Hello,
I have created custom button to choose city and then to select records according to city. I want to validate "show selected" button so that it will show alert if we do not select any records and try to move further. plz see my code
Controller:
public with sharing class MyCompController3
{
public id tid{get;set;}
public String str;
public String city{get;set;}
public MyCompController3(ApexPages.StandardController controller)
{
}
public pagereference dosome(){
wrapAccountList = new List<wrapAccount>();
for(Account a: [select Id, RecordType.Name, Name, City__c from Account where City__c =:city ORDER BY RecordType.Name DESC])
{
wrapAccountList.add(new wrapAccount(a));
system.debug('*****************wrapAccountList******************'+wrapAccountList);
}
return null;
}
public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('--None--','--None--'));
options.add(new SelectOption('Ahmednagar','Ahmednagar'));
options.add(new SelectOption('Akola','Akola'));
options.add(new SelectOption('Amravati','Amravati'));
options.add(new SelectOption('Aurangabad','Aurangabad'));
options.add(new SelectOption('Bhiwandi','Bhiwandi'));
options.add(new SelectOption('Chandrapur','Chandrapur'));
options.add(new SelectOption('Chinchwad','Chinchwad'));
options.add(new SelectOption('Dhule','Dhule'));
options.add(new SelectOption('Dombivali','Dombivali'));
options.add(new SelectOption('Jalgaon','Jalgaon'));
options.add(new SelectOption('Kalyan','Kalyan'));
options.add(new SelectOption('Kolhapur','Kolhapur'));
options.add(new SelectOption('Latur','Latur'));
options.add(new SelectOption('Malegaon','Malegaon'));
options.add(new SelectOption('Mumbai','Mumbai'));
options.add(new SelectOption('Nagpur','Nagpur'));
options.add(new SelectOption('Nanded','Nanded'));
options.add(new SelectOption('Nashik','Nashik'));
options.add(new SelectOption('Navi Mumbai','Navi Mumbai'));
options.add(new SelectOption('Parbhani','Parbhani'));
options.add(new SelectOption('Pimpri','Pimpri'));
options.add(new SelectOption('Pune','Pune'));
options.add(new SelectOption('Sangli','Sangli'));
options.add(new SelectOption('Satara','Satara'));
options.add(new SelectOption('Solapur','Solapur'));
options.add(new SelectOption('Thane','Thane'));
options.add(new SelectOption('Vasai','Vasai'));
options.add(new SelectOption('Virar','Virar'));
return options;
}
public List<wrapAccount> wrapAccountList {get; set;}
public List<Account> selectedAccount{get;set;}
private List<Id> accountids=new list<Id>();
public List<Account> acc;
public PageReference processSelected()
{
selectedAccount = new List<Account>();
for(wrapAccount wrapAccountObj : wrapAccountList)
{
if(wrapAccountObj.selected == true)
{
selectedAccount.add(wrapAccountObj.acc);
//accWrap.acc.Name
system.debug('*****************!!!!!!!!!wrapAccountList******************'+selectedAccount);
}
}
PageReference pageRef = new PageReference('/apex/Visitplan');
return pageRef;
}
public list<Account> srecs;
public list<Account> getSrecs()
{
srecs= [select Id, Name, RecordType.Name, City__c from Account];
system.debug('*****************!!!!!!!!!srecssrecssrecssrecs******************'+srecs);
tid = ApexPages.currentPage().getParameters().get('id');
system.debug('*****************!!!!!!!!!$$$$$$$$$$$$$$$$$$$$$$$$$******************'+tid);
return selectedAccount;
}
public PageReference CreateRec()
{
for(Account acr :selectedAccount)
{
Planned_Visit__c pv =new Planned_Visit__c();
pv.Daily_Visit_Plan__c= tid;
pv.Name__c=acr.id;
insert pv;
ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.CONFIRM,'Successfully Inserted Visit.'));
}
return null;
}
public class wrapAccount {
public Account acc {get; set;}
public Boolean selected {get; set;}
public wrapAccount(Account a)
{
acc = a;
selected = false;
}
}
}
VF PAGE:
<apex:page standardController="Daily_Visit_Plan__c" extensions="MyCompController3" standardStylesheets="false" sidebar="false">
<style type="text/css">
.myButton {
-moz-box-shadow: 3px -3px 5px -2px #899599;
-webkit-box-shadow: 3px -3px 5px -2px #899599;
box-shadow: 3px -3px 5px -2px #899599;
background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #ededed), color-stop(1, #bab1ba));
background:-moz-linear-gradient(top, #ededed 5%, #bab1ba 100%);
background:-webkit-linear-gradient(top, #ededed 5%, #bab1ba 100%);
background:-o-linear-gradient(top, #ededed 5%, #bab1ba 100%);
background:-ms-linear-gradient(top, #ededed 5%, #bab1ba 100%);
background:linear-gradient(to bottom, #ededed 5%, #bab1ba 100%);
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ededed', endColorstr='#bab1ba',GradientType=0);
background-color:#ededed !Important;
-moz-border-radius:24px !Important;
-webkit-border-radius:24px !Important;
border-radius:24px !Important;
border:2px solid #d6bcd6 !Important;
display:inline-block !Important;
cursor:pointer !Important;
color:#3a8a9e !Important;
font-family:Arial !Important;
font-size:18px !Important;
font-weight:bold !Important;
padding:2px 36px !Important;
text-decoration:none !Important;
text-shadow:0px 0px 0px #e1e2ed !Important;
}
.myButton:hover {
background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #bab1ba), color-stop(1, #ededed));
background:-moz-linear-gradient(top, #bab1ba 5%, #ededed 100%);
background:-webkit-linear-gradient(top, #bab1ba 5%, #ededed 100%);
background:-o-linear-gradient(top, #bab1ba 5%, #ededed 100%);
background:-ms-linear-gradient(top, #bab1ba 5%, #ededed 100%);
background:linear-gradient(to bottom, #bab1ba 5%, #ededed 100%);
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#bab1ba', endColorstr='#ededed',GradientType=0);
background-color:#bab1ba;
}
.myButton:active {
position:relative;
top:1px;
}
</style>
<script type="text/javascript">
function selectAllCheckboxes(obj,receivedInputID)
{
var inputCheckBox = document.getElementsByTagName("input"); for(var i=0; i<inputCheckBox.length; i++)
{
if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1)
{
inputCheckBox[i].checked = obj.checked;
} } }
</script>
<apex:form >
<center>
<apex:commandButton styleClass="myButton" value="Show Selected" action="{!processSelected}" />
<br />
<br />
<br />
<apex:actionRegion >
<h1> Choose City </h1>
<apex:selectList style="float: center; width: 250px; height: 40px; margin: 10px; background-color:#ededed;
border-radius:24px !Important;
border:2px solid #d6bcd6 !Important;
display:inline-block !Important;
cursor:pointer !Important;
color:#3a8a9e !Important;
font-family:Arial !Important;
font-size:18px !Important;
font-weight:bold !Important;
padding:2px 36px !Important;
text-decoration:none !Important;
text-shadow:0px 0px 0px #e1e2ed !Important; cursor:pointer" value="{!city}" size="1">
<apex:selectOptions value="{!items}"/>
<apex:actionSupport event="onchange" action="{!dosome}" reRender="table" />
</apex:selectList>
</apex:actionRegion>
<br />
<br />
<br />
<apex:pageBlock >
<apex:pageblockSection title="All Doctors And Chemists" collapsible="false" columns="1">
<apex:pageBlockTable value="{!wrapAccountList}" var="accWrap" title="All Doctors And Chemists" id="table">
<apex:column >
<apex:facet name="header">
<apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
</apex:facet>
<apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
</apex:column>
<apex:column value="{!accWrap.acc.Name}" />
<apex:column value="{!accWrap.acc.City__c}" />
<apex:column value="{!accWrap.acc.RecordType.Name}" />
</apex:pageBlockTable>
</apex:pageblockSection>
</apex:pageBlock>
<br />
<br />
<br />
<apex:commandButton styleClass="myButton" value="Show Selected" action="{!processSelected}" />
</center>
</apex:form>
</apex:page>
I have created custom button to choose city and then to select records according to city. I want to validate "show selected" button so that it will show alert if we do not select any records and try to move further. plz see my code
Controller:
public with sharing class MyCompController3
{
public id tid{get;set;}
public String str;
public String city{get;set;}
public MyCompController3(ApexPages.StandardController controller)
{
}
public pagereference dosome(){
wrapAccountList = new List<wrapAccount>();
for(Account a: [select Id, RecordType.Name, Name, City__c from Account where City__c =:city ORDER BY RecordType.Name DESC])
{
wrapAccountList.add(new wrapAccount(a));
system.debug('*****************wrapAccountList******************'+wrapAccountList);
}
return null;
}
public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('--None--','--None--'));
options.add(new SelectOption('Ahmednagar','Ahmednagar'));
options.add(new SelectOption('Akola','Akola'));
options.add(new SelectOption('Amravati','Amravati'));
options.add(new SelectOption('Aurangabad','Aurangabad'));
options.add(new SelectOption('Bhiwandi','Bhiwandi'));
options.add(new SelectOption('Chandrapur','Chandrapur'));
options.add(new SelectOption('Chinchwad','Chinchwad'));
options.add(new SelectOption('Dhule','Dhule'));
options.add(new SelectOption('Dombivali','Dombivali'));
options.add(new SelectOption('Jalgaon','Jalgaon'));
options.add(new SelectOption('Kalyan','Kalyan'));
options.add(new SelectOption('Kolhapur','Kolhapur'));
options.add(new SelectOption('Latur','Latur'));
options.add(new SelectOption('Malegaon','Malegaon'));
options.add(new SelectOption('Mumbai','Mumbai'));
options.add(new SelectOption('Nagpur','Nagpur'));
options.add(new SelectOption('Nanded','Nanded'));
options.add(new SelectOption('Nashik','Nashik'));
options.add(new SelectOption('Navi Mumbai','Navi Mumbai'));
options.add(new SelectOption('Parbhani','Parbhani'));
options.add(new SelectOption('Pimpri','Pimpri'));
options.add(new SelectOption('Pune','Pune'));
options.add(new SelectOption('Sangli','Sangli'));
options.add(new SelectOption('Satara','Satara'));
options.add(new SelectOption('Solapur','Solapur'));
options.add(new SelectOption('Thane','Thane'));
options.add(new SelectOption('Vasai','Vasai'));
options.add(new SelectOption('Virar','Virar'));
return options;
}
public List<wrapAccount> wrapAccountList {get; set;}
public List<Account> selectedAccount{get;set;}
private List<Id> accountids=new list<Id>();
public List<Account> acc;
public PageReference processSelected()
{
selectedAccount = new List<Account>();
for(wrapAccount wrapAccountObj : wrapAccountList)
{
if(wrapAccountObj.selected == true)
{
selectedAccount.add(wrapAccountObj.acc);
//accWrap.acc.Name
system.debug('*****************!!!!!!!!!wrapAccountList******************'+selectedAccount);
}
}
PageReference pageRef = new PageReference('/apex/Visitplan');
return pageRef;
}
public list<Account> srecs;
public list<Account> getSrecs()
{
srecs= [select Id, Name, RecordType.Name, City__c from Account];
system.debug('*****************!!!!!!!!!srecssrecssrecssrecs******************'+srecs);
tid = ApexPages.currentPage().getParameters().get('id');
system.debug('*****************!!!!!!!!!$$$$$$$$$$$$$$$$$$$$$$$$$******************'+tid);
return selectedAccount;
}
public PageReference CreateRec()
{
for(Account acr :selectedAccount)
{
Planned_Visit__c pv =new Planned_Visit__c();
pv.Daily_Visit_Plan__c= tid;
pv.Name__c=acr.id;
insert pv;
ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.CONFIRM,'Successfully Inserted Visit.'));
}
return null;
}
public class wrapAccount {
public Account acc {get; set;}
public Boolean selected {get; set;}
public wrapAccount(Account a)
{
acc = a;
selected = false;
}
}
}
VF PAGE:
<apex:page standardController="Daily_Visit_Plan__c" extensions="MyCompController3" standardStylesheets="false" sidebar="false">
<style type="text/css">
.myButton {
-moz-box-shadow: 3px -3px 5px -2px #899599;
-webkit-box-shadow: 3px -3px 5px -2px #899599;
box-shadow: 3px -3px 5px -2px #899599;
background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #ededed), color-stop(1, #bab1ba));
background:-moz-linear-gradient(top, #ededed 5%, #bab1ba 100%);
background:-webkit-linear-gradient(top, #ededed 5%, #bab1ba 100%);
background:-o-linear-gradient(top, #ededed 5%, #bab1ba 100%);
background:-ms-linear-gradient(top, #ededed 5%, #bab1ba 100%);
background:linear-gradient(to bottom, #ededed 5%, #bab1ba 100%);
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ededed', endColorstr='#bab1ba',GradientType=0);
background-color:#ededed !Important;
-moz-border-radius:24px !Important;
-webkit-border-radius:24px !Important;
border-radius:24px !Important;
border:2px solid #d6bcd6 !Important;
display:inline-block !Important;
cursor:pointer !Important;
color:#3a8a9e !Important;
font-family:Arial !Important;
font-size:18px !Important;
font-weight:bold !Important;
padding:2px 36px !Important;
text-decoration:none !Important;
text-shadow:0px 0px 0px #e1e2ed !Important;
}
.myButton:hover {
background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #bab1ba), color-stop(1, #ededed));
background:-moz-linear-gradient(top, #bab1ba 5%, #ededed 100%);
background:-webkit-linear-gradient(top, #bab1ba 5%, #ededed 100%);
background:-o-linear-gradient(top, #bab1ba 5%, #ededed 100%);
background:-ms-linear-gradient(top, #bab1ba 5%, #ededed 100%);
background:linear-gradient(to bottom, #bab1ba 5%, #ededed 100%);
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#bab1ba', endColorstr='#ededed',GradientType=0);
background-color:#bab1ba;
}
.myButton:active {
position:relative;
top:1px;
}
</style>
<script type="text/javascript">
function selectAllCheckboxes(obj,receivedInputID)
{
var inputCheckBox = document.getElementsByTagName("input"); for(var i=0; i<inputCheckBox.length; i++)
{
if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1)
{
inputCheckBox[i].checked = obj.checked;
} } }
</script>
<apex:form >
<center>
<apex:commandButton styleClass="myButton" value="Show Selected" action="{!processSelected}" />
<br />
<br />
<br />
<apex:actionRegion >
<h1> Choose City </h1>
<apex:selectList style="float: center; width: 250px; height: 40px; margin: 10px; background-color:#ededed;
border-radius:24px !Important;
border:2px solid #d6bcd6 !Important;
display:inline-block !Important;
cursor:pointer !Important;
color:#3a8a9e !Important;
font-family:Arial !Important;
font-size:18px !Important;
font-weight:bold !Important;
padding:2px 36px !Important;
text-decoration:none !Important;
text-shadow:0px 0px 0px #e1e2ed !Important; cursor:pointer" value="{!city}" size="1">
<apex:selectOptions value="{!items}"/>
<apex:actionSupport event="onchange" action="{!dosome}" reRender="table" />
</apex:selectList>
</apex:actionRegion>
<br />
<br />
<br />
<apex:pageBlock >
<apex:pageblockSection title="All Doctors And Chemists" collapsible="false" columns="1">
<apex:pageBlockTable value="{!wrapAccountList}" var="accWrap" title="All Doctors And Chemists" id="table">
<apex:column >
<apex:facet name="header">
<apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
</apex:facet>
<apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
</apex:column>
<apex:column value="{!accWrap.acc.Name}" />
<apex:column value="{!accWrap.acc.City__c}" />
<apex:column value="{!accWrap.acc.RecordType.Name}" />
</apex:pageBlockTable>
</apex:pageblockSection>
</apex:pageBlock>
<br />
<br />
<br />
<apex:commandButton styleClass="myButton" value="Show Selected" action="{!processSelected}" />
</center>
</apex:form>
</apex:page>
Use try catch block and add <apex:Messages/> tag
All Answers
try this:
in vf :
use <apex:pageMessages /> after opening the form tag <apex:form>
Thanks
Ankur Saini
http://mirketa.com
Use try catch block and add <apex:Messages/> tag
Best Regards,
Satakshi