Sub topic 4
Passing Record IDs and Re-querying Data
This chapter assumes you can write basic Apex classes with static methods and collections, run SOQL queries and DML, and you know what an @future method looks like. You will learn why a future method should receive record IDs instead of full records, and how to re-query the data inside it.
Why not pass records?
A future method accepts only primitive types, arrays of primitives, and collections of primitives. It cannot accept sObjects or collections of sObjects. This restriction is deliberate. A future method does not run right away. It runs later, in its own transaction. If you passed a full record, the method would work with a snapshot taken at call time. Another transaction could change the record before the future method starts, and your code would then overwrite newer data with stale values.
Passing the ID avoids this. The method reads the record when it actually runs, so it sees current data.
The pattern
Follow three steps every time:
- Collect the IDs in the calling code.
- Pass them as a
Set<Id>orList<Id>. - Query the records inside the future method, then change them and save.
public class AccountService {
@future
public static void updateAccountRatings(Set<Id> accountIds) {
List<Account> accounts = [SELECT Id, Rating FROM Account WHERE Id IN :accountIds];
for (Account a : accounts) {
a.Rating = 'Hot';
}
update accounts;
}
}The method is static, returns void, and takes a Set<Id>. Id counts as a primitive type here, so this signature is allowed. The WHERE Id IN :accountIds clause uses a bind variable, so one query handles every record in the set.
Common errors
Passing sObjects. This declaration does not compile:
@future
public static void broken(List<Account> accounts) { }The compiler reports that the method has an unsupported parameter type. Change the parameter to Set<Id> and query inside.
Assuming the record still exists. Between the call and the run, someone may delete a record. The query then simply returns fewer rows. Do not index into the result list without checking its size.
Assuming the record still matches. Another process may have changed a field your logic depends on. If your method should act only on accounts in a certain state, add that condition to the query:
List<Account> accounts = [
SELECT Id, Rating
FROM Account
WHERE Id IN :accountIds AND Rating = null
];Querying one record at a time. Calling the future method once per record, with a query inside each call, burns limits fast. Pass the full set once.
Passing needed field values as parameters. If you pass a value such as a status string along with an ID, that value is also a snapshot. Pass it only when the snapshot is what you want, for example the value that triggered the change. Otherwise re-query it.
What if the data you need is not on a record?
Sometimes the future method needs a value that is not stored, such as a calculated flag or a setting chosen by the user. Pass it as a primitive next to the IDs:
public class RatingService {
@future
public static void setRating(Set<Id> accountIds, String rating) {
List<Account> accounts = [SELECT Id, Rating FROM Account WHERE Id IN :accountIds];
for (Account a : accounts) {
a.Rating = rating;
}
update accounts;
}
}Keep the parameter list short and simple. A Map<Id, String> is also valid when each record needs its own value, since a map of primitives is allowed.
Check your understanding
Exercise
A teammate writes @future public static void process(List<Contact> contacts). Why does this fail, and how do you fix it?
Show answer
Future methods cannot take sObjects or collections of sObjects as parameters. Change the parameter to Set<Id> contactIds (or List<Id>), then query the Contact records by ID inside the method, so the code also works with data that changed after the call.
Exercise
A future method receives 200 account IDs, and 5 of those accounts were deleted before it ran. What happens to the query SELECT Id FROM Account WHERE Id IN :accountIds?
Show answer
It returns the 195 accounts that still exist. The deleted records are not in the result and no error occurs, so the code must handle a result smaller than the input set.