⚡ Code it · 40 XP
sObjects and field access
sObjects and field access
Every Salesforce record in Apex is an sObject. Each object has its own concrete type (Account, Contact, Quest__c), and they all share the generic SObject type.
Concrete types: fields as properties
Account acme = new Account(Name = 'Acme', Industry = 'Technology');
acme.AnnualRevenue = 1000000;
System.debug(acme.Name);
The compiler knows every field, so typos fail at compile time. 👍
Generic SObject: fields by name
When you don't know the type at compile time (frameworks, dynamic code), use get and put:
SObject record = acme; // any sObject fits
Object raw = record.get('Name'); // returns Object
String name = (String) record.get('Name'); // cast it
record.put('Description', 'Set dynamically');
SObjectType t = record.getSObjectType(); // Account
Relationship fields
Contact c = [SELECT LastName, Account.Name FROM Contact LIMIT 1];
System.debug(c.Account.Name); // parent field via __r / relationship name
Records in memory vs in the database
Creating an sObject with new only builds it in memory. It doesn't exist in the database (and has no Id) until you insert it.
Your mission
Build a Contact in memory, read its fields both ways, and don't insert it. Quill is watching your DML count.