An assignment statement is any statement that places a value into a
variable.
An assignment statement generally takes one of two
forms:
[LValue] = [new_value_expression]; [LValue] = [[inline_soql_query]];
In the forms above, [LValue] stands for any
expression that can be placed on the left side of an assignment operator. These include:
- A simple variable. For
example:
Integer i = 1; Account a = new Account(); Account[] accts = [SELECT Id FROM Account WITH USER_MODE];
- A de-referenced list element. For
example:
ints[0] = 1; accts[0].Name = 'Acme'; - An sObject field reference that the context user has permission to edit. For
example:
Account a = new Account(Name = 'Acme', BillingCity = 'San Francisco'); // IDs cannot be set prior to an insert call // a.Id = '00300000003T2PGAA0'; // Instead, insert the record. The system automatically assigns it an ID. insert as user a; // Fields also must be writable for the context user // a.CreatedDate = System.today(); This code is invalid because // createdDate is read-only! // Since the account a has been inserted, it is now possible to // create a new contact that is related to it Contact c = new Contact(LastName = 'Roth', Account = a); // Notice that you can write to the account name directly through the contact c.Account.Name = 'salesforce.com';
Assignment is always done by reference. For
example:
Account a = new Account(); Account b; Account[] c = new Account[]{}; a.Name = 'Acme'; b = a; c.add(a); // These asserts should now be true. You can reference the data // originally allocated to account a through account b and account list c. Assert.areEqual('Acme', b.Name); Assert.areEqual('Acme', c[0].Name);
Similarly, two lists can point at the same value in memory. For
example:
Account[] a = new Account[]{new Account()}; Account[] b = a; a[0].Name = 'Acme'; Assert.areEqua('Acme', b[0].Name);
In addition to =, other valid assignment operators include +=, *=, /=, |=, &=, ++, and --. See Expression Operators.