How to Fix Error: Formula Expression is Required on the Action Attributes

When creating a visualforce page for a specific requirement, it is important to ensure the syntax used to write the page is accurate as often times the error returned may not correctly indicate where the problem is in your code.

One such error that developers often come across with Visualforce Pages is ‘Formula Expression is required on the action attributes‘.

In this article we detail a few common scenarios because of which a developer may come across this error.

Common Scenarios and Fixes

1. PageReference Syntax

When trying to redirect a Visualforce Page to another using PageReference in your controller class, ensure the syntax you use is compliant with what Salesforce would like to receive.

If you miss to add the backsplash before specifying your visualforce page name or even adding ‘/apex/’ before the VFP, then an error will be thrown on Runtime not during compilation.

Incorrect syntax

PageReference redirectPage = new PageReference('apex/SomeVisualForcePage');

Correct Syntax

PageReference redirectPage = new PageReference('/apex/SomeVisualForcePage');

2. Commanbuttons Action Expression Syntax

A common reason why ‘Formula Expression is required on the action attributes‘ error might occur is when a developer does not specify the method on an Action expression of a Command Button correctly.

Incorrect syntax

<apex:commandButton value="Save" action="!Save"/>

Correct Syntax

<apex:commandButton value="Save" action="{!Save}"/>

3. Miscellaneous Causes

There are other reasons why a  developer may encounter ‘Formula Expression is required on the action attributes’  error. We have listed a couple below:

  1. Using Onclick instead of Action to Pass Parameters to action function using Command Button. 
  2. Returning Values from CommandButton Methods to VFP

Conclusion

Salesforce does not make it easy for a developer to easily diagnose issues with visualforce pages. That is why it is important to ensure that the syntax used in the markup of the page is correct.