function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ashish BiswasAshish 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
Best Answer chosen by Ashish Biswas
sandeep sankhlasandeep sankhla
Hi Ashish,

Please use below code to make it work perfect
 
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');
        }
    }
}


<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="ValidUser()"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
		
		<apex:outputpanel id="dummy">
		</apex:outputpanel
    </apex:form>
    <script>
    function ValidUser(){
        var bool = {!isValidCredential};
        if(bool){
            alert("Welcome to SALESFORCE");
        else
            alert("Invalide User details");
        }        
    }
    </script>
</apex:page>

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

sandeep sankhlasandeep sankhla
Hi Ashish,

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 
Ashish BiswasAshish Biswas
Thanks for the reply,
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;
    }
}
Ashish BiswasAshish Biswas
Changed the controller with the following code as well

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');
        }
    }
}
sandeep sankhlasandeep sankhla
Hi Ashish,

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 
 
Ashish BiswasAshish Biswas
I am not getting any error but the pop-up message is shoing the bolean value false. That means the function is not called ...
sandeep sankhlasandeep sankhla
Hi Ashish,

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 
 
sandeep sankhlasandeep sankhla
Hi Ashish,

Please use below code to make it work perfect
 
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');
        }
    }
}


<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="ValidUser()"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
		
		<apex:outputpanel id="dummy">
		</apex:outputpanel
    </apex:form>
    <script>
    function ValidUser(){
        var bool = {!isValidCredential};
        if(bool){
            alert("Welcome to SALESFORCE");
        else
            alert("Invalide User details");
        }        
    }
    </script>
</apex:page>

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 was selected as the best answer
Ashish BiswasAshish Biswas
Hi sandeep,
This is not working. 
Ashish BiswasAshish Biswas
No error reported though. The clcik even is not populating any message.
sandeep sankhlasandeep sankhla
Hi ,

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
Ashish BiswasAshish Biswas
I am facing a typical problem now, when I used the 'alert()' on oncomplete event, it's working fine and showing the boolean value properly. But when I change the event call with JS function, then what evenr value I entered its showing 'Invalide User', means the boolean value remain false or not refreshing.
Ashish BiswasAshish Biswas
Something like this-

<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>
sandeep sankhlasandeep sankhla
Hi Ashish,

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 
Ashish BiswasAshish Biswas
Not Working -
<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>
sandeep sankhlasandeep sankhla
Hi Ashish,

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 

 
Ashish BiswasAshish Biswas
Thanks, actually, its working once I put the script into output panel. Thanks a lot
sandeep sankhlasandeep sankhla
Hi Ashish,

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 
 
Ashish BiswasAshish Biswas
Hi Sandeep,
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
sandeep sankhlasandeep sankhla
Hi Ashish,

You can use action function with <Apex:param> to pass the value from VF page to controller..

Thanks
Sandeep
Ashish BiswasAshish Biswas
ok
sandeep sankhlasandeep sankhla

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

jhons dakejhons dake
Hi,
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.