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
bharathvbharathv 

login controller error

Hi,

 

i am trying to validate the credintials but its giving error

Below is the my login controller let me know where i am wrong

 

 


public class LoginController {

public list<Candidate__c> listCandidates{get;set;}
    public string searchString {get;set;}
    public boolean showmsg{get;set;}
    public LoginController(ApexPages.StandardController controller)
    {
        listCandidates=new list<Candidate__c>();
        showmsg=false;
    }
    public pagereference ValidateUserName()
    {
        String AccountQuery = 'select UserName__c, PassWord__c from Candidate__c';
        
        if(searchString != '' && searchString != null)
            AccountQuery = AccountQuery +
            ' where name  =: ' + 'searchString' ;

 

so when i tried to login,i am getting below error i have entered user name as 'jakson'

 

System.QueryException: Variable does not exist: jakson

 

Error is in expression '{!ValidateUserName}' in component <apex:page> in page candidate_login_page

 

 

Class.LoginController.ValidateUserName: line 18, column 1          

 

 

Can anyone help me in this issue,

Thanks & Regards,

Bharath

Best Answer chosen by Admin (Salesforce Developers) 
NiketNiket

Hi ,

 

As I can observe in your code, searchString is a binding variable and you have put it in single quotes. That should not be the case.

 

' where name  =\''+ searchString +'\'' ;

 

Try this in the last line.

 

Please mark it as the solution if it answers your question so that others can also take benifit. 

All Answers

NiketNiket

Hi ,

 

As I can observe in your code, searchString is a binding variable and you have put it in single quotes. That should not be the case.

 

' where name  =\''+ searchString +'\'' ;

 

Try this in the last line.

 

Please mark it as the solution if it answers your question so that others can also take benifit. 

This was selected as the best answer
bharathvbharathv

Thanks Niket for quick response

 

it's working fine now.

 

Actually,i am validating the username and password for login page, so till username validation its fine.

 

Then how to check for corresponding password to that username

 

Thaks & Regards,

Bharath

 

NiketNiket

once user puts user name and password , you can fire a query like this :

 

select Id from Candidate__c where UserName__c ='Test Name' AND  PassWord__c ='Test Password'

(Please make this query dynamic as per your need.)

 

if this query returns any id, then user name and password is validated else not.

 

There may be any other solution , but I am sure it will work for you.

bharathvbharathv

Can you tell me how to make it dynamic

 

<apex:inputText label="Search" value="{!searchusername }"/>

<apex:inputText label="Search" value="{!searchpassword }"/>

 

those are the tags i am used in my login page

 

Thanks & regards,

Bharath

 

 

bharathvbharathv

Below one works?

 

select Id from Candidate__c where UserName__c ='searchusername' AND  PassWord__c ='searchpassword'

 

 

 

 

NiketNiket

String query = 'select Id from Candidate__c where UserName__c =\'' + searchusername+'\''+ ' AND  PassWord__c =\'' + searchpassword+ '\'';

 

This should work.

Abc234Abc234

below is my code

but its not validating the creditinals instead its redirecting to page i have mentioned

 

 public pagereference ValidateUserName()
    {

      String AccountQuery = 'select UserName__c, PassWord__c from Candidate__c';
       
      if(searchString != '' && searchString != null)
         
      AccountQuery = AccountQuery +
      'where UserName__c =\'' + searchString+'\''+ ' AND  PassWord__c =\'' + searchpassword+ '\''';
     
      listCandidates=database.query(AccountQuery);   
            
                        
      if(listCandidates.size()>0 && listCandidates!=null)
      showmsg=false;
      else
      {
        ApexPages.Message errMsg = new ApexPages.Message
        (ApexPages.Severity.error,'UserName is invalid   ');
        ApexPages.addMessage(errMsg);
        showmsg=true;
      }

       Pagereference pg = page.New_Job_Application;
      pg.setRedirect(true);
    return pg;
    }
  }

 

 

Thanks & Regards

Bharath

NiketNiket

if(listCandidates.size()>0 && listCandidates!=null)
{
showmsg=false;
Pagereference pg = page.New_Job_Application;
pg.setRedirect(true);
return pg;
}

else
{
ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.error,'UserName is invalid ');
ApexPages.addMessage(errMsg);
showmsg=true;
return null;
}

 

Try this if it works.

Abc234Abc234

still i am getting below error

 

below is the my complete controller

 

public class LoginController {

public list<Candidate__c> listCandidates{get;set;}
public string searchString {get;set;}
public string searchpassword {get;set;}

public boolean showmsg{get;set;}
public LoginController(ApexPages.StandardController controller)
    {
        listCandidates=new list<Candidate__c>();
        showmsg=false;
    }
    public pagereference ValidateUserName()
    {

       
        String AccountQuery = 'select UserName__c, PassWord__c from Candidate__c';
       
        if(searchString != '' && searchString != null)
         
            AccountQuery = AccountQuery +
           ' where Candidate__c.UserName__c  =: ' + searchString + 'AND  Candidate__c.PassWord__c =: ' + searchpassword ;

// 'where Candidate__c.UserName__c =\'' + searchString+'\''+ ' AND  Candidate__c.PassWord__c =\'' + searchpassword+ '\''';

// if use above class am getting  LoginController Compile Error: line breaks not allowed in string literals at line 22 column -1 so i used first where condition
           
           listCandidates=database.query(AccountQuery);  
            if(listCandidates.size()>0 && listCandidates!=null)
        {
            showmsg=false;
            Pagereference pg = page.New_Job_Application;
            pg.setRedirect(true);
            return pg;
        }

        else
        {
            ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.error,'UserName is invalid ');
            ApexPages.addMessage(errMsg);
            showmsg=true;
            return null;
        }

                        
    
    }
}

 

 

Thanks & Regards,

Bharath

NiketNiket

In the commented line , there is one extra single quote at the end. correct line is :

 

 'where Candidate__c.UserName__c =\'' + searchString+'\''+ ' AND  Candidate__c.PassWord__c =\'' + searchpassword+ '\'';

 

at what point you are stuck now ?

Abc234Abc234

now its giving below one

 

System.QueryException: Invalid alias Candidate__c.UserName__c

Error is in expression '{!ValidateUserName}' in component <apex:page> in page candidate_login_page
 
Thanks & Regards,
Bharath