When to use a future method
There are two common use cases for future methods that most developers are familiar with—avoiding mixed DML errors and making background API requests. These are not the only use cases, but they are the two most common ones and so we will discuss them first.
Mixed DML
Certain sObjects
cannot be used with other sObjects
in DML operations without causing a mixed DML error. These are known as setup objects and are any object that affects a user’s record access. The most common examples are when creating a User
with a specified role or updating a User
and changing one of the following fields:
UserRoleId
IsActive
ForecastEnabled
IsPortalEnabled
Username
ProfileId
In these situations, it is a common practice to refactor the User
(or another setup object) manipulation into a future method to be managed asynchronously. Most actions on these objects are not considered time-sensitive; for example, marking an inactive...