0

I'm having some difficulty creating a method to call in a VF page. I believe I should be doing this by creating an Apex Class as a controller extension, but please correct me if I'm wrong and help me understand where I can create a method. I think SalesForce doesn't always do well with covering the very basics.

Here is what I currently have in a new Apex Class, but I'm getting errors on the variables, although, I believe I'm accessing the Intake controller, where these variables exist, so I'm not sure.

public with sharing class ShouldShowSHSection {
    public ApexPages.StandardController Intake {get; set;}
    public Boolean getShouldShowSHSection() {
  if(
    record.Personal_Care__c.contains('Toileting/Bathing/Self-Care in Bathroom') && !record.Physical_Concerns__c.contains('Controlling Environment with Limited Mobility (turning on lights/opening doors)')
    ) 
    {
    return true;
    }

  if(
    record.Personal_Care__c.contains('Sleeping Routines/Up and About at Night') && !record.Physical_Concerns__c.contains('Controlling Environment with Limited Mobility (turning on lights/opening doors)')
    )  
    {
    return true;
    }

    if(
    record.Personal_Care__c.contains('Completing Daily Living Routines with Auditory Prompts like Laundry/Cooking/Cleaning') && !record.Physical_Concerns__c.contains('Controlling Environment with Limited Mobility (turning on lights/opening doors)')
    )  
    {
    return true;
    }
  
  if(
    record.Safety_Concerns__c.contains('Cooking Safety/Meal Prep') && !record.Physical_Concerns__c.contains('Controlling Environment with Limited Mobility (turning on lights/opening doors)')
    )  
    {
    return true;
    }

  if(
    record.Social_Concerns__c.contains('Having Privacy or Being Alone Part or All of Day or Night') && !record.Physical_Concerns__c.contains('Controlling Environment with Limited Mobility (turning on lights/opening doors)')
    )  
    {
    return true;
    }

    if(
    record.Social_Concerns__c.contains('Using Tech for Social Connection with Friends and Family') && !record.Physical_Concerns__c.contains('Controlling Environment with Limited Mobility (turning on lights/opening doors)')
    )  
    {
    return true;
    }


    if(
    record.Social_Concerns__c.contains('Using Tech for Entertainment (smart speakers/social media)') && !record.Physical_Concerns__c.contains('Controlling Environment with Limited Mobility (turning on lights/opening doors)')
    )  
    {
    return true;
    }


    if(
    record.Safety_Concerns__c.contains('Using Tech for Entertainment (smart speakers/social media)') && (
    !record.Physical_Concerns__c.contains('Controlling Environment with Limited Mobility (turning on lights/opening doors)') ||
    record.Personal_Care__c.contains('Toileting/Bathing/Self-Care in Bathroom') ||
    record.Personal_Care__c.contains('Sleeping Routines/Up and About at Night') ||
    record.Personal_Care__c.contains('Completing Daily Living Routines with Auditory Prompts like Laundry/Cooking/Cleaning') ||
    record.Safety_Concerns__c.contains('Cooking Safety/Meal Prep') ||
    record.Social_Concerns__c.contains('Having Privacy or Being Alone Part or All of Day or Night') ||
    record.Social_Concerns__c.contains('Using Tech for Social Connection with Friends and Family') ||
    record.Social_Concerns__c.contains('Using Tech for Entertainment (smart speakers/social media)') ||
    record.Physical_Concerns__c.contains('Getting in or Out of Bed or Chair')
    )
    )  
    {
    return true;
    }


    if(
    record.Safety_Concerns__c.contains('Wandering or Elopement/Running Way or Egress (Leaving the Home)') && (
    !record.Physical_Concerns__c.contains('Controlling Environment with Limited Mobility (turning on lights/opening doors)') ||
    record.Personal_Care__c.contains('Toileting/Bathing/Self-Care in Bathroom') ||
    record.Personal_Care__c.contains('Sleeping Routines/Up and About at Night') ||
    record.Personal_Care__c.contains('Completing Daily Living Routines with Auditory Prompts like Laundry/Cooking/Cleaning') ||
    record.Safety_Concerns__c.contains('Cooking Safety/Meal Prep') ||
    record.Social_Concerns__c.contains('Having Privacy or Being Alone Part or All of Day or Night') ||
    record.Social_Concerns__c.contains('Using Tech for Social Connection with Friends and Family') ||
    record.Social_Concerns__c.contains('Using Tech for Entertainment (smart speakers/social media)') ||
    record.Physical_Concerns__c.contains('Getting in or Out of Bed or Chair')
    )
    )  
    {
    return true;
    }

    if(
    record.Safety_Concerns__c.contains('Confusion or Dementia') && (
    !record.Physical_Concerns__c.contains('Controlling Environment with Limited Mobility (turning on lights/opening doors)') ||
    record.Personal_Care__c.contains('Toileting/Bathing/Self-Care in Bathroom') ||
    record.Personal_Care__c.contains('Sleeping Routines/Up and About at Night') ||
    record.Personal_Care__c.contains('Completing Daily Living Routines with Auditory Prompts like Laundry/Cooking/Cleaning') ||
    record.Safety_Concerns__c.contains('Cooking Safety/Meal Prep') ||
    record.Social_Concerns__c.contains('Having Privacy or Being Alone Part or All of Day or Night') ||
    record.Social_Concerns__c.contains('Using Tech for Social Connection with Friends and Family') ||
    record.Social_Concerns__c.contains('Using Tech for Entertainment (smart speakers/social media)') ||
    record.Physical_Concerns__c.contains('Getting in or Out of Bed or Chair')
    )
    )  
    {
    return true;
    }

    if(
    record.Safety_Concerns__c.contains('Accessing Help for an Emergency') && (
    !record.Physical_Concerns__c.contains('Controlling Environment with Limited Mobility (turning on lights/opening doors)') ||
    record.Personal_Care__c.contains('Toileting/Bathing/Self-Care in Bathroom') ||
    record.Personal_Care__c.contains('Sleeping Routines/Up and About at Night') ||
    record.Personal_Care__c.contains('Completing Daily Living Routines with Auditory Prompts like Laundry/Cooking/Cleaning') ||
    record.Safety_Concerns__c.contains('Cooking Safety/Meal Prep') ||
    record.Social_Concerns__c.contains('Having Privacy or Being Alone Part or All of Day or Night') ||
    record.Social_Concerns__c.contains('Using Tech for Social Connection with Friends and Family') ||
    record.Social_Concerns__c.contains('Using Tech for Entertainment (smart speakers/social media)') ||
    record.Physical_Concerns__c.contains('Getting in or Out of Bed or Chair')
    )
    )  
    {
    return true;
    }

    if(
    record.Physical_Concerns__c.contains('Walking or Mobility') && (
    !record.Physical_Concerns__c.contains('Controlling Environment with Limited Mobility (turning on lights/opening doors)') ||
    record.Personal_Care__c.contains('Toileting/Bathing/Self-Care in Bathroom') ||
    record.Personal_Care__c.contains('Sleeping Routines/Up and About at Night') ||
    record.Personal_Care__c.contains('Completing Daily Living Routines with Auditory Prompts like Laundry/Cooking/Cleaning') ||
    record.Safety_Concerns__c.contains('Cooking Safety/Meal Prep') ||
    record.Social_Concerns__c.contains('Having Privacy or Being Alone Part or All of Day or Night') ||
    record.Social_Concerns__c.contains('Using Tech for Social Connection with Friends and Family') ||
    record.Social_Concerns__c.contains('Using Tech for Entertainment (smart speakers/social media)') ||
    record.Physical_Concerns__c.contains('Getting in or Out of Bed or Chair')
    )
    )  
    {
    return true;
    }

    if(
    record.Physical_Concerns__c.contains('Getting in or Out of Bed or Chair') && (
    !record.Physical_Concerns__c.contains('Controlling Environment with Limited Mobility (turning on lights/opening doors)') ||
    record.Personal_Care__c.contains('Toileting/Bathing/Self-Care in Bathroom') ||
    record.Personal_Care__c.contains('Sleeping Routines/Up and About at Night') ||
    record.Personal_Care__c.contains('Completing Daily Living Routines with Auditory Prompts like Laundry/Cooking/Cleaning') ||
    record.Safety_Concerns__c.contains('Cooking Safety/Meal Prep') ||
    record.Social_Concerns__c.contains('Having Privacy or Being Alone Part or All of Day or Night') ||
    record.Social_Concerns__c.contains('Using Tech for Social Connection with Friends and Family') ||
    record.Social_Concerns__c.contains('Using Tech for Entertainment (smart speakers/social media)') ||
    record.Physical_Concerns__c.contains('Getting in or Out of Bed or Chair')
    )
    )  
    {
    return true;
    }

  return false;
}

}

My VisualForce Page will be calling this like this:

<apex:pageBlockSection rendered="{!shouldShowSHSection}">
1
  • you probably want to be using containsIgnoreCase(...) to avoid failing to match on capitalization of some term in these long strings
    – cropredy
    Oct 28, 2020 at 22:35

1 Answer 1

1

You don't need to create a method to access data in Visualforce; you need to expose a property. You've really already done this, because Visualforce understands a method named getShouldShowSHSection() to be the "getter" for a property named shouldSHowSHSection, which you can access in a Visualforce expression. Note that this is largely equivalent to the get/set style getters and setters that you can also use.

The problem here is that the variable record does not exist. You'd need to access it by doing this.Intake.getRecord() to ask the standard controller for it.

Note that you have to ensure that the standard controller queries all of the fields you wish to use when it acquires record data. The easiest way to do so is to reference the field in your page's markup; if you do not reference the fields you are using here in markup, you would need to call the standard controller's addFields() method on the standard controller instance when you receive it in your extension class's controller.

Edit

Your Apex and Visualforce are correct, saving only that you are referencing an undefined record variable in Apex. You have to assign it via Intake__c record = (Intake__c)this.Intake.getRecord().

Also bear in mind the note above about field access, though; if you get an error about a field not being retrieved via SOQL, that's the problem.

4
  • David, I apologize, I'm still having difficulty understanding. Initially, I tried to add this through ifContains within my Visualforce page, but the function was too long and could not be saved, so I've rewritten it as you see here. Would you be able to explain to me with a visual example of how I can update my existing work? Thank you so much for your help. Oct 28, 2020 at 20:36
  • @AlexisFrisch Can you edit your question to show where you reference this property in your Visualforce markup? I think you have everything correct except the reference to the record variable that doesn't (yet) exist.
    – David Reed
    Oct 28, 2020 at 20:37
  • I have updated above, Is that what you mean? Oct 28, 2020 at 20:42
  • I'm having trouble with this. I'm getting a Attempt to de-reference a null object error. I expect this might be because we're asking to 'get' a record that doesn't yet exist, as the record is created with user-filled fields on the form when they click 'submit' - Do you have any feedback? Oct 29, 2020 at 15:41

You must log in to answer this question.

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