Accessing sObject Fields

As in Java, sObject fields can be accessed or changed with simple dot notation. For example:

Account a = new Account();
a.Name = 'Acme';    // Access the account name field and assign it 'Acme'

System generated fields, such as Created By or Last Modified Date, cannot be modified. If you try, the Apex runtime engine generates an error. Additionally, formula field values and values for other fields that are read-only for the context user cannot be changed.

If you use the generic sObject type instead of a specific object, such as Account, you can retrieve only the Id field using dot notation. You can set the Id field for Apex code saved using Salesforce API version 27.0 and later). Alternatively, you can use the generic sObject put and get methods. See sObject Class.

This example shows how you can access the Id field and operations that aren’t allowed on generic sObjects.

Account a = new Account(Name = 'Acme', BillingCity = 'San Francisco');
insert a;
sObject s = [SELECT Id, Name FROM Account WHERE Name = 'Acme' LIMIT 1];
// This is allowed
ID id = s.Id;
// The following line results in an error when you try to save
String x = s.Name;
// This line results in an error when you try to save using API version 26.0 or earlier
s.Id = [SELECT Id FROM Account WHERE Name = 'Acme' LIMIT 1].Id;
Note

Note

If your organization has enabled person accounts, you have two different kinds of accounts: business accounts and person accounts. If your code creates a new account using name, a business account is created. If your code uses LastName, a person account is created.

If you want to perform operations on an sObject, it is recommended that you first convert it into a specific object. For example:
Account a = new Account(Name = 'Acme', BillingCity = 'San Francisco');
insert a;
sObject s = [SELECT Id, Name FROM Account WHERE Name = 'Acme' LIMIT 1];
ID id = s.ID;
Account convertedAccount = (Account)s;
convertedAccount.name = 'Acme2';
update convertedAccount;
Contact sal = new Contact(FirstName = 'Sal', Account = convertedAccount);
The following example shows how you can use SOSL over a set of records to determine their object types. Once you have converted the generic sObject record into a Contact, Lead, or Account, you can modify its fields accordingly:
public class convertToCLA {
    List<Contact> contacts;
    List<Lead> leads;
    List<Account> accounts;

    public void convertType(Integer phoneNumber) {
        List<List<sObject>> results = [FIND '4155557000' 
                         IN Phone FIELDS 
                         RETURNING Contact(Id, Phone, FirstName, LastName), 
                         Lead(Id, Phone, FirstName, LastName), Account(Id, Phone, Name)];
        sObject[] records = ((List<sObject>)results[0]);
    
        if (!records.isEmpty()) {
            for (Integer i = 0; i < records.size(); i++) {
              sObject record = records[i];
              if (record.getSObjectType() == Contact.sObjectType) {
                contacts.add((Contact) record);
              } else if (record.getSObjectType() == Lead.sObjectType){
                leads.add((Lead) record);
              } else if (record.getSObjectType() == Account.sObjectType) {
                accounts.add((Account) record);
              }
            }
        }  
    }
}
Previous
Next