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
  1. Account acc = new Account(); — a standard object.
  2. acc.put('Name', 'Acme Logistics'); — put(fieldName, value) accesses a field using its API name as a String, rather than the fixed acc.Name dot notation.
  3. 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');
  1. public void updateFieldDynamically(sObject record, String fieldName, Object newValue) { — fieldName is only known at runtime, as a parameter — there's no way to write record.fieldName literally.
  2. record.put(fieldName, newValue); — dynamic access is the only option here.
  3. } — closes the method.
  4. (blank line)
  5. Account acc = new Account(); — a standard object.
  6. updateFieldDynamically(acc, 'Industry', 'Manufacturing'); — the field name arrives as a plain String argument.

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));
}
  1. Map<String, Object> importedRow = new Map<String, Object>{ — simulates a row of imported spreadsheet data.
  2. 'Name' => 'Riverbend Farms', — a column name mapped to its value.
  3. 'Industry' => 'Agriculture' — another column.
  4. }; — closes the map literal.
  5. (blank line)
  6. Account acc = new Account(); — the record to populate.
  7. 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.
  8. acc.put(fieldName, importedRow.get(fieldName)); — dynamic put() is exactly what makes a genuinely reusable import routine possible.
  9. } — 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);

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Dynamic sObjects Quiz

1. When is dynamic field access (get/put) necessary instead of dot notation?

Log in to submit the quiz and save your score.

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.