Newer Version Available

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

Inserting and Updating Records

Using DML, you can insert new records and commit them to the database. Similarly, you can update the field values of existing records.

Where possible, we changed noninclusive terms to align with our company value of Equality. We maintained certain terms to avoid any effect on customer implementations.

Important

This example inserts three account records and updates an existing account record. First, three Account sObjects are created and added to a list. An insert statement bulk inserts the list of accounts as an argument. Then, the second account record is updated, the billing city is updated, and the update statement is called to persist the change in the database.

1Account[] accts = new List<Account>();
2for(Integer i=0;i<3;i++) {
3    Account a = new Account(Name='Acme' + i, 
4                            BillingCity='San Francisco');
5    accts.add(a);
6}
7Account accountToUpdate;
8try {
9    insert accts;        
10    
11    // Update account Acme2.
12    accountToUpdate = 
13        [SELECT BillingCity FROM Account 
14         WHERE Name='Acme2' AND BillingCity='San Francisco'
15         LIMIT 1];
16    // Update the billing city.
17    accountToUpdate.BillingCity = 'New York';
18    // Make the update call.
19    update accountToUpdate;
20} catch(DmlException e) {
21    System.debug('An unexpected error has occurred: ' + e.getMessage());
22}
23
24// Verify that the billing city was updated to New York.
25Account afterUpdate = 
26    [SELECT BillingCity FROM Account WHERE Id=:accountToUpdate.Id];
27System.assertEquals('New York', afterUpdate.BillingCity);

Inserting Related Records

You can insert records related to existing records if a relationship has already been defined between the two objects, such as a lookup or master-detail relationship. A record is associated with a related record through a foreign key ID. For example, when inserting a new contact, you can specify the contact’s related account record by setting the value of the AccountId field.

This example adds a contact to an account (the related record) by setting the AccountId field on the contact. Contact and Account are linked through a lookup relationship.

1try {
2    Account acct = new Account(Name='SFDC Account');
3    insert acct;
4
5    // Once the account is inserted, the sObject will be 
6    // populated with an ID.
7    // Get this ID.
8    ID acctID = acct.ID;
9
10    // Add a contact to this account.
11    Contact con = new Contact(
12        FirstName='Joe',
13        LastName='Smith',
14        Phone='415.555.1212',
15        AccountId=acctID);
16    insert con;
17} catch(DmlException e) {
18    System.debug('An unexpected error has occurred: ' + e.getMessage());
19}

Updating Related Records

Fields on related records can't be updated with the same call to the DML operation and require a separate DML call. For example, if inserting a new contact, you can specify the contact's related account record by setting the value of the AccountId field. However, you can't change the account's name without updating the account itself with a separate DML call. Similarly, when updating a contact, if you also want to update the contact’s related account, you must make two DML calls. The following example updates a contact and its related account using two update statements.
1try {
2    // Query for the contact, which has been associated with an account.
3    Contact queriedContact = [SELECT Account.Name 
4                              FROM Contact 
5                              WHERE FirstName = 'Joe' AND LastName='Smith'
6                              LIMIT 1];
7
8    // Update the contact's phone number
9    queriedContact.Phone = '415.555.1213';
10
11    // Update the related account industry
12    queriedContact.Account.Industry = 'Technology';
13
14    // Make two separate calls 
15    // 1. This call is to update the contact's phone.
16    update queriedContact;
17    // 2. This call is to update the related account's Industry field.
18    update queriedContact.Account; 
19} catch(Exception e) {
20    System.debug('An unexpected error has occurred: ' + e.getMessage());
21}