0

I have a pageblockSection I want to render if a conditional evaluates to true.

This is my VF code

<apex:page standardController="Intake__c" extensions="IntakeExtension,ShouldShowSHSection" standardStyleSheets="false" showHeader="false" sidebar="false">
 
 <apex:pageblock id="technologyRecommendation">
     <apex:pageblockSection rendered="{!shouldShowSHSection}">
         <apex:outputText value="SH System Recommendation"/>
     </apex:pageblockSection>
 </apex:pageblock>
</apex:page>

This is my Controller Code

public with sharing class ShouldShowSHSection{
    public ShouldShowSHSection(ApexPages.StandardController paramController) {
       Intake = paramController;}
    public ApexPages.StandardController Intake {get; set;}
    public Boolean getShouldShowSHSection() {
       Intake__c record = new Intake__c();
          if(
            record.Personal_Care__c != null && (record.Personal_Care__c.contains('Bathroom') && record.Physical_Concerns__c == null)
            ) 
            {
            return true;
            }
 
          return false;
        }
        
        }

Can someone explain to me why my pageblockSection does not render when Personal_Care__c contains 'Bathrooom' ?

1 Answer 1

1

Because you are checking the values of the variable record that you have just declared.

If you try to replace

Intake__c record = new Intake__c();

with (could be wrong depending on the scenario, continue reading)

Intake__c record = (Intake__c)Intake.getRecord();

You would have an issue as the standard controller only brings with it the fields that are requested on the page (besides the Id)

You have (at least) two options:

  1. You could add hidden fields to your view that includes the fields you would use in your extension(s), which adds "coupling", which is NOT recommended.
  2. Y could take the id and query the fields you need yourself.

Following number 2, you would need to replace it with

Id recordId = ((Intake__c)Intake.getRecord()).Id;

if (recordId == null) {
    return true; // or false, whatever you want
} else {

    Intake__c record = [
        SELECT 
            Personal_Care__c, 
            Physical_Concerns__c 
        FROM Intake__c 
        WHERE Id = :recordId
    ];

    // the rest of the method
}// close them properly ;)

Result after chat

For future reference, in case it helps (a possible solution). also possible to have an apex:function called on the onChange of each apex:inputField, for example

Page
<apex:page standardController="Intake__c" extensions="IntakeExtension,ShouldShowSHSection" standardStyleSheets="false" showHeader="false" sidebar="false">
    <apex:form>
        ...
        <apex:pageBlock>
            <apex:pageBlockSection >
                <apex:inputField styleClass="fieldset" value="{!Intake__c.Personal_Care__c}">
                    <apex:actionSupport event="onchange" reRender="technologyRecommendation"/>
                </apex:inputField>
            </apex:pageBlockSection>

            <apex:pageBlockSection >
                <apex:inputField styleClass="fieldset" value="{!Intake__c.Physical_Concerns__c}">
                    <apex:actionSupport event="onchange" reRender="technologyRecommendation"/>
                </apex:inputField>
            </apex:pageBlockSection>
        </apex:pageblock>
        ...
        <apex:pageBlock id="technologyRecommendation">
            <apex:pageBlockSection rendered="{!shouldShowSHSection}">
                <apex:outputText value="SH System Recommendation"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
        ....
    </apex:form>
</apex:page>
Apex
public with sharing class ShouldShowSHSection {

    Intake__c record;
    public ShouldShowSHSection(ApexPages.StandardController paramController) {
        record = (Intake__c)paramController.getRecord();Intake = paramController;
    }

    public Boolean getShouldShowSHSection() {
        return record.Personal_Care__c != null && record.Personal_Care__c.contains('Bathroom') && record.Physical_Concerns__c == null;
    }

}
7
  • I return the error 'Method does not exist or incorrect signature: void getRecordId() from the type ApexPages.StandardController' here Commented Nov 4, 2020 at 21:22
  • edited, can you try again? Commented Nov 4, 2020 at 21:28
  • Yes - When I attempt to load the VF page, I meet this error 'List has no rows for assignment to SObject' Commented Nov 4, 2020 at 21:28
  • do you have an id in the url? (https://....com/apex/yourPage?id=xxxxxx) and xxxxxx is the id of an Intake__c record? Commented Nov 4, 2020 at 21:29
  • I do not - The field is null on the VF page until a user makes a selection. Could this affect this? Commented Nov 4, 2020 at 21:31

You must log in to answer this question.

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