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:

1Account a = new Account();
2a.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.

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

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:
1Account a = new Account(Name = 'Acme', BillingCity = 'San Francisco');
2insert a;
3sObject s = [SELECT Id, Name FROM Account WHERE Name = 'Acme' LIMIT 1];
4ID id = s.ID;
5Account convertedAccount = (Account)s;
6convertedAccount.name = 'Acme2';
7update convertedAccount;
8Contact 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:
1swfobject.registerObject("clippy.codeblock-3", "9");public class convertToCLA {
2    List<Contact> contacts;
3    List<Lead> leads;
4    List<Account> accounts;
5
6    public void convertType(Integer phoneNumber) {
7        List<List<sObject>> results = [FIND '4155557000' 
8                         IN Phone FIELDS 
9                         RETURNING Contact(Id, Phone, FirstName, LastName), 
10                         Lead(Id, Phone, FirstName, LastName), Account(Id, Phone, Name)];
11        sObject[] records = ((List<sObject>)results[0]);
12    
13        if (!records.isEmpty()) {
14            for (Integer i = 0; i < records.size(); i++) {
15              sObject record = records[i];
16              if (record.getSObjectType() == Contact.sObjectType) {
17                contacts.add((Contact) record);
18              } else if (record.getSObjectType() == Lead.sObjectType){
19                leads.add((Lead) record);
20              } else if (record.getSObjectType() == Account.sObjectType) {
21                accounts.add((Account) record);
22              }
23            }
24        }  
25    }
26}
27