Friday, June 1, 2018

Alternatives to OnClick Javascript buttons : Way to replace OnClick Javascript buttons across all objects with Single VF page

We always prefer onclick javascript button to perform different operations like updating fields on same records or on other records with single click by user. In order to do this, we either use ajax toolkit to either perform logic or to call apex method.

We can also perform these operation even by redirecting user to VF page on click of button and call apex method on VF page load (by specifying action attribute on <apex:page>) and redirecting User back to record details page.

Developers usually not use this approach as they do not want to create new VF page every time for these kind of requirements.

After the launch of lightning, onclick javascript buttons are not recommended and everyone is finding alternative for onclick javascript buttons functionality but sometime these alternative results in more than 1 click for end user.

Through this blog, I just to share one approach through which we can utilize single VF for all custom buttons. We just need to pass recordId and apex class name which contains your logic for operation in URL. This approach also involves single click from user and user will get notified about success and failure of operation (in onclick javascript we user alert to notify).

Different steps involved for this approach:
  • Create a VF page and controller. Create wrapper in this controller to store redirect url (to redirect user after performing operation), success and failure message which you want to display to user.
  • Create an Interface with method name executeLogic (return type will be wrapper of VF controller).
  • Create a new apex class which implements interface and will contain logic in executeLogic method.
  • Create a custom button and specify URL. pass recordid and apex class name where you have written your logic.

Sample Requirement:

I need to create a custom button on Account. When user click on that, I need to check if billing country and billing postalcode in not blank, then change the type field to 'Prospect'.

Traditional Approach:

Create a onclick Javascript button and call apex method from that to perform operation. Dispaly success/error message using alerts.

New Approach:

Below are code related to new approach:

Now create custom button on Account as mentioned in below image

URL will be like:


here "AccountUtility" is apex class name which contain logic.

If we use this approach then, going forward we just need to create apex class with method and implement the interface.

If we consider the both approaches, the amount of code that we need to write for any new custom button functionality will remain same.

Demo snapshots:

When I click on Change to Prospect button with billing country and billing postal code as blank, getting below message:

When clicking on Change to Prospect button after specifying values for billing country and postal code, getting below message:

These success and error message are specified in apex method in apex class. So you can specify different message based on different requirements.

Also you can specify the redirect URL in apex method where user will navigate after performing the requested action.

You can style your VF page as per your org need and all users will have same user experience.

Hope this will help!!!

Looking forward for everyone comments and suggestions.


  1. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.

    Herbalife in Chennai
    Wellnesscentres in Chennai
    Weight loss in Chennai
    Weight gain in Chennai

    1. Great Article android based projects

      Java Training in Chennai Project Center in Chennai Java Training in Chennai projects for cse The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

  2. Can we do the same for list button also how will you assign VF page to list button?

  3. This is innovative...Kudos

  4. You can find dissertation online sites from the web make your home pickup unsurprisingly referred to in your ─▒nternet sites. how to link js to html