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
OldDeadBugOldDeadBug 

Conditional Redirect

I have a VF Page where the constructor access a get method which queries a list of Accounts for display in a PageBlockTable.

 

If the list gets loaded, the page displays correctly with the table of Accounts.

 

However, if no Accounts are loaded from the query, the page is blank. What I want to do is redirect to a different page with a message saying No Accounts are available with a button to return back to another page. 

 

I haven't been able to figure how to add this redirect though. 

 

I have a PageReference method which is set correctly, and the constructor reads

 

 

public class AccountList { public AccountList() { EligibleAccts = getEligibleAccts(); if (getEligibleAccts().isEmpty()) { NoEligibleAccts(); } system.debug('EligibleAccts size is '+EligibleAccts.size()); }

 

public Account[] getEligibleAccts()

{

  EligibleAccts = [QUERY];
return EligibleAccts;

} public Pagereference NoEligibleAccts() { PageReference PR = new PageReference('/apex/NoEligibleAccts'); PR.setRedirect(true); }

 

 THE PAGE CODE

 

<apex:page Controller="AccountList" sidebar="false" standardStylesheets="true"

 <apex:form >
<apex:pageBlock >
<apex:pageMessages />
<apex:pageBlockButtons >
<apex:CommandButton action="{!TRStatusSet}" value="Set Transfer Status"/>
<apex:CommandButton action="{!Cancel}" value="Cancel"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!EligibleAccts}" var="acc" >

....PageBlockTable columns

</apex:pageBlockTable>

</apex:pageBlock>
</apex:form>

 

</apex:page>

 

 

So, this doesn't work. The page loads as a blank white page.

 

So I'm not sure of the best method for doing this, and none of my attempts have worked. I suspect this is due to my lack of understanding of JavaScript, which I am learning slowly. Maybe an answer to this will help.

 

So, should I

1) Try to use an 'action' process on the <apex:page> that could redirect, if I can create a conditional statement at that point in the code

2) Tweak my constructor method to make the above process work?

3) Adapt the link that calls the page - currently I'm just using a URL for the Custom Link. I believe I've seen an OnClick JavaScript syntax before which can direct the user to a different page, but can I access the controller's getEligibleAccts method to check for a result before I set which VF Page to sned the user???

 

Any help is appreciated

 

ODB

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
S_LieS_Lie

Hi,

 

You can use 

 

<apex:page Controller="AccountList" sidebar="false" standardStylesheets="true" actions = "initCheck">

 

function autoCheck will check the condition whether the query is empty

 

     public PageReference initCheck()
    {


        elligibleActs = [query]       

        if(elligibleActs.isEmpty())
        {
            PageReference pageRef = new PageReference('/apex/NoEligibleAccts');
            pageRef.setRedirect(true);
            return pageRef;
        }
       
        return null;
    }

 

its shold work.

All Answers

S_LieS_Lie

Hi,

 

You can use 

 

<apex:page Controller="AccountList" sidebar="false" standardStylesheets="true" actions = "initCheck">

 

function autoCheck will check the condition whether the query is empty

 

     public PageReference initCheck()
    {


        elligibleActs = [query]       

        if(elligibleActs.isEmpty())
        {
            PageReference pageRef = new PageReference('/apex/NoEligibleAccts');
            pageRef.setRedirect(true);
            return pageRef;
        }
       
        return null;
    }

 

its shold work.

This was selected as the best answer
OldDeadBugOldDeadBug

That did the trick!

 

Thanks

 

 

Yunus KalamYunus Kalam
S_Lie your trick is really helpful, but there is 1 spell mistake on actions ="initCheck" it is action="initCheck" :)

Thank you
Yunus Kalam