No Results
Search Tips:
- Please consider misspellings
- Try different search keywords
Newer Version Available
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.com 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;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);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