Loading...
Categories
CRM Salesforce Uncategorized

Google reCAPTCHA Integration in Salesforce Visualforce Page using Apex

In the modern digital world, securing online forms from bots and spam is more important than ever. Whether you’re capturing leads, handling contact requests, or managing user registrations through a Visualforce page in Salesforce, protecting your forms from automated submissions is essential. Google reCAPTCHA is a powerful tool that helps identify and block such unwanted activity.

This article explains how to integrate Google reCAPTCHA into a Salesforce Visualforce page and validate user submissions using Apex, without diving into code.

What is Google reCAPTCHA?

Google reCAPTCHA is a free service by Google that protects websites from spam and abuse by verifying if a user is a human or a bot. It can be displayed as a simple checkbox, an invisible check, or a challenge-based interaction (like identifying traffic lights in images). It works on both the client-side (user’s browser) and the server-side (your Apex controller).

How Integration Works

Integrating reCAPTCHA in Salesforce involves a few simple steps:

  1. Register Your Website: Go to the Google reCAPTCHA admin console and generate a site key and a secret key.
  2. Add reCAPTCHA to Visualforce Page: The site key is used on the Visualforce page to show the reCAPTCHA widget.
  3. Form Submission: When a user fills out the form and submits it, a token is generated by reCAPTCHA and passed along with the form data.
  4. Server-Side Verification: The Apex controller receives the token and uses the secret key to verify the token by sending a request to Google’s reCAPTCHA API.
  5. Response Handling: Based on the verification result from Google, the form is either accepted or rejected.

Why Use Two Apex Classes?

To keep the integration clean and maintainable, the logic is split into two classes:

  • 1. Visualforce Page Controller: This handles form inputs, manages user interactions, and calls the reCAPTCHA validation logic.
  • 2. reCAPTCHA Service Class: This makes the callout to Google’s verification API, parses the response, and sends back a success or failure status

This approach follows Salesforce best practices: separation of concerns, code reusability, and easy testing

Benefits of Using reCAPTCHA with Visualforce

  • Prevents spam: Stops bots from submitting fake entries.
  • Improves security: Ensures only real users can interact with your forms.
  • Better user experience: The invisible or checkbox method is smooth for users.
  • Easy integration: With just a small setup, your forms become much safer.

 Best Practices

  • Always store the secret key securely using Custom Metadata, Custom Settings, or Named Credentials.
  • Handle errors and timeouts gracefully in your Apex logic.
  • Use HTTP mocks in test classes for callout testing.
  • Log or monitor verification failures for auditing or debugging.

conclusion:-

Implementing Google reCAPTCHA in a Visualforce form using Apex is a smart way to secure your Salesforce application from bots and spam. It not only enhances the trustworthiness of your system but also ensures a smoother experience for real users.

By separating responsibilities between a controller and a helper class, you follow clean architecture principles that make your solution scalable and easier to maintain in the long run.