Beginner 15 min read

Why Apex Exists

By the end of this lesson, you'll be able to:

  • Explain multitenancy in plain language
  • Describe why Salesforce needed its own language instead of letting customers run arbitrary code
  • Connect multitenancy to why governor limits exist

Prerequisites: "What Is Apex?"

The apartment building, revisited

Picture an apartment building where every tenant could run any appliance they wanted, at any wattage, with no limits. One tenant plugs in an industrial furnace, trips the building's power grid, and now nobody in the building has electricity. That's the risk Salesforce faced when it designed a platform where thousands of companies share the same servers, the same database engine, the same everything — this is called multitenancy.

If Salesforce let customers upload and run any code, in any language, with no restrictions, one company's badly-written script could realistically slow down or crash the shared system that thousands of other companies depend on at the same moment.

The solution: a controlled language

Apex is Salesforce's answer: a language they designed, compile, and run themselves, with hard limits built directly into the runtime — how many database queries a single transaction can run, how much memory it can use, how long it can take. These are called governor limits, and you'll meet them properly in a later module.

This is why Apex isn't "just Java on Salesforce" — a general-purpose language like Java has no concept of "you're only allowed 100 database queries in this transaction." Apex was built from day one with the shared-tenant reality in mind.

What this means for how you write code

Because of multitenancy, Apex code that works fine on 1 record can fail outright on 200 — not because of a bug in the traditional sense, but because it crosses a governor limit. This single fact is the reason so much of this course is structured around bulk-safe patterns (writing code that handles many records at once, safely) rather than "get it working for one record and move on."

A limit you will actually hit

// Querying inside a loop is a classic governor-limit mistake —
// covered in full in "Governor Limits and Bulk-Safe Code," but
// worth seeing once here so the concept isn't abstract.
for (Account acc : accountList) {
    List<Contact> contacts = [SELECT Id FROM Contact WHERE AccountId = :acc.Id];
    // ...
}

This runs one SOQL query per Account in the loop. With 5 accounts, fine. With 500, this can exceed Salesforce's limit of 100 SOQL queries per transaction — a direct, visible consequence of multitenancy's shared-resource rules.

Exercise

In your own words, write two or three sentences explaining multitenancy to someone who has never used Salesforce, using an analogy other than the apartment building from this lesson.

Show hint

Shared kitchens in a co-working space, a shared laundry room, a public swimming pool with lane rules — any "shared resource with rules" analogy works.

Why Apex Exists Quiz

1. What is multitenancy?

2. Governor limits exist mainly to make Apex harder to learn.

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

Apex exists because Salesforce runs every customer's org on shared infrastructure — multitenancy — and needed a language it could fully control, so one customer's runaway code could never slow down or break another customer's org.