2

I have a requirement where I have to display different pages depending on a Lead's custom field's (Score__c) value. Kindly help me with a sample code which states that if the value of Score__c is 'A', display /apex/PageA and If value of Score__c is 'B' then display /apex/PageB.

Here is the Controller:

public PageReference getScoreA  () {
    PageReference pageRef = new PageReference( '/apex/PageA' );        
    pageRef.setRedirect( true );
    return pageRef;
}   
public PageReference getScoreB () {
    PageReference pageRef = new PageReference( '/apex/PageB' );        
    pageRef.setRedirect( true );
    return pageRef;
}

public PageReference ViewProposal() {
  PageReference pageRef = null;
  List<Lead> LeadList = [Select id, Name ,Score__c FROM Lead];
  if (record.Score__C== 'A' {              
            pageRef = this.getScoreA();
    } else if (record.Score== 'B' {

           pageRef = this.getScoreB();
    }      
   return pageRef;
  }

Page-

 <apex:pageBlock>
    <apex:pageblockButtons>
        <apex:commandButton title="Submit" action="{!ViewProposal}" value="Submit" />            
    </apex:pageblockButtons>
  </apex:pageBlock>

2 Answers 2

3

Using several pages and the rendered attribute, you could do this relatively easily. Simplest form of that would look like this:

<apex:page standardController="Lead" ...>

  <apex:include pageName="pageA" rendered="{!Lead.Score__c == 'A'}"/>
  <apex:include pageName="pageB" rendered="{!Lead.Score__c == 'B'}"/>

</apex:page>

You would then need the pages called pageA and pageB respectively. But you probably don't need to do this as you can just do it on different components of your VF page like this:

<apex:page standardController="Lead" ...>

  <apex:pageBlock rendered="{!Lead.Score__c == 'A'}">
    <!-- one page block that works when score is A -->
  </apex:pageBlock>
  <apex:pageBlock rendered="{!Lead.Score__c == 'B'}">
    <!-- another page block that works when score is B -->
  </apex:pageBlock>

</apex:page>

In all these cases, this bit: {!Lead.Score__c == 'B'} is essentially a formula, resulting in a Boolean (true/false) value. If true, show this component and all content. If false, don't.

This is one of Visualforce's strengths: data-driven rendering of UI components. You can do this on as big a scale as you want (like the page blocks above), or on as minute a scale (a single field).

There are even ways to dynamically do this as values change using partial page refresh. There are lots of examples of this including the answer linked above.

EDIT: While I realize this is not a direct reply to the request to have a navigation that goes either to one page or another, I would recommend this approach. There are some advantages along the lines of maintainability namely:

  • There are fewer artifacts to maintain. By putting the logic in a single page, you have one place to go to change this when it is required in the future
  • If you have the data driven logic in the navigation, if you add a third value, you need to not only add another page, but you also must know to change all navigation points in the future.
  • The logic of which fields to display is all encapsulated in this page, meaning anytime any developer chooses to navigate to this page, it should behave according to your requirements. With your architecture, a developer must know that PageA is for one data value, and PageB is for another.
1
  • Thanks @Peter. I tried your code but its not giving the required output. I need to add an action on commandbutton which will check the score of a specific Lead and then display the page depending on the score. Sep 9, 2014 at 16:18
0

You could simplify your controller to be as shown below

public PageReference ViewProposal() {
  PageReference resPg;
  // extensible map as you add scores
  Map<String,PageReference> scoreToPgRefMap = 
        new Map<String,PageReference> {'A' => new PageReference('/apex/PageA'),
                                       'B' => new PageReference('/apex/PageB')};

  Lead l = [select id, score__c from Lead where id = :someLeadId];
  if (scoreToPgRefMap.containsKey(l.score__c) {
      resPg =  scoreToPgRefMap.get(l.score__c);
      resPg.setRedirect(true);
  } 
  return resPg;
}

I note that the code you posted references a variable called record which isn't declared - presumably you want to reference the Lead associated with the page that the commandButton is on. It wasn't clear if that button was on the Lead page - if so, then you don't need to query the Lead as you already have the lead in scope within your constructor.

5
  • Thanks for you reply. The code is giving an error: "unexpected token: '{' " at line 'new Map<String,PageReference> {'A' => new PageReference('/apex/PageA'), 'B' => new PageReference('/apex/PageB')};' I tried resolving this but it didn't solved Sep 10, 2014 at 7:05
  • I also tried to display images instead of VF pages depending on Score__c value. I tried this code '<apex:outputPanel layout="none"> <apex:image value="{!$Resource.ImageA}" rendered="{!Lead.Score__c}"/> <apex:image value="{!$Resource.ImageB}" rendered="{!Lead.Score__C}"/> </apex:outputPanel>' This displayed the images but if I want to display images depending on Score's value replacing '<apex:image value="{!$Resource.ImageA}" rendered="{!Lead.Score__c}"/>' with <apex:image value="{!$Resource.ImageA}" rendered="{!Lead.Score__c == 'A'}"/>' it gives a blank page Sep 10, 2014 at 7:28
  • fixed code; i had comment in wrong place
    – cropredy
    Sep 10, 2014 at 16:05
  • please add your new attempt as an update to the original post
    – cropredy
    Sep 10, 2014 at 16:07
  • I would recommend you put your comment relating to displaying images into a new question.
    – pchittum
    Sep 11, 2014 at 10:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .