The Apex Master Class

When to Use the Global Keyword in Apex (Ep. 22)

3 min read

Subscribe on YouTube

First, What Is An Access Modifier?

This is the first of four episodes on access modifiers, so let’s define the term before we get into the specific one.

You’ve seen me writing public and private in front of classes, methods and variables since about episode 7. Those are access modifiers, and all they do is answer one question:

Who is allowed to touch this?

Apex gives you four, from most locked down to most open:

1. private – only this class
2. protected – this class and anything that extends it
3. public – anything in your org (well, your namespace)
4. global – anything, anywhere, including outside your org

Today we’re doing the last one, because it’s the one with genuine consequences attached.


What Global Actually Does

global is the widest access you can grant. A global class or method can be reached from anywhere, including from outside your org entirely, and including from other namespaces.

Apex
global class MyGlobalService
{
    global static String doSomething(String input)
    {
        return 'You said: ' + input;
    }
}

One syntax rule to know: a global method has to live in a global class. You can’t sneak a global method into a public class. Apex won’t have it.


When You Genuinely Need It

There are really only a few situations, and they’re all “the platform is making me”:

1. Web services callable from outside Salesforce.

Apex
@RestResource(urlMapping='/MyEndpoint/*')
global with sharing class MyRestResource
{
    @HttpGet
    global static String getIt()
    {
        return 'Hello from Salesforce';
    }
}

If an external system needs to call into your org, this is the way. I’ve got a full walkthrough in my post on building custom REST resources in Apex.

2. Managed packages.

This is the big one. If you’re building a package for AppExchange, anything a subscriber org needs to call must be global. Public isn’t enough, because your package has its own namespace and public stops at the namespace boundary.

3. Some Batch and Schedulable implementations.

Historically Database.Batchable and Schedulable implementations were written as global. These days public works fine for anything inside your own org, so you mostly see global here in older code and in packages.


Why You Should Be Careful With It

Here’s the part that actually matters, and it’s why I said think hard before typing it.

In a managed package, you can never remove or change a global method. Ever.

Not “it’s difficult.” Not “it needs a migration plan.” Salesforce will straight up refuse to let you upload a new package version that removes a global method or changes its signature. Because a subscriber somewhere might be calling it, and breaking their org isn’t an option.

So every global method you publish is a promise you’re making forever. Get the signature slightly wrong and you now maintain that mistake for the life of the package, usually alongside the doSomethingV2() you had to add next to it. You’ve seen packages with processV2, processV3, processFinal. Now you know why.

Even outside packaging, global widens your org’s public surface for no benefit if nothing external is calling it.

So: default to private, open up only as far as you actually need. Reach for global only when the platform genuinely requires it.


A Quick Note On webservice

You may bump into the webservice keyword in older code, used for SOAP endpoints:

Apex
global class MySoapService
{
    webservice static String doSomething(String input)
    {
        return 'You said: ' + input;
    }
}

It also requires a global class. For anything new, use @RestResource instead. SOAP still works, but you probably don’t want to start there in 2026 unless something on the other end is insisting.


What’s Next

Global: the widest access there is, needed for REST endpoints and managed packages, and permanent once published in a package. Use it sparingly.

The access modifiers documentation is here if you want the formal version.

In the next episode we cover public, which is the one you’ll actually type most days.

See you next time!


Get Coding With The Force Merch!!

We now have a redbubble store setup so you can buy cool Coding With The Force merchandise! Please check it out! Every purchase goes to supporting the blog and YouTube channel.

Get Shirts Here!
Get Cups, Artwork, Coffee Cups, Bags, Masks and more here!


Check Out More Coding With The Force Stuff!

If you liked this post make sure to follow us on all our social media outlets to stay as up to date as possible with everything!

Youtube
Patreon
Github
Facebook
Twitter
Instagram


Salesforce Development Books I Recommend

Advanced Apex Programming
Salesforce Lightning Platform Enterprise Architecture
Mastering Salesforce DevOps

Good Non-SF Specific Development Books:

Clean Code
Clean Architecture