function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Calling a class from a button

Hi All,


I want to add a button to the Opportunity Page that, when clicked, checks that certain fields have valid data.


If they don't, then I will display an error message.


If they do they I'll do something else.


So, is the right way to do this:


1) User clicks button which calls URL to a VF page

2) VF page calls the controller to check validation

3) If not valid, set errors on Opportunity and redirect user back to Opportunity page (somehow)

4) If valid, do the other action and send user back to Opportunity (somehow)


This feels wrong. For a start the VF page is purely there for calling the controller class and never actually displays anything.


What's the 'right' way to do this?




You'll need


  1. Create an apex class that does the conditional routing analysis.
  2. Expose this class as a webservice so you can call it using javascript from when your button is clicked (
  3. Use the Apex. PageReference class to redirect to Visualforce pages or back to the Opportunity (see Visualforce Guide or Apex Guide reference sections)
  4. Use System.assert or the Exception classes to push your error back to the Opportunity page as a nice red error message.

Hope that helps!