+ Start a Discussion
Scott0987Scott0987 

visualforce page redirect based on field value

I am creating a login page where some of my customers can access a small amount of data.  I have created a login page and after it verifies the username and password of the user based on a username and password field on the account, it then sends them to a second page where the info will be displayed.  I have a field on the account called Logged In that gets checked when they log in and then unchecked after a period of time to log them out.  I have created the following visualforce page and extension.  What happens is that it never flags it as not logged in.  So the if else statement never seems to run. Here is the code.

Visualforce Page:

<apex:page standardController="account" extensions="CustomerPageExt" showHeader="false">
<apex:outputPanel rendered="alreadyLoggedIn()">
<script>
window.top.location='http://pccaresupport.sandbox.cs17.force.com/login';
</script>
</apex:outputPanel>

<apex:outputField value="{!Account.name}" />
<apex:outputField value="{!Account.Logged_In__c}" />


</apex:page>

 

Extension:

public class CustomerPageExt {

public account currentAcctID {get;set;}

public CustomerPageExt(ApexPages.StandardController controller) {
currentAcctID = (Account) controller.getrecord();
}

public boolean alreadyLoggedIn(){
Account a = [select id, Logged_In__c from Account where id = :currentAcctID.id];
if(a.Logged_In__c = True){
return True;
}
Else{
return False;
}
}
}

Best Answer chosen by Admin (Salesforce Developers) 
Scott0987Scott0987

I figured it out.  After your example I played with a couple of things and figured it out.  Here is the code I ended up with.

Visualforce Page: 

<apex:page standardController="account" extensions="CustomerPageExt" showHeader="false" >
<apex:outputPanel rendered="{!b}">
<script>
window.top.location='http://pccaresupport.sandbox.cs17.force.com/login';
</script>
</apex:outputPanel>

<apex:outputField value="{!Account.name}" /><br />
<apex:outputField value="{!Account.Logged_In__c}" /><br />
{!b}<br />
currentAcctID.id: {!i}<br />
{!ai}

 

</apex:page>

 

Extension:

public class CustomerPageExt {

public account currentAcctID {get;set;}
public boolean b {get;set;}
public id i {get;set;}
public id ai {get;set;}

public CustomerPageExt(ApexPages.StandardController controller) {
currentAcctID = (Account) controller.getrecord();
i = currentAcctID.id;
Account a = [select id, Logged_In__c from Account where id = :i limit 1];
b = !a.Logged_In__c;
ai = a.id;
}
}

 

I was using the variables to figure out where the data was lost.  It was then that I figured I could simplify it.  and that ended up working. 

All Answers

Gunners_23Gunners_23

I guess the problem is in the below line

 

<apex:outputPanel rendered="alreadyLoggedIn()">

 

Here you're trying to call the controller method but since syntax is improper its not calling constructor method

 

Change it below code and try

 

<apex:outputPanel rendered="{!alreadyLoggedIn}">

Scott0987Scott0987

When I do it that way I get this error:

Error: Unknown property 'AccountStandardController.alreadyLoggedIn'

Scott0987Scott0987

I have this morning narrowed down to some extent where the issue is.  I took and assigned things to variables at different points to see where the info is lost.  my querry    

Account a = [select id, Logged_In__c from Account where id = :currentAcctID.id];

is not returning any results.  I am wondering if it is because the currentAcctID.id is the 18 character ID.  Is it possible that it is trying to compair 15 character IDs to the ID that I get and that is why it does not find anything?  

Scott0987Scott0987

I figured it out.  After your example I played with a couple of things and figured it out.  Here is the code I ended up with.

Visualforce Page: 

<apex:page standardController="account" extensions="CustomerPageExt" showHeader="false" >
<apex:outputPanel rendered="{!b}">
<script>
window.top.location='http://pccaresupport.sandbox.cs17.force.com/login';
</script>
</apex:outputPanel>

<apex:outputField value="{!Account.name}" /><br />
<apex:outputField value="{!Account.Logged_In__c}" /><br />
{!b}<br />
currentAcctID.id: {!i}<br />
{!ai}

 

</apex:page>

 

Extension:

public class CustomerPageExt {

public account currentAcctID {get;set;}
public boolean b {get;set;}
public id i {get;set;}
public id ai {get;set;}

public CustomerPageExt(ApexPages.StandardController controller) {
currentAcctID = (Account) controller.getrecord();
i = currentAcctID.id;
Account a = [select id, Logged_In__c from Account where id = :i limit 1];
b = !a.Logged_In__c;
ai = a.id;
}
}

 

I was using the variables to figure out where the data was lost.  It was then that I figured I could simplify it.  and that ended up working. 

This was selected as the best answer