Sub topic 10
SObjects as Data Types
This chapter assumes you know Apex primitives, variables, and collections (List, Set, Map), and that you understand Salesforce objects and fields at a high level.
What an SObject is
An SObject is the Apex type that represents a Salesforce record. Every standard and custom object has a matching type. Account, Contact, and Invoice__c are all SObject types. A variable of one of these types holds one record. Its fields are properties you read and write with dot notation.
Apex also has a generic type named SObject. It can hold a record of any object type. Use it when code must work across several objects.
Declaring and creating records
You create a record in memory with new. Nothing is saved to the database until you run a DML statement such as insert.
Account acc = new Account();
acc.Name = 'Acme';
acc.Industry = 'Retail';
Account acc2 = new Account(Name = 'Globex', NumberOfEmployees = 50);
System.debug(acc.Name);
System.debug(acc2.NumberOfEmployees);The second form sets fields in the constructor. A field you never set holds null, as with any other variable.
Custom objects and fields use the __c suffix, for example Invoice__c and Amount__c.
Generic and specific variables
A specific variable gives you compile-time checking of field names. A generic variable accepts any record type, but you must use methods to reach fields.
Account acc = new Account(Name = 'Acme');
SObject record = acc;
record.put('Name', 'Acme Corp');
System.debug(record.get('Name'));
Account back = (Account) record;
System.debug(back.Name);put and get take the field name as a string. get returns Object, so you often cast the result. Assigning a specific type to SObject needs no cast. Going back to the specific type needs an explicit cast, as shown above. A wrong cast throws a TypeException at run time.
Collections of SObjects
Lists, sets, and maps can hold SObjects. A list of records is the standard input for DML and the standard result of a query.
List<Account> accounts = new List<Account>();
accounts.add(new Account(Name = 'Acme'));
accounts.add(new Account(Name = 'Globex'));
List<SObject> mixed = new List<SObject>();
mixed.add(new Account(Name = 'Initech'));
mixed.add(new Contact(LastName = 'Smith'));
System.debug(accounts.size());
System.debug(mixed.size());A List<Account> can be assigned to a List<SObject> variable. Keep each list to one object type when you plan to run DML on it. A list that mixes types can fail or behave unexpectedly depending on how it is declared.
A Map<Id, Account> is common. You can build one straight from a list of records:
List<Account> accounts = new List<Account>{
new Account(Name = 'Acme'),
new Account(Name = 'Globex')
};
Map<Id, Account> byId = new Map<Id, Account>(accounts);
System.debug(byId.size());Records that are not yet inserted have a null Id. The map above is therefore empty. Records returned by a query or saved by insert have Ids, and then the map holds one entry per record.
Common errors
- Assigning a generic
SObjectto a specific type without a cast. This is a compile error. - Casting to the wrong object type. This is a
TypeExceptionat run time. - Misspelling a field name in
getorput. This fails at run time, not at compile time. - Assuming a new record has an Id. It does not until it is inserted.
Exercise
You build Map<Id, Account> m = new Map<Id, Account>(accs); from a list of new, un-inserted accounts. Why is m.size() 0, and what makes it non-zero?
Show answer
Un-inserted records have a null Id, so the map has no keys to use. After the records are inserted, or when they come from a query, each has an Id and the map holds one entry per record.
Exercise
What is the difference between Account a = (Account) record; and SObject s = a; in terms of casting?
Show answer
Assigning a specific type to the generic SObject type is implicit and needs no cast. Converting a generic SObject back to Account needs an explicit cast and throws a TypeException if the record is not an Account.