0

I need to simplify my code, due to the IF formula being too long. This formula is being used in a visualforce page that populates a pagesection based on user input choices.

Can someone help me with shortening this so I no longer get an error?

<apex:pageblockSection rendered="{!
    OR(
        AND(
            CONTAINS(Intake__c.Personal_Care__c, 'Toileting/Bathing/Self-Care in Bathroom'), 
            !CONTAINS(Intake__c.Physical_Concerns__c, 'Controlling Environment with Limited Mobility (turning on lights/opening doors)'), 
            )
        AND(
            CONTAINS(Intake__c.Personal_Care__c, 'Sleeping Routines/Up and About at Night'),
            !CONTAINS(Intake__c.Physical_Concerns__c, 'Controlling Environment with Limited Mobility (turning on lights/opening doors)'), 
            )
        AND(
            CONTAINS(Intake__c.Personal_Care__c, 'Completing Daily Living Routines with Auditory Prompts like Laundry/Cooking/Cleaning'),
            !CONTAINS(Intake__c.Physical_Concerns__c, 'Controlling Environment with Limited Mobility (turning on lights/opening doors)'), 
            )
        AND(
            CONTAINS(Intake__c.Safety_Concerns__c, 'Cooking Safety/Meal Prep'),
            !CONTAINS(Intake__c.Physical_Concerns__c, 'Controlling Environment with Limited Mobility (turning on lights/opening doors)'), 
            )
        AND(
            CONTAINS(Intake__c.Social_Concerns__c, 'Having Privacy or Being Alone Part or All of Day or Night'),
            !CONTAINS(Intake__c.Physical_Concerns__c, 'Controlling Environment with Limited Mobility (turning on lights/opening doors)'), 
            )
        AND(
            CONTAINS(Intake__c.Social_Concerns__c, 'Using Tech for Social Connection with Friends and Family'),
            !CONTAINS(Intake__c.Physical_Concerns__c, 'Controlling Environment with Limited Mobility (turning on lights/opening doors)'), 
            )
        AND(
            CONTAINS(Intake__c.Social_Concerns__c, 'Using Tech for Entertainment (smart speakers/social media)'),
            !CONTAINS(Intake__c.Physical_Concerns__c, 'Controlling Environment with Limited Mobility (turning on lights/opening doors)'), 
            )
        AND(
            CONTAINS(Intake__c.Safety_Concerns__c, 'Entering or Exiting the Home/Answering the Door'),
                (OR(
                    CONTAINS(Intake__c.Personal_Care__c, 'Toileting/Bathing/Self-Care in Bathroom'), 
                    CONTAINS(Intake__c.Personal_Care__c, 'Sleeping Routines/Up and About at Night'),
                    CONTAINS(Intake__c.Personal_Care__c, 'Completing Daily Living Routines with Auditory Prompts like Laundry/Cooking/Cleaning'),
                    CONTAINS(Intake__c.Safety_Concerns__c, 'Cooking Safety/Meal Prep'),
                    CONTAINS(Intake__c.Social_Concerns__c, 'Using Tech for Social Connection with Friends and Family'),
                    CONTAINS(Intake__c.Social_Concerns__c, 'Having Privacy or Being Alone Part or All of Day or Night'),
                    CONTAINS(Intake__c.Social_Concerns__c, 'Using Tech for Entertainment (smart speakers/social media)'),
                    CONTAINS(Intake__c.Physical_Concerns__c, 'Getting in or Out of Bed or Chair'),
                    !CONTAINS(Intake__c.Physical_Concerns__c, 'Controlling Environment with Limited Mobility (turning on lights/opening doors)'),
                    )
                )
            )
            AND(
            CONTAINS(Intake__c.Safety_Concerns__c, 'Wandering or Elopement/Running Way or Egress (Leaving the Home)'),
                (OR(
                    CONTAINS(Intake__c.Personal_Care__c, 'Toileting/Bathing/Self-Care in Bathroom'), 
                    CONTAINS(Intake__c.Personal_Care__c, 'Sleeping Routines/Up and About at Night'),
                    CONTAINS(Intake__c.Personal_Care__c, 'Completing Daily Living Routines with Auditory Prompts like Laundry/Cooking/Cleaning'),
                    CONTAINS(Intake__c.Safety_Concerns__c, 'Cooking Safety/Meal Prep'),
                    CONTAINS(Intake__c.Social_Concerns__c, 'Using Tech for Social Connection with Friends and Family'),
                    CONTAINS(Intake__c.Social_Concerns__c, 'Having Privacy or Being Alone Part or All of Day or Night'),
                    CONTAINS(Intake__c.Social_Concerns__c, 'Using Tech for Entertainment (smart speakers/social media)'),
                    CONTAINS(Intake__c.Physical_Concerns__c, 'Getting in or Out of Bed or Chair'),
                    !CONTAINS(Intake__c.Physical_Concerns__c, 'Controlling Environment with Limited Mobility (turning on lights/opening doors)'),
                    )
                )
            )
            AND(
            CONTAINS(Intake__c.Safety_Concerns__c, 'Confusion or Dementia'),
                (OR(
                    CONTAINS(Intake__c.Personal_Care__c, 'Toileting/Bathing/Self-Care in Bathroom'), 
                    CONTAINS(Intake__c.Personal_Care__c, 'Sleeping Routines/Up and About at Night'),
                    CONTAINS(Intake__c.Personal_Care__c, 'Completing Daily Living Routines with Auditory Prompts like Laundry/Cooking/Cleaning'),
                    CONTAINS(Intake__c.Safety_Concerns__c, 'Cooking Safety/Meal Prep'),
                    CONTAINS(Intake__c.Social_Concerns__c, 'Using Tech for Social Connection with Friends and Family'),
                    CONTAINS(Intake__c.Social_Concerns__c, 'Having Privacy or Being Alone Part or All of Day or Night'),
                    CONTAINS(Intake__c.Social_Concerns__c, 'Using Tech for Entertainment (smart speakers/social media)'),
                    CONTAINS(Intake__c.Physical_Concerns__c, 'Getting in or Out of Bed or Chair'),
                    !CONTAINS(Intake__c.Physical_Concerns__c, 'Controlling Environment with Limited Mobility (turning on lights/opening doors)'),
                   )
                )
            )
            AND(
            CONTAINS(Intake__c.Safety_Concerns__c, 'Accessing Help for an Emergency'),
                (OR(
                    CONTAINS(Intake__c.Personal_Care__c, 'Toileting/Bathing/Self-Care in Bathroom'), 
                    CONTAINS(Intake__c.Personal_Care__c, 'Sleeping Routines/Up and About at Night'),
                    CONTAINS(Intake__c.Personal_Care__c, 'Completing Daily Living Routines with Auditory Prompts like Laundry/Cooking/Cleaning'),
                    CONTAINS(Intake__c.Safety_Concerns__c, 'Cooking Safety/Meal Prep'),
                    CONTAINS(Intake__c.Social_Concerns__c, 'Using Tech for Social Connection with Friends and Family'),
                    CONTAINS(Intake__c.Social_Concerns__c, 'Having Privacy or Being Alone Part or All of Day or Night'),
                    CONTAINS(Intake__c.Social_Concerns__c, 'Using Tech for Entertainment (smart speakers/social media)'),
                    CONTAINS(Intake__c.Physical_Concerns__c, 'Getting in or Out of Bed or Chair'),
                    !CONTAINS(Intake__c.Physical_Concerns__c, 'Controlling Environment with Limited Mobility (turning on lights/opening doors)'),
                    )
                )
            )
            AND(
            CONTAINS(Intake__c.Physical_Concerns__c, 'Walking or Mobility'),
                (OR(
                    CONTAINS(Intake__c.Personal_Care__c, 'Toileting/Bathing/Self-Care in Bathroom'), 
                    CONTAINS(Intake__c.Personal_Care__c, 'Sleeping Routines/Up and About at Night'),
                    CONTAINS(Intake__c.Personal_Care__c, 'Completing Daily Living Routines with Auditory Prompts like Laundry/Cooking/Cleaning'),
                    CONTAINS(Intake__c.Safety_Concerns__c, 'Cooking Safety/Meal Prep'),
                    CONTAINS(Intake__c.Social_Concerns__c, 'Using Tech for Social Connection with Friends and Family'),
                    CONTAINS(Intake__c.Social_Concerns__c, 'Having Privacy or Being Alone Part or All of Day or Night'),
                    CONTAINS(Intake__c.Social_Concerns__c, 'Using Tech for Entertainment (smart speakers/social media)'),
                    CONTAINS(Intake__c.Physical_Concerns__c, 'Getting in or Out of Bed or Chair'),
                    !CONTAINS(Intake__c.Physical_Concerns__c, 'Controlling Environment with Limited Mobility (turning on lights/opening doors)'),
                    )
                )
            )
            AND(
            CONTAINS(Intake__c.Physical_Concerns__c, 'Getting in or Out of Bed or Chair'),
                (OR(
                    CONTAINS(Intake__c.Personal_Care__c, 'Toileting/Bathing/Self-Care in Bathroom'), 
                    CONTAINS(Intake__c.Personal_Care__c, 'Sleeping Routines/Up and About at Night'),
                    CONTAINS(Intake__c.Personal_Care__c, 'Completing Daily Living Routines with Auditory Prompts like Laundry/Cooking/Cleaning'),
                    CONTAINS(Intake__c.Safety_Concerns__c, 'Cooking Safety/Meal Prep'),
                    CONTAINS(Intake__c.Social_Concerns__c, 'Using Tech for Social Connection with Friends and Family'),
                    CONTAINS(Intake__c.Social_Concerns__c, 'Having Privacy or Being Alone Part or All of Day or Night'),
                    CONTAINS(Intake__c.Social_Concerns__c, 'Using Tech for Entertainment (smart speakers/social media)'),
                    CONTAINS(Intake__c.Physical_Concerns__c, 'Getting in or Out of Bed or Chair'),
                    !CONTAINS(Intake__c.Physical_Concerns__c, 'Controlling Environment with Limited Mobility (turning on lights/opening doors)'),
                   )
                )
            )

        )
}">
                    <apex:outputText value="SH System Recommendation"/>
              </apex:pageblockSection>

1 Answer 1

1

You shouldn't really be using a formula for this. Build a method in Apex and use the result from that instead:

<apex:pageBlockSection rendered="{!shouldShowSection}">

Which is then a getter method:

public Boolean getShouldShowSection() {
  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;
  }
  // more if statements here as needed
  // Finally, we return false if the above if statements were not met:
  return false;
}

I suspect it may be possible optimize your conditions further, too, you may want to take more time optimizing your logic.

You must log in to answer this question.

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