Newer Version Available
Working with Salesforce Records
The term sObject refers to any object that can be stored in Lightning Platform. 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:
1Account a = new Account();
2MyCustomObject__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.
1public with sharing class AccountController {
2
3 @AuraEnabled
4 public static void updateAnnualRevenue(String accountId, Decimal annualRevenue) {
5 Account acct = [SELECT Id, Name, BillingCity FROM Account WHERE Id = :accountId];
6 acct.AnnualRevenue = annualRevenue;
7
8 // Perform isAccessible() and isUpdateable() checks here
9 update acct;
10 }
11}For an example of calling Apex code from JavaScript code, see the Quick Start.
Loading Record Data from a Standard Object
1public with sharing class OpportunityController {
2
3 @AuraEnabled
4 public static List<Opportunity> getOpportunities() {
5 List<Opportunity> opportunities =
6 [SELECT Id, Name, CloseDate FROM Opportunity];
7 return opportunities;
8 }
9
10 @AuraEnabled
11 public static Opportunity getOpportunity(Id id) {
12 Opportunity opportunity = [
13 SELECT Id, Account.Name, Name, CloseDate,
14 Owner.Name, Amount, Description, StageName
15 FROM Opportunity
16 WHERE Id = :id
17 ];
18
19 // Perform isAccessible() check here
20 return opportunity;
21 }
22}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
1public with sharing class MyObjController {
2
3 @AuraEnabled
4 public static List<MyObj__c> getMyObjects() {
5
6 // Perform isAccessible() checks here
7 return [SELECT Id, Name, myField__c FROM MyObj__c];
8 }
9}This example component uses the previous controller to display a list of records from the myObj__c custom object.
1<aura:component controller="MyObjController">
2 <aura:attribute name="myObjects" type="namespace.MyObj__c[]"/>
3 <aura:iteration items="{!v.myObjects}" var="obj">
4 {!obj.Name}, {!obj.namespace__myField__c}
5 </aura:iteration>
6</aura:component>1getMyObjects: function(cmp){
2 var action = cmp.get("c.getMyObjects");
3 action.setCallback(this, function(response){
4 var state = response.getState();
5 if (state === "SUCCESS") {
6 cmp.set("v.myObjects", response.getReturnValue());
7 }
8 });
9 $A.enqueueAction(action);
10}For an example on loading and updating records using controllers, see the Quick Start.