Intermediate
25 min read
Dynamic sObjects
By the end of this lesson, you'll be able to:
- Access a field by name at runtime using get() and put()
- Explain when a dynamic field access is necessary versus dot notation
- Use Schema-based type resolution to create an sObject when the type isn't known at compile time
Prerequisites: "What Is an sObject?"
Reading and writing a field dynamically
Account acc = new Account();
acc.put('Name', 'Acme Logistics'); // same as acc.Name = 'Acme Logistics'
System.debug(acc.get('Name')); // same as acc.Name
Account acc = new Account();— a standard object.acc.put('Name', 'Acme Logistics');—put(fieldName, value)accesses a field using its API name as aString, rather than the fixedacc.Namedot notation.System.debug(acc.get('Name'));—get(fieldName)reads it back — the result is identical to dot notation, just a different way of reading and writing the same field.
When this is actually necessary
public void updateFieldDynamically(sObject record, String fieldName, Object newValue) {
record.put(fieldName, newValue);
}
Account acc = new Account();
updateFieldDynamically(acc, 'Industry', 'Manufacturing');
public void updateFieldDynamically(sObject record, String fieldName, Object newValue) {—fieldNameis only known at runtime, as a parameter — there's no way to writerecord.fieldNameliterally.record.put(fieldName, newValue);— dynamic access is the only option here.}— closes the method.- (blank line)
Account acc = new Account();— a standard object.updateFieldDynamically(acc, 'Industry', 'Manufacturing');— the field name arrives as a plainStringargument.
A real business example: Configurable Field Mapping
Map<String, Object> importedRow = new Map<String, Object>{
'Name' => 'Riverbend Farms',
'Industry' => 'Agriculture'
};
Account acc = new Account();
for (String fieldName : importedRow.keySet()) {
acc.put(fieldName, importedRow.get(fieldName));
}
Map<String, Object> importedRow = new Map<String, Object>{— simulates a row of imported spreadsheet data.'Name' => 'Riverbend Farms',— a column name mapped to its value.'Industry' => 'Agriculture'— another column.};— closes the map literal.- (blank line)
Account acc = new Account();— the record to populate.for (String fieldName : importedRow.keySet()) {— loops over whatever column names actually exist in this import — a data import tool has no way to know those field names at compile time.acc.put(fieldName, importedRow.get(fieldName));— dynamicput()is exactly what makes a genuinely reusable import routine possible.}— closes the loop, regardless of which fields a given spreadsheet happens to contain.
Exercise
Write a method getFieldValue(sObject record, String fieldName) that returns the value of the given field using dynamic access.
Show hint
return record.get(fieldName);
Dynamic sObjects Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
Sometimes the field or object you need to work with isn't known until the code actually runs — dynamic sObject access handles this using field names as Strings instead of fixed dot notation.