You need to sign in to do that
Don't have an account?
Ashish Biswas
Sample login screen
Hi,
I am trying to build a login screen with VF, it has 2 text boxes and a command button. The value entered in th text boxes are setting the value of userid and password properties in my controll class. I have a method define in control class that will return true/false based on the credential. Below is the VF page
<apex:page controller="apxLoginCheck" showHeader="false" sidebar="false">
<apex:form id="frmLogin">
<apex:pageBlock title="Enter User details" id="pbLogin">
<apex:pageBlockSection id="pbc">
<apex:inputText id="txtID" value="{!UID}"/>
<apex:inputText id="txtPWD" value="{!PWD}"/>
<apex:commandButton id="btnOk" value="Click to Login" action="{!UserDetails}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
Below is the Controller
public class apxLoginCheck {
public String UID{get; set;}
public string PWD{get; set;}
public apxLoginCheck(){
}
public boolean UserDetails(){
string newID ='ashish.biswas';
string newPWD = 'ayushi@23';
System.debug('B4 conditional check');
if(UID==newID && PWD==newPWD){
return true;
System.debug('Method has been called');
}
return false;
}
}
Can anyone please guide me, how shall I catch the boolean return value in VF on click of command button and call a JS function accordingly.
Thanks in advance
I am trying to build a login screen with VF, it has 2 text boxes and a command button. The value entered in th text boxes are setting the value of userid and password properties in my controll class. I have a method define in control class that will return true/false based on the credential. Below is the VF page
<apex:page controller="apxLoginCheck" showHeader="false" sidebar="false">
<apex:form id="frmLogin">
<apex:pageBlock title="Enter User details" id="pbLogin">
<apex:pageBlockSection id="pbc">
<apex:inputText id="txtID" value="{!UID}"/>
<apex:inputText id="txtPWD" value="{!PWD}"/>
<apex:commandButton id="btnOk" value="Click to Login" action="{!UserDetails}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
Below is the Controller
public class apxLoginCheck {
public String UID{get; set;}
public string PWD{get; set;}
public apxLoginCheck(){
}
public boolean UserDetails(){
string newID ='ashish.biswas';
string newPWD = 'ayushi@23';
System.debug('B4 conditional check');
if(UID==newID && PWD==newPWD){
return true;
System.debug('Method has been called');
}
return false;
}
}
Can anyone please guide me, how shall I catch the boolean return value in VF on click of command button and call a JS function accordingly.
Thanks in advance
Please use below code to make it work perfect
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
All Answers
For this req, you can simply create a get set booelan varaibel in your controller and simply assign the tru false for that in your method and you can use anytime that variable page side to do whatever you want based on value..
//define one variable
public boolean isValidCredentials {get;set;}
//initalize to false by defalut
isValidCredentials = false//in constructor
public void UserDetails(){
string newID ='ashish.biswas';
string newPWD = 'ayushi@23';
System.debug('B4 conditional check');
if(UID==newID && PWD==newPWD){
isValidCredentials =true;
System.debug('Method has been called');
}
isValidCredentials = false;
}
simply use above method foir assign the valuse and then that variable you can use anywhere
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
I have tried to do the way you say but can not access from VF
VF Code
<apex:page controller="apxLoginCheck" showHeader="false" sidebar="false">
<apex:form id="frmLogin">
<apex:pageBlock title="Enter User details" id="pbLogin">
<apex:pageBlockSection id="pbc">
<apex:inputText id="txtID" value="{!UID}"/>
<apex:inputText id="txtPWD" value="{!PWD}"/>
<apex:actionFunction action="{!UserDetails}" name="UserDetail"/>
<apex:commandButton id="btnOk" value="Click to Login" onclick="{UserDetail()}" oncomplete="ValidUser()"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
<script>
function ValidUser(){
var bool = {!isValidCredential};
if(bool){
alert("Welcome to SALESFORCE");
else
alert("Invalide User details");
}
}
</script>
</apex:page>
Controller Code
public class apxLoginCheck {
public String UID{get; set;}
public string PWD{get; set;}
public boolean isValidCredential{get; set;}
public apxLoginCheck(){
isValidCredential = false;
}
public PageReference UserDetails(){
string newID ='ashish.biswas';
string newPWD = 'ayushi@23';
System.debug('B4 conditional check');
if(UID==newID && PWD==newPWD){
isValidCredential = true;
System.debug('Method has been called');
}
return null;
}
}
public class apxLoginCheck {
public String UID{get; set;}
public string PWD{get; set;}
public boolean isValidCredential{get; set;}
public apxLoginCheck(){
isValidCredential = false;
}
public void UserDetails(){
string newID ='ashish.biswas';
string newPWD = 'ayushi@23';
System.debug('B4 conditional check');
if(UID==newID && PWD==newPWD){
isValidCredential = true;
System.debug('Method has been called');
}
}
}
what error you are getting?
create a dummy poanel on vf page and rerender that from command button
<apex:outputpanel id="dummy"> </apex:outputpanel>
<apex:commandButton rerender="dummy" id="btnOk" value="Click to Login" onclick="{UserDetail()}" oncomplete="ValidUser()"/>
if we dont use rerender then it gives the error sometime..pleae check with this and let me check your code if is there any thing which you missed..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
I saw your code..check if you are getting that debug in that method...
also..no need of action function, you can directly call that method from command button from actiuon method
<apex:commandButton id="btnOk" value="Click to Login" action="{UserDetail}" oncomplete="ValidUser()" rerender="dummy"/>
you can simply repalce ypour command button with this...create a panel as I said and rerender that..
Please check with this and let me know if it solves your issue..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
Please use below code to make it work perfect
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
This is not working.
Do one thing..check the value of that variable on oncomplete
<apex:commandButton rerender="dummy" id="btnOk" value="Click to Login" action="{!UserDetails}" oncomplete="alert('{!isValidCredential}')"/>
check the above alert and Did you check the debug if control i sgoing to the userDetails method or not ??
Thanks
Sandeep
<apex:page controller="apxLoginCheck" showHeader="false" sidebar="false">
<apex:form id="frmLogin">
<apex:pageBlock title="Enter User details" id="pbLogin">
<apex:pageBlockSection id="pbc">
<apex:inputText id="txtID" value="{!UID}"/>
<apex:inputText id="txtPWD" value="{!PWD}"/>
<apex:commandButton rerender="dummy" id="btnOk" value="Click to Login" action="{!UserDetails}" oncomplete="Show()"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:outputpanel id="dummy">
</apex:outputpanel>
</apex:form>
<script type="text/javascript">
function Show(){
var bool = {!isValidCredential};
if(bool){
alert("Redirecting to GOOGLE");
window.location = "https://www.google.co.in";
}
else {
alert("Not a valide User");
}
}
</script>
</apex:page>
Put the javascript in that outputpanel.....because as you are getting proper value in oncomplete alert but not in javscript..
because we have not refereshed the script...
so when we do rerender it will automatically take the latest value..
put the script in that outputpanel adn check
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
<apex:commandButton rerender="dummy" id="btnOk" value="Click to Login" action="{!UserDetails}" oncomplete="Show()"/>
Working -
<apex:commandButton rerender="dummy" id="btnOk" value="Click to Login" action="{!UserDetails}" oncomplete="alert('{!isValidCredential}')"/>
Script -
<script type="text/javascript">
function Show(){
var bool = '{!isValidCredential}';
if(bool){
alert("Redirecting to GOOGLE");
window.location = "https://www.google.co.in";
}
else {
alert("Not a valide User");
}
}
</script>
it is simple..what you need to do is..you are already getting the correct value on oncomplete so pass that value as paramtere in show method like below
no need to chck there
<apex:commandButton rerender="dummy" id="btnOk" value="Click to Login" action="{!UserDetails}" oncomplete="
Show('{!isValidCredential}');"/>
<script type="text/javascript">
function Show(var bool){
if(bool){
alert("Redirecting to GOOGLE");
window.location = "https://www.google.co.in";
}
else {
alert("Not a valide User");
}
}
</script>
Then no need to calculate the value..you will simplye directly get the true false and you are passing that to show method and there it will work..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
Yes because after going to the controller when we are coming to the page..we have not refereshed the section so there it was containning the old values..after rerender they will get the latest one and work perfect..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
I would appriciate, if you can help me with one more doubt -
1. How can I send parameter to a controller function from VF page button click.
Thanks in Advance
Ashish
You can use action function with <Apex:param> to pass the value from VF page to controller..
Thanks
Sandeep
Hi Ashish,
Please refer below link fo more details
https://success.salesforce.com/ideaView?id=08730000000YcV8AAK
You can simply use apex:param to pass...
Thanks,
Sandeep
You can utilize the 'oncomplete' attribute of the command button to call a JavaScript function based on the boolean return value. Here's a modification of your VF page code:
htmlCopy code
<apex:commandButton id="btnOk" value="Click to Login" action="{!UserDetails}" oncomplete="handleLoginResult({!UserDetails})"/> <script> function handleLoginResult(loginSuccessful) { if (loginSuccessful) { // Do something for successful login alert('Login successful!'); } else { // Do something for unsuccessful login alert('Login failed. Incorrect credentials.'); } } </script>
In this https://golfshoesforflatfeet.com/ example, the 'oncomplete' attribute calls a JavaScript function 'handleLoginResult' and passes the boolean result of the 'UserDetails' action. Depending on whether it's true or false, you can perform actions accordingly in the JavaScript function.