Asking the Lightning Platform About Itself

The Lightning Platform loves to talk about itself. You just have to ask the right questions and it will tell you just about anything you want to know.

Part of becoming a good Apex developer is learning how to ask the platform about itself and using the contextual data that it responds with to make decisions in your code.

Some of this might be jumping ahead a little since we haven’t yet gotten the concepts being mentioned, but this section is more to show you the breadth of information available from the platform to give you an idea of what information you can work with simply by asking the platform about itself.

The types of questions you will typically be asking the platform include:

  • Asking about the current running user
  • Asking about the database schema
  • Asking whether a user has permissions to access to a resource
  • Asking about the context in which Apex is executing
  • Asking whether code is being called by a unit test
  • Asking about governor limit usage
  • Asking about the instance of Salesforce you’re running on
  • Asking about when maintenance is scheduled
  • Asking what time it is

UserInfo Class

Apex includes a class named UserInfo that will return information about the current running user, include the username, the profile the user is assigned to, the user’s language and currency preferences, and so forth.

You can reference UserInfo directly in Apex code without having to perform a query on the User object.

Schema Describe Result Classes

You will use SOQL queries to retrieve data from the database, but if you want to ask the platform about the objects and fields in the database, you will use what is called the Schema class.

The Schema class can be referenced to return what are called DescribeSObjectResult and DescribeFieldResult objects that provide methods for returning data that describes database objects and fields.

These Describe Result objects contain a wealth of information and are typically used to genericize your Apex code to perform operations on objects or fields without creating a dependency on the referenced metadata.

Using Schema Describe Result Classes for Object and Field Security

Some of the most valuable pieces of data provided by Describe Result objects are returned by individual methods that can be interrogated to determine the current running user’s access to objects and fields.

At the object level, the DescribeSObjectResult class provides CRED (Create / Read / Edit / Delete) details for the user based on their profile settings.

At the field level, the DescribeFieldResult class provides FLS (Field Level Security) details for the user, including whether they have access to a field, and if they do have access to the field whether it’s read-only or read-write access.

Determining the Apex Runtime Execution Context

One of the things you will learn is that Apex is an on-demand language that does not execute until it is explicitly invoked from one of a number of contexts.

From within your Apex code, you can actually ask the platform about which context the Apex transaction is running in.

You can determine if Apex is running in an asynchronous context (Future, Queueable or Batch), if it was invoked by a scheduled job, or if it was invoked by a Trigger.

Determining if Apex Code is Being Executed From a Unit Test

Similar to asking the platform about its runtime execution context, you can ask the platform if your Apex runtime code is being called by a test method.

Knowing Where You Stand With Governor Limits

Nobody is exempt from governor limits. Knowing what the governor limit for a specific resource is for the current release as well as what your Apex code has consumed at a certain point in a transaction can help you code defensively and avoid fatal, uncatchable exceptions in your runtime code.

The Limits class has roughly three dozen methods that will tell you where you stand with everything from SOQL queries, to DML statements, to memory usage, to CPU time, and just about everything in between.

Identifying the Salesforce Org

You have the ability to ask the platform whether your code is running in the production instance of Salesforce, or in a sandbox org.

There is another class that can provide you with the base URL of your Salesforce instance if you need to use it for creating URLs.

Knowing When Maintenance is Scheduled

Not many people know this, but you can actually ask the platform when maintenance is scheduled.

This could potentially be of value if you want to ensure that no critical processes are executed when the database is placed into read-only mode during a maintenance window.

Does Anyone Really Know What Time It Is?

It may seem like a trivial task to perform, but you can rest easier at night knowing that there are a number of system classes available to help you figure out what time it is.

This is by no means a comprehensive list of things that you can ask the platform, but it does demonstrate the power of the Lightning Platform and what information you can get from it.

All you have to do is ask.