Salesforce is a versatile platform offering various tools to customise and enhance your organisation’s CRM experience. Two essential tools in the Salesforce developer’s toolkit are Apex and Visualforce. While they often work together, they serve very different purposes. Let’s explore what Apex and Visualforce are, how they differ, and when to use them, with some code examples to clarify.
What is Apex?
Apex is a programming language developed by Salesforce that allows developers to write backend logic and perform complex operations within the Salesforce platform. It’s similar to Java and is used to automate processes, manipulate data, and customise system behaviours.
Key Features of Apex:
- It runs on Salesforce servers, ensuring security and reliability.
- Allows complex business logic, such as validations, triggers, and batch processing.
- Supports database operations through SOQL (Salesforce Object Query Language).
- Used for asynchronous processes like scheduled jobs.
When to Use Apex:
- Automating repetitive tasks (e.g., updating records, sending emails).
- Customising behaviour beyond standard Salesforce functionality.
- Writing triggers that execute automatically when data changes.
- Creating web services or integrations with external systems.
Example of Apex Code (Trigger):
trigger UpdateContactEmail on Account (after update) {
List<Contact> contactsToUpdate = new List<Contact>();
for (Account acc : Trigger.new) {
if (acc.Email__c != null) {
List<Contact> contacts = [SELECT Id FROM Contact WHERE AccountId = :acc.Id];
for (Contact con : contacts) {
con.Email = acc.Email__c;
contactsToUpdate.add(con);
}
}
}
if (!contactsToUpdate.isEmpty()) {
update contactsToUpdate;
}
}
This trigger ensures that whenever an Account’s custom email field is updated, the associated Contacts’ email fields are updated as well.
What is Visualforce?
Visualforce is a framework used to build custom user interfaces (UI) for Salesforce applications. It allows developers to create pages that go beyond the standard Salesforce UI and is especially useful for building unique layouts or embedding functionality.
Key Features of Visualforce:
- Provides a markup language (similar to HTML) to design pages.
- Supports custom controllers written in Apex to handle data and logic.
- Offers pre-built components to simplify development (e.g., forms, tables).
- Pages can be styled with CSS or enhanced with JavaScript for interactivity.
When to Use Visualforce:
- Creating highly customised pages that standard Salesforce layouts can’t support.
- Embedding pages into Lightning or Classic Salesforce environments.
- Designing interactive forms or data entry screens.
- Integrating third-party services directly into Salesforce.
Example of Visualforce Code (Page):
<apex:page controller="AccountController">
<apex:form>
<apex:pageMessages />
<apex:pageBlock title="Account Information">
<apex:pageBlockSection columns="2">
<apex:outputLabel value="Account Name:" />
<apex:inputField value="{!account.Name}" />
<apex:outputLabel value="Account Phone:" />
<apex:inputField value="{!account.Phone}" />
</apex:pageBlockSection>
<apex:commandButton value="Save" action="{!save}" />
</apex:pageBlock>
</apex:form>
</apex:page>
This page displays a form for editing an Account’s name and phone number. It relies on an Apex controller to handle saving changes.
Key Differences Between Apex and Visualforce:
Feature | Apex | Visualforce |
Purpose | Backend logic and automation | Frontend UI design and user interaction |
Type | Programming language | Framework for UI development |
Use Case | Data manipulation, triggers, web services | Custom pages, interactive forms, visual layouts |
Language | Similar to Java | Similar to HTML |
Integration | Works with SOQL, Visualforce, Lightning | Uses Apex controllers for backend logic |
Complexity | Requires programming knowledge | Easier to learn for those familiar with markup |
When to Use Apex and Visualforce Together
Apex and Visualforce often work hand-in-hand. For example, Visualforce pages can use Apex controllers to fetch and process data from Salesforce. This combination allows developers to build dynamic pages with backend logic seamlessly integrated. Example below:
Apex Controller:
public class AccountController {
public Account account { get; set; }
public AccountController() {
account = [SELECT Id, Name, Phone FROM Account LIMIT 1];
}
public void save() {
update account;
}
}
Visualforce Page:
<apex:page controller="AccountController">
<apex:form>
<apex:pageBlock title="Edit Account">
<apex:pageBlockSection>
<apex:inputField value="{!account.Name}" />
<apex:inputField value="{!account.Phone}" />
</apex:pageBlockSection>
<apex:commandButton value="Save" action="{!save}" />
</apex:pageBlock>
</apex:form>
</apex:page>
This example demonstrates a form where users can edit an Account’s details. The Visualforce page handles the UI, while the Apex controller manages the backend logic.
Final Thoughts
Apex and Visualforce are core tools for customising Salesforce. While Apex focuses on backend logic, Visualforce allows you to create custom user interfaces. Together, they enable developers to build powerful, tailored solutions for their organisation’s needs.
Whether you’re automating tasks with Apex or designing user-friendly pages with Visualforce, both tools offer incredible flexibility. If you need help creating custom Salesforce solutions, get in touch with IntegraLogic today for expert support!