Newer Version Available

This content describes an older version of this product. View Latest

Accessing sObject Fields

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

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.

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.

Note

If you want to perform operations on an sObject, it is recommended that you first convert it into a specific object. For example:
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: