Newer Version Available
Working with Salesforce Records
The term sObject refers to any object that can be stored in Force.com. This could be a standard object, such as Account, or a custom object that you create, such as a Merchandise object.
An sObject variable represents a row of data, also known as a record. To work with an object in Apex, declare it using the SOAP API name of the object. For example:
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17Account a = new Account();
18MyCustomObject__c co = new MyCustomObject__c();For more information on working on records with Apex, see Working with Data in Apex.
This example controller persists an updated Account record. Note that the update method has the @AuraEnabled annotation, which enables it to be called as a server-side controller action.
1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public with sharing class AccountController {
18
19 @AuraEnabled
20 public static void updateAnnualRevenue(String accountId, Decimal annualRevenue) {
21 Account acct = [SELECT Id, Name, BillingCity FROM Account WHERE Id = :accountId];
22 acct.AnnualRevenue = annualRevenue;
23
24 // Perform isAccessible() and isUpdateable() checks here
25 update acct;
26 }
27}For an example of calling Apex code from JavaScript code, see the Quick Start.
Loading Record Data from a Standard Object
1swfobject.registerObject("clippy.codeblock-2", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public with sharing class OpportunityController {
18
19 @AuraEnabled
20 public static List<Opportunity> getOpportunities() {
21 List<Opportunity> opportunities =
22 [SELECT Id, Name, CloseDate FROM Opportunity];
23 return opportunities;
24 }
25
26 @AuraEnabled
27 public static Opportunity getOpportunity(Id id) {
28 Opportunity opportunity = [
29 SELECT Id, Account.Name, Name, CloseDate,
30 Owner.Name, Amount, Description, StageName
31 FROM Opportunity
32 WHERE Id = :id
33 ];
34
35 // Perform isAccessible() check here
36 return opportunity;
37 }
38}1<aura:component controller="OpportunityController">
2 <aura:attribute name="opportunities" type="Opportunity[]"/>
3
4 <ui:button label="Get Opportunities" press="{!c.getOpps}"/>
5 <aura:iteration var="opportunity" items="{!v.opportunities}">
6 <p>{!opportunity.Name} : {!opportunity.CloseDate}</p>
7 </aura:iteration>
8</aura:component>1({
2 getOpps: function(cmp){
3 var action = cmp.get("c.getOpportunities");
4 action.setCallback(this, function(response){
5 var state = response.getState();
6 if (state === "SUCCESS") {
7 cmp.set("v.opportunities", response.getReturnValue());
8 }
9 });
10 $A.enqueueAction(action);
11 }
12})Loading Record Data from a Custom Object
1swfobject.registerObject("clippy.codeblock-5", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public with sharing class MyObjController {
18
19 @AuraEnabled
20 public static List<MyObj__c> getMyObjects() {
21
22 // Perform isAccessible() checks here
23 return [SELECT Id, Name, myField__c FROM MyObj__c];
24 }
25}This example component uses the previous controller to display a list of records from the myObj__c custom object.
1swfobject.registerObject("clippy.codeblock-6", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<aura:component controller="MyObjController"/>
18<aura:attribute name="myObjects" type="namespace.MyObj__c[]"/>
19<aura:iteration items="{!v.myObjects}" var="obj">
20 {!obj.Name}, {!obj.namespace__myField__c}
21</aura:iteration>1swfobject.registerObject("clippy.codeblock-7", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17getMyObjects: function(cmp){
18 var action = cmp.get("c.getMyObjects");
19 action.setCallback(this, function(response){
20 var state = response.getState();
21 if (state === "SUCCESS") {
22 cmp.set("v.myObjects", response.getReturnValue());
23 }
24 });
25 $A.enqueueAction(action);
26}For an example on loading and updating records using controllers, see the Quick Start.